gpt4 book ai didi

java - 使用 map reduce 的行数

转载 作者:可可西里 更新时间:2023-11-01 14:48:00 24 4
gpt4 key购买 nike

我每天都有几千个文件从其他人那里放到一个目录中,每个文件大约 400MB 到 1GB 大。

我想统计目录中的总行数。

我打算像下面这样做 map reduce

映射器

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,那么最好的选择是使用计数器。将您的映射器更改为类似这样的内容,并将缩减器的数量设置为 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 - 使用 map reduce 的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51069818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com