2013-12-10 49 views
0

這是我第一次使用Hadoop進行編程,我在hadoop教程網站上根據WordCount v1.0進行了我的任務。hadoop key返回多個值

作業:你有兩個文件。 File0包含字典中的每個單詞。 File1包含一個隨機單詞。例如:'美麗'。

當我運行它應該在File0返回的每一個字的程序,其大小爲文件1字相同

*美將在字典

例如返回每9個字母組成的單詞: 美麗 - AARDVARKS AASVOGELS ABAMPERES .... ZYMOGRAMS ZYMURGIES

所以我的問題是我應該怎麼做呢? hadoop wordcount v1.0返回鍵和單個值。 ----例如(beautiful 4)

是否需要將值從int更改爲字符串或者某種數組包含與密鑰大小相同的每個單詞?

*基本上,我需要的格式從

(美4)

改變

(美麗:土豚AASVOGELS ABAMPERES ... ZYMOGENES酶譜ZYMURGIES)

這裏是代碼(來源於他們的網站):

package org.myorg; 

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 WordCount { 

    public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> { 
    private final static IntWritable one = new IntWritable(1); 
    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); 
     while (tokenizer.hasMoreTokens()) { 
     word.set(tokenizer.nextToken()); 
     output.collect(word, one); 
     } 
    } 
    } 

    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(WordCount.class); 
    conf.setJobName("wordcount"); 

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

我需要改變地圖,減少還是兩者?如何??

有人可以請幫忙!非常感謝

回答

0

在reducer中,不要在循環中進行求和,而應使用StringBuilder並將與隨機單詞長度匹配的單詞串附加在一起。您可能還必須將您的主方法中的OutputValueClass從IntWritable更改爲Text。你還需要改變你的減速來實現:

Reducer<Text, IntWritable, Text, Text> 
映射器中,而不是寫一個作爲輸出值

還寫了一個字的長度。

快樂編碼