1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.mapreduce;
21
22 import java.io.DataInput;
23 import java.io.DataOutput;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.apache.hadoop.io.NullWritable;
29 import org.apache.hadoop.io.Writable;
30 import org.apache.hadoop.mapreduce.InputFormat;
31 import org.apache.hadoop.mapreduce.InputSplit;
32 import org.apache.hadoop.mapreduce.JobContext;
33 import org.apache.hadoop.mapreduce.RecordReader;
34 import org.apache.hadoop.mapreduce.TaskAttemptContext;
35
36
37
38
39
40
41
42
43 public class NMapInputFormat extends InputFormat<NullWritable, NullWritable> {
44
45 @Override
46 public RecordReader<NullWritable, NullWritable> createRecordReader(
47 InputSplit split,
48 TaskAttemptContext tac) throws IOException, InterruptedException {
49 return new SingleRecordReader<NullWritable, NullWritable>(
50 NullWritable.get(), NullWritable.get());
51 }
52
53 @Override
54 public List<InputSplit> getSplits(JobContext context) throws IOException,
55 InterruptedException {
56 int count = context.getConfiguration().getInt("mapred.map.tasks", 1);
57 List<InputSplit> splits = new ArrayList<InputSplit>(count);
58 for (int i = 0; i < count; i++) {
59 splits.add(new NullInputSplit());
60 }
61 return splits;
62 }
63
64 private static class NullInputSplit extends InputSplit implements Writable {
65 @Override
66 public long getLength() throws IOException, InterruptedException {
67 return 0;
68 }
69
70 @Override
71 public String[] getLocations() throws IOException, InterruptedException {
72 return new String[] {};
73 }
74
75 @Override
76 public void readFields(DataInput in) throws IOException {
77 }
78
79 @Override
80 public void write(DataOutput out) throws IOException {
81 }
82 }
83
84 private static class SingleRecordReader<K, V>
85 extends RecordReader<K, V> {
86
87 private final K key;
88 private final V value;
89 boolean providedKey = false;
90
91 SingleRecordReader(K key, V value) {
92 this.key = key;
93 this.value = value;
94 }
95
96 @Override
97 public void close() {
98 }
99
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) return false;
122 providedKey = true;
123 return true;
124 }
125
126 }
127 }