Changes between Version 1 and Version 2 of waue/2010/0115-HelloHadoopV2


Ignore:
Timestamp:
Jan 20, 2010, 9:27:21 PM (14 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2010/0115-HelloHadoopV2

    v1 v2  
    11 = Hello Hadoop V2  =
    22
     3
     4 * hellohadoop v2 main
    35{{{
    46#!java
     
    7274
    7375}}}
     76
     77 * mapper
     78
     79{{{
     80#!java
     81import java.io.IOException;
     82import org.apache.hadoop.io.LongWritable;
     83import org.apache.hadoop.io.Text;
     84import org.apache.hadoop.mapreduce.Mapper;
     85
     86public class HelloMapperV2 extends Mapper<LongWritable, Text, Text, Text> {
     87
     88        public void map(LongWritable key, Text value, Context context)
     89                        throws IOException, InterruptedException {
     90                context.write(new Text(key.toString()), value);
     91        }
     92
     93}
     94
     95}}}
     96
     97 * reducer
     98
     99{{{
     100#!java
     101import java.io.IOException;
     102import org.apache.hadoop.io.Text;
     103import org.apache.hadoop.mapreduce.Reducer;
     104
     105public class HelloReducerV2 extends Reducer<Text, Text, Text, Text> {
     106        public void reduce(Text key, Iterable<Text> values, Context context)
     107                        throws IOException, InterruptedException {
     108
     109                String str = new String("");
     110                Text final_key = new Text();
     111                Text final_value = new Text();
     112                // 將key值相同的values,透過 && 符號分隔之
     113                for (Text tmp : values) {
     114                        str += tmp.toString() + " &&";
     115                }
     116
     117                final_key.set(key);
     118                final_value.set(str);
     119
     120                context.write(final_key, final_value);
     121        }
     122}
     123}}}