Mapreduce函数,使用java计算入度和出度并显示和

Mapreduce函数,使用java计算入度和出度并显示和,java,mapreduce,Java,Mapreduce,我试图对一组数据的入度和出度求和。 以下是示例数据: Source Target 1 2 2 1 3 1 2 3 因此,预期产出为: ID In degree Out degree 1 2 1 2 1 2 3 1 1 如何使用mapreduce Java实现这一点,并在一行中打印结果。一个

我试图对一组数据的入度和出度求和。 以下是示例数据:

Source  Target

1        2  
2        1  
3        1  
2        3  
因此,预期产出为:

ID     In degree   Out degree  
1       2            1  
2       1            2  
3       1            1  

如何使用mapreduce Java实现这一点,并在一行中打印结果。

一个选项涉及一个MR作业: 假设原始数据集看起来像
[node1,node2]

-映射器读取原始数据集,并为每一行发出三元组
[node1,out]
[node2,in]

-reducer以
[key,label]
的形式从映射器中获取三元组,通过分别计算每个键的“out”标签和“in”标签来计算outdegree和indegree,并以
[key,indegree,outdegree]的形式输出它们。

实现类似于以下内容(假设数据集中的
node1
node2
以空格分隔,并且还假设数据集中仅包含不同的对):

制图员:

public class YourMapper extends MapReduceBase implements Mapper<LongWritable, Text, Text, Text> {

      public void map(LongWritable key, Text value, OutputCollector<Text, Text> output, Reporter reporter) throws IOException {

        String line = value.toString();     
        String[] line_spl = line.split(" ");

        String node1 = line_spl[0];
        String node2 = line_spl[1];

        Text node1_txt = new Text(node1);
        Text node2_txt = new Text(node2);
        Text emit_out = new Text("out");
        Text emit_in  = new Text("in");

        output.collect(node1_txt, emit_out);
        output.collect(node2_txt, emit_in );

      }//end map function


}//end mapper class
公共类YourMapper扩展MapReduceBase实现Mapper{
公共void映射(LongWritable键、文本值、OutputCollector输出、Reporter报告器)引发IOException{
字符串行=value.toString();
String[]line\u spl=line.split(“”);
字符串node1=line_spl[0];
字符串node2=行_spl[1];
Text node1_txt=新文本(node1);
Text node2_txt=新文本(node2);
文本输出=新文本(“输出”);
Text emit_in=新文本(“in”);
收集(node1_txt,emit_out);
收集(node2_txt,emit_in);
}//结束映射函数
}//结束映射器类
减速器:

public class YourReducer extends MapReduceBase implements Reducer<Text, Text, Text, Text> {

    public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException {

         int count_outs = 0;
         int count_ins  = 0;

            while (values.hasNext()) {

              Text value = (Text) values.next();

              String value_str = value.toString();

              if(value_str.equals("out"))
                 count_outs++;
              else
              if(value_str.equals("in"))
                 count_ins++;  

            }

            Text out = new Text(count_ins + " " + count_outs);              
            output.collect(key, out);

    }//end reduce function

}//end reducer class
公共类YourReducer扩展MapReduceBase实现Reducer{
公共void reduce(文本键、迭代器值、OutputCollector输出、Reporter报告器)引发IOException{
整数计数=0;
int count_ins=0;
while(values.hasNext()){
文本值=(文本)值。下一步();
字符串值_str=value.toString();
如果(值_str.等于(“out”))
计数++;
其他的
如果(值_str.equals(“in”))
count_ins++;
}
文本输出=新文本(计数输入+“”+计数输出);
输出。收集(键,输出);
}//端点归约函数
}//端部减速器类

为什么1的outdegree是2?嗨,对不起,这是我的错误,我应该感谢你的回答,但我无法打印3列,因为它只是一个键值对,你能建议最好的方法吗?是的,它是键值对)因此在mapper中,你可以通过连接像[node1,“node2_out”]和[node2,“node1_在”]中。然后在reducer中,使用“Iterable values”作为参数,解析每个值(即用分隔符“”分割以检查它是否为indegree或outdegree),然后进行计数。您可以提供一个虚拟示例说明如何进行此操作吗?@testAccount在reducer中,
上下文。write(key,value1+“”+value2)
…这将输出“三列”,当然,每个值中可能存在多个“列”variable@testAccount请参阅我更新的答案中的近似实现