简体   繁体   English

Hadoop Map Reduce查询

[英]Hadoop Map Reduce Query

I was trying to use HADOOP MadReduce to calculate the sum of the weights of all incoming edges for each node in a graph.The input is in a .tsv format and it looks like: 我试图使用HADOOP MadReduce计算图中每个节点的所有传入边的权重之和。输入采用.tsv格式,看起来像:

src tgt weight 安全重量

X 102 1 X 102 1

X 200 1 X 200 1

X 123 5 X 123 5

Y 245 1 Y 245 1

Y 101 1 Y 101 1

Z 99 2 Z 99 2

X 145 3 X 145 3

Y 24 1 Y 24 1

A 21 5 A 21 5

. . .

. . .

The expected output is: 预期的输出是:

src SUM(weight) src SUM(重量)

X 10 X 10

Y 3 Y 3

Z 2 Z 2

A 5 A 5

. .

. .

I've used an example code of WordCount from hadoop ( http://www.cloudera.com/content/cloudera/en/documentation/hadoop-tutorial/CDH5/Hadoop-Tutorial/ht_wordcount1_source.html?scroll=topic_5_1 ) as the reference. 我使用了hadoop( http://www.cloudera.com/content/cloudera/en/documentation/hadoop-tutorial/CDH5/Hadoop-Tutorial/ht_wordcount1_source.html?scroll=topic_5_1 )中的WordCount示例代码作为参考。 I tried manipulating the code, but all my efforts ended up in vain. 我试图操纵代码,但所有的努力都以徒劳告终。

I am pretty new to JAVA and HADOOP. 我对JAVA和HADOOP很陌生。 I have shared my code. 我已经分享了我的代码。 Kindly help me figure out what is wrong with the code. 请帮助我弄清楚代码有什么问题。

Thanks. 谢谢。

Code: 码:

import java.io.IOException;
import java.util.*;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;

public class Task1 {

public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable value_parsed = new IntWritable();
private Text word = new Text();

public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
Text keys = new Text();
int sum;
while(tokenizer.hasMoreTokens())
{
    tokenizer.nextToken();
    keys.set(tokenizer.nextToken());
    sum = Integer.parseInt(tokenizer.nextToken());
    output.collect(keys, new IntWritable(sum));
}
}
}

public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException    {
    int sum = 0;
    while (values.hasNext()) {
    sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}

public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(Task1.class);
conf.setJobName("Task1");

conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);

conf.setMapperClass(Map.class);
conf.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class);

conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);

FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));

JobClient.runJob(conf);
}
}

You have to change your code little bit. 您必须稍微更改代码。

    while (tokenizer.hasMoreTokens()) {
        tokenizer.nextToken(); // this value is of first column
        keys.set(tokenizer.nextToken()); // this is wrong --you have to set
                                            // first column as key not
                                            // second column
        sum = Integer.parseInt(tokenizer.nextToken()); //  here 
                                                        // third column
        output.collect(keys, new IntWritable(sum));
    }

Hope this could help you 希望这可以帮助您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM