在java mapreduce中查找具有最大值的键
我试图找到具有最大值的键,但每次都会生成一个错误,表示无法将文本转换为字符串。这与java mapreduce有关 错误: TaskInputOutputContext类型中的write(Text,IntWritable)方法不适用于参数(String,IntWritable) 列表:在java mapreduce中查找具有最大值的键,java,mapreduce,Java,Mapreduce,我试图找到具有最大值的键,但每次都会生成一个错误,表示无法将文本转换为字符串。这与java mapreduce有关 错误: TaskInputOutputContext类型中的write(Text,IntWritable)方法不适用于参数(String,IntWritable) 列表: 994290 5 994380 33 994410 1 994440 11 995010 2 995030 5 994380 33 @Override public
994290 5
994380 33
994410 1
994440 11
995010 2
995030 5
994380 33
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int max = 0;
String keyWithMax = "";
for (IntWritable value : values) {
if (value.get() > max) {
max = value.get();
keyWithMax = key.toString();
}
}
context.write(keyWithMax, new IntWritable(max));
}
预期的:
994290 5
994380 33
994410 1
994440 11
995010 2
995030 5
994380 33
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int max = 0;
String keyWithMax = "";
for (IntWritable value : values) {
if (value.get() > max) {
max = value.get();
keyWithMax = key.toString();
}
}
context.write(keyWithMax, new IntWritable(max));
}
代码:
994290 5
994380 33
994410 1
994440 11
995010 2
995030 5
994380 33
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int max = 0;
String keyWithMax = "";
for (IntWritable value : values) {
if (value.get() > max) {
max = value.get();
keyWithMax = key.toString();
}
}
context.write(keyWithMax, new IntWritable(max));
}
@覆盖
公共void reduce(文本键、Iterable值、上下文)
抛出IOException、InterruptedException{
int max=0;
字符串keyWithMax=“”;
for(可写入值:值){
if(value.get()>max){
max=value.get();
keyWithMax=key.toString();
}
}
write(keyWithMax,新的IntWritable(max));
}
你能帮我一下吗?我想你不需要用
键做任何事情,你可以直接用它
@Override
public void reduce(Text key, Iterable<IntWritable> values,
Context context)
throws IOException, InterruptedException {
int max= 0;
for (IntWritable value : values) {
if(value.get() > max){
max= value.get();
}
}
context.write(key, new IntWritable(max));
}
@覆盖
public void reduce(文本键、Iterable值、,
上下文(上下文)
抛出IOException、InterruptedException{
int max=0;
for(可写入值:值){
if(value.get()>max){
max=value.get();
}
}
write(key,新的IntWritable(max));
}
您正在将keyWithMax
传递给上下文.write()
方法,该方法是字符串类型。它需要Text()
数据类型作为键
问题在于行context.write(keyWithMax,newintWritable(max))代码>
将其更改为context.write(新文本(keyWithMax),新的IntWritable(max))代码>
或者更好,将输出键的全局变量定义为
private Text outKey= new Text();
并在写入上下文之前设置它
outKey.set(keyWithMax);
这是因为创建一个新文本()
比创建一次并重复使用成本更高。与新IntWritable(max)的情况相同
Hi avigil,上面修改的代码显示具有最小值的键。你能看一下吗?这个减少步骤将找到单个键的最大值,你还需要一个额外的步骤来找到所有键中的最大值。嗨,阿维吉尔,你能帮我写几行代码吗?因为我对java和大数据还不熟悉,这让我有点困惑…提前感谢请检查我的答案。希望能有帮助。