Java 产生垃圾值的MapReduce键值对的输出值
问题陈述-找到最大值并将其与密钥一起打印 输入:Java 产生垃圾值的MapReduce键值对的输出值,java,hadoop,mapreduce,Java,Hadoop,Mapreduce,问题陈述-找到最大值并将其与密钥一起打印 输入: Key Value ABC 10 TCA 13 RTY 23 FTY 45 左侧列上的键将是唯一的。不允许重复 输出: FTY 45 因为45是所有值中的最高值,所以它必须与键一起打印 我已经基于此链接中共享的伪代码编写了MapReduce代码 地图- import java.io.IOException; import java.util.StringTokenizer
Key Value
ABC 10
TCA 13
RTY 23
FTY 45
左侧列上的键将是唯一的。不允许重复
输出:
FTY 45
因为45是所有值中的最高值,所以它必须与键一起打印
我已经基于此链接中共享的伪代码编写了MapReduce代码
地图-
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.mapreduce.Mapper;
public class Map
extends Mapper<LongWritable,Text,Text,IntWritable>
{
private Text maxKey = new Text();
private IntWritable maxValue = new IntWritable(Integer.MIN_VALUE);
@Override
protected void map( LongWritable key,Text value,Context context)
throws IOException,InterruptedException
{
String line = value.toString().trim();
StringTokenizer token = new StringTokenizer(line);
if(token.countTokens() == 2)
{
String str = token.nextToken();
while(token.hasMoreTokens())
{
int temp = Integer.parseInt(token.nextToken());
if(temp > maxValue.get())
{
maxValue.set(temp);
maxKey.set(str);
}
}
}
}
@Override
protected void cleanup(Context context)
throws IOException,InterruptedException
{
context.write(maxKey,maxValue);
}
}
程序编译并运行以显示此输出-
-2147483648
maxValue of map()和reduce()的设置可能不正确。如何正确设置值(使用Integer.MIN_值初始化和比较后更新),以便reduce()函数接收正确的键值对?由于键总是唯一的,因此无法在reducer中聚合它们。因此,如果您的数据集不是非常大,您可以使用一个公共键编写映射器的输出,这将强制映射器的所有输出只转到一个减速机 然后在reducer中,您可以对值进行迭代以比较,并将最大值与键一起写入 在mapper类中,使用公共密钥对将文件写入
context
public class Map extends Mapper<LongWritable,Text,Text,Text>{
private final Text commonKey = new Text("CommonKey");
@Override
protected void map( LongWritable key,Text value,Context context)
throws IOException,InterruptedException {
String line = value.toString().trim();
String[] kvpair = line.split("\\s+");
context.write(commonKey, new Text(kvpair[0] + "," + kvpair[1]));
}
}
公共类映射扩展映射器{
私有最终文本commonKey=新文本(“commonKey”);
@凌驾
受保护的void映射(可长写键、文本值、上下文)
抛出IOException、InterruptedException{
字符串行=value.toString().trim();
字符串[]kvpair=line.split(\\s+);
write(commonKey,新文本(kvpair[0]+”,“+kvpair[1]);
}
}
然后在reducer中,找到最大值并写入上下文
public static class Reduce extends Reducer<Text, Text, NullWritable, Text>{
private final Integer MAXIMUM_VALUE = Integer.MIN_VALUE;
public void reduce(Text commonKey, Iterable<Text> values, Context context){
Integer finalMax = MAXIMUM_VALUE;
String finalKey;
for (Text value: values){
String[] kvpair = value.toString().trim().split(",")
if(Integer.parseInt(kvpair[1]) > finalMax){
finalKey = kvpair[0];
finalMax = Integer.parseInt(kvpair[1]);
}
}
context.write(new Text(finalKey), new IntWritable(finalMax) );
}
}
公共静态类Reduce扩展Reducer{
私有最终整数最大值=Integer.MIN\u值;
public void reduce(文本公用键、Iterable值、上下文){
整数finalMax=最大值;
字符串finalKey;
用于(文本值:值){
字符串[]kvpair=value.toString().trim().split(“,”)
if(Integer.parseInt(kvpair[1])>finalMax){
finalKey=kvpair[0];
finalMax=Integer.parseInt(kvpair[1]);
}
}
write(新文本(finalKey),新intwriteable(finalMax));
}
}
代码中可能会出现一些错误。只是在一个文本编辑器中编写,让您稍微了解如何以不同的方式处理问题。另外,请注意,
key
也没有编写。因此,在程序的整个生命周期中,变量maxKey
和maxValue
很可能保持不变。另一方面:为什么不使用Spark来实现这一点?我是一名Hadoop初学者,刚刚开始学习Java中MapReduce的概念。谢谢你的建议!我一定会记住这一点,一旦我进入那里,我会使用Spark。
public class Map extends Mapper<LongWritable,Text,Text,Text>{
private final Text commonKey = new Text("CommonKey");
@Override
protected void map( LongWritable key,Text value,Context context)
throws IOException,InterruptedException {
String line = value.toString().trim();
String[] kvpair = line.split("\\s+");
context.write(commonKey, new Text(kvpair[0] + "," + kvpair[1]));
}
}
public static class Reduce extends Reducer<Text, Text, NullWritable, Text>{
private final Integer MAXIMUM_VALUE = Integer.MIN_VALUE;
public void reduce(Text commonKey, Iterable<Text> values, Context context){
Integer finalMax = MAXIMUM_VALUE;
String finalKey;
for (Text value: values){
String[] kvpair = value.toString().trim().split(",")
if(Integer.parseInt(kvpair[1]) > finalMax){
finalKey = kvpair[0];
finalMax = Integer.parseInt(kvpair[1]);
}
}
context.write(new Text(finalKey), new IntWritable(finalMax) );
}
}