gpt4 book ai didi

hadoop - 将 MapWritable 转换为 Hadoop 中的字符串

转载 作者:可可西里 更新时间:2023-11-01 14:49:59 26 4
gpt4 key购买 nike

当我使用 toString() 方法运行我的输出时,我得到:

#zombie org.apache.hadoop.io.MapWritable@b779f586
#zombies org.apache.hadoop.io.MapWritable@c8008ef9
#zona org.apache.hadoop.io.MapWritable@99e061a1
#zoology org.apache.hadoop.io.MapWritable@9d0060be
#zzp org.apache.hadoop.io.MapWritable@3e52c108

这是我的 reducer 代码,我怎样才能让 map 值打印出来?

package sample;

import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.MapWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.Reducer;

public class IntSumReducer
extends Reducer<Text,MapWritable,Text,MapWritable> {
private MapWritable result = new MapWritable();
String temp = "";
public void reduce(Text key, Iterable<MapWritable> values, Context context)throws IOException, InterruptedException {
result.clear();
for (MapWritable val : values) {
Iterable<Writable> keys = val.keySet();
for (Writable k : keys) {
IntWritable tally = (IntWritable) val.get(k);
if (result.containsKey(k)) {
IntWritable tallies = (IntWritable) result.get(k);
tallies.set(tallies.get() + tally.get());
temp = toString() + " : " + tallies.get();
result.put(new Text(temp), tallies);
} else {
temp = k.toString() + " : " + tally.get();
result.put(new Text(temp), tally);
}
}
}
context.write(key, result);
}
}

感谢帮助

最佳答案

添加这样的类应该可行:

class MyMapWritable extends MapWritable {
@Override
public String toString() {
StringBuilder result = new StringBuilder();
Set<Writable> keySet = this.keySet();

for (Object key : keySet) {
result.append("{" + key.toString() + " = " + this.get(key) + "}");
}
return result.toString();
}
}

然后这样调用:

MyMapWritable mw = new MyMapWritable();
mw.toString();

关于hadoop - 将 MapWritable 转换为 Hadoop 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23209174/

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