簡體   English   中英

Hadoop Map Reduce查詢

[英]Hadoop Map Reduce Query

我試圖使用HADOOP MadReduce計算圖中每個節點的所有傳入邊的權重之和。輸入采用.tsv格式,看起來像:

安全重量

X 102 1

X 200 1

X 123 5

Y 245 1

Y 101 1

Z 99 2

X 145 3

Y 24 1

A 21 5

預期的輸出是:

src SUM(重量)

X 10

Y 3

Z 2

A 5

我使用了hadoop( http://www.cloudera.com/content/cloudera/en/documentation/hadoop-tutorial/CDH5/Hadoop-Tutorial/ht_wordcount1_source.html?scroll=topic_5_1 )中的WordCount示例代碼作為參考。 我試圖操縱代碼,但所有的努力都以徒勞告終。

我對JAVA和HADOOP很陌生。 我已經分享了我的代碼。 請幫助我弄清楚代碼有什么問題。

謝謝。

碼:

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);
}
}

您必須稍微更改代碼。

    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));
    }

希望這可以幫助您

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM