-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHelloWordCount.java
88 lines (74 loc) · 3.18 KB
/
HelloWordCount.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/// A Test Program for word count.
/// 2020-1-18 by [email protected]
///
/// need add external libs:
/// /usr/local/hadoop/share/hadoop/common/hadoop-common-2.7.6.jar
/// /usr/local/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.7.6.jar
/// /usr/local/hadoop/share/hadoop/common/lib/commons-logging-1.1.3.jar
/// /usr/local/hadoop/share/hadoop/common/lib/guava-11.0.2.jar
/// /usr/local/hadoop/share/hadoop/common/lib/commons-collections-3.2.2.jar
/// /usr/local/hadoop/share/hadoop/common/lib/log4j-1.2.17.jar
/// /usr/local/hadoop/share/hadoop/common/lib/commons-io.2.4.jar
///
/// run:
/// 1. >start-dfs.sh
/// 2. >hadoop fs -put ~/testfile.txt /testfile.txt
/// 3. use idea build the jar.
/// 4. >hadoop HelloWordCount.jar /some/input/file /hdfs/output/dir
/// 5. >hadoop fs -cat /hdfs/output/dir/part-00000
///
package com.company;
import java.io.*;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.* ;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
/// this wordcount2.0 use Hadoop2.x API.
/// check the tutorial at hadooop.apache.org/docs/r2.7.6/
public class Main {
public static class Map extends Mapper<LongWritable,Text,Text,IntWritable>{
private final static IntWritable one = new IntWritable(1) ;
private final Text word = new Text() ;
public void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException {
String line = value.toString() ;
StringTokenizer tok = new StringTokenizer(line) ;
while( tok.hasMoreTokens() )
{
word.set(tok.nextToken()) ;
context.write( word , one);
}
}
}
public static class Reduce extends Reducer<Text,IntWritable,Text,IntWritable>{
public void reduce(Text key , Iterator<IntWritable> values ,
Context context) throws IOException, InterruptedException {
int sum = 0 ;
while( values.hasNext())
{
sum += values.next().get() ;
}
context.write(key , new IntWritable(sum));
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
// write your code here
Job job = Job.getInstance(new Configuration() , "Hello Word Count") ;
job.setJarByClass(Main.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job , new Path(args[0]));
FileOutputFormat.setOutputPath(job , new Path(args[1]));
job.waitForCompletion(true) ;
}
}