-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexingCombiner.java
More file actions
34 lines (32 loc) · 1.17 KB
/
Copy pathIndexingCombiner.java
File metadata and controls
34 lines (32 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class IndexingCombiner extends Reducer<Text, Item, Text, Item>{
@Override
public void reduce(Text key, Iterable<Item> values, Context context) throws IOException, InterruptedException {
map<String, Integer> map = new HashMap<>();
for(Item item: values){
String fileName = item.getFileName().toString();
int count = item.getCount();
if (map.containsKey(fileName)) {
map.put(fileName, map.get(fileName) + count);
}
else {
map.put(fileName, count);
}
// map.put(fileName, map.getOrDefault(fileName, 0) + count);
}
Iterator<Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
@SuppressWarnings("unchecked")
Map.Entry<String, Integer> pair = (Map.Entry<String, Integer>) iterator.next();
// context.write(key, new Item(new Text(pair.getKey()), new IntWritable(pair.getValue())));
context.write(key, new Item(pair.getKey(), pair.getValue()));
}
}
}