001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.util.bulkdatagenerator;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.List;
023import org.apache.hadoop.io.NullWritable;
024import org.apache.hadoop.io.Text;
025import org.apache.hadoop.mapreduce.InputFormat;
026import org.apache.hadoop.mapreduce.InputSplit;
027import org.apache.hadoop.mapreduce.JobContext;
028import org.apache.hadoop.mapreduce.RecordReader;
029import org.apache.hadoop.mapreduce.TaskAttemptContext;
030
031import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
032
033public class BulkDataGeneratorInputFormat extends InputFormat<Text, NullWritable> {
034
035  public static final String MAPPER_TASK_COUNT_KEY =
036    BulkDataGeneratorInputFormat.class.getName() + "mapper.task.count";
037
038  @Override
039  public List<InputSplit> getSplits(JobContext job) throws IOException {
040    // Get the number of mapper tasks configured
041    int mapperCount = job.getConfiguration().getInt(MAPPER_TASK_COUNT_KEY, -1);
042    Preconditions.checkArgument(mapperCount > 1, MAPPER_TASK_COUNT_KEY + " is not set.");
043
044    // Create a number of input splits equal to the number of mapper tasks
045    ArrayList<InputSplit> splits = new ArrayList<InputSplit>();
046    for (int i = 0; i < mapperCount; ++i) {
047      splits.add(new BulkDataGeneratorInputSplit());
048    }
049    return splits;
050  }
051
052  @Override
053  public RecordReader<Text, NullWritable> createRecordReader(InputSplit split,
054    TaskAttemptContext context) throws IOException, InterruptedException {
055    BulkDataGeneratorRecordReader bulkDataGeneratorRecordReader =
056      new BulkDataGeneratorRecordReader();
057    bulkDataGeneratorRecordReader.initialize(split, context);
058    return bulkDataGeneratorRecordReader;
059  }
060}