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.mapreduce;
019
020import java.io.DataInput;
021import java.io.DataOutput;
022import java.util.ArrayList;
023import java.util.List;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.io.NullWritable;
026import org.apache.hadoop.io.Writable;
027import org.apache.hadoop.mapreduce.InputFormat;
028import org.apache.hadoop.mapreduce.InputSplit;
029import org.apache.hadoop.mapreduce.JobContext;
030import org.apache.hadoop.mapreduce.RecordReader;
031import org.apache.hadoop.mapreduce.TaskAttemptContext;
032
033/**
034 * Input format that creates a configurable number of map tasks each provided with a single row of
035 * NullWritables. This can be useful when trying to write mappers which don't have any real input
036 * (eg when the mapper is simply producing random data as output)
037 */
038public class NMapInputFormat extends InputFormat<NullWritable, NullWritable> {
039  private static final String NMAPS_KEY = "nmapinputformat.num.maps";
040
041  @Override
042  public RecordReader<NullWritable, NullWritable> createRecordReader(InputSplit split,
043    TaskAttemptContext tac) {
044    return new SingleRecordReader<>(NullWritable.get(), NullWritable.get());
045  }
046
047  @Override
048  public List<InputSplit> getSplits(JobContext context) {
049    int count = getNumMapTasks(context.getConfiguration());
050    List<InputSplit> splits = new ArrayList<>(count);
051    for (int i = 0; i < count; i++) {
052      splits.add(new NullInputSplit());
053    }
054    return splits;
055  }
056
057  public static void setNumMapTasks(Configuration conf, int numTasks) {
058    conf.setInt(NMAPS_KEY, numTasks);
059  }
060
061  public static int getNumMapTasks(Configuration conf) {
062    return conf.getInt(NMAPS_KEY, 1);
063  }
064
065  private static class NullInputSplit extends InputSplit implements Writable {
066    @Override
067    public long getLength() {
068      return 0;
069    }
070
071    @Override
072    public String[] getLocations() {
073      return new String[] {};
074    }
075
076    @Override
077    public void readFields(DataInput in) {
078    }
079
080    @Override
081    public void write(DataOutput out) {
082    }
083  }
084
085  private static class SingleRecordReader<K, V> extends RecordReader<K, V> {
086
087    private final K key;
088    private final V value;
089    boolean providedKey = false;
090
091    SingleRecordReader(K key, V value) {
092      this.key = key;
093      this.value = value;
094    }
095
096    @Override
097    public void close() {
098    }
099
100    @Override
101    public K getCurrentKey() {
102      return key;
103    }
104
105    @Override
106    public V getCurrentValue() {
107      return value;
108    }
109
110    @Override
111    public float getProgress() {
112      return 0;
113    }
114
115    @Override
116    public void initialize(InputSplit split, TaskAttemptContext tac) {
117    }
118
119    @Override
120    public boolean nextKeyValue() {
121      if (providedKey) {
122        return false;
123      }
124
125      providedKey = true;
126      return true;
127    }
128  }
129}