我每天都有几千个文件从其他人那里放到目录中,每个文件大约 400MB 到 1GB 大。
我想计算目录中的总行数。
我打算像下面这样进行 map 缩减
映射器
public static class LineMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
context.write("static_key", one);
}
}
reducer
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
但是经过思考,我觉得reducer节点将会不堪重负,因为只有1个key。
有办法避免这种情况吗?
最佳答案
如果你想为此使用mapreduce,那么最好的选择是使用计数器。将您的映射器更改为类似的内容并将 reducer 数量设置为 0。
public static class LineMapper extends Mapper<Object, Text, Text, IntWritable>{
enum MyCounters {
TOTAL_COUNT;
}
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
context.getCounter(MyCounters.TOTAL_COUNT).increment(1L);
}
}
关于java - 使用 MapReduce 进行行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51069818/