理解Hadoop源码 --- WordCount
2017-10-03 15:44
246 查看
Gradle:
group 'yqg'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
// https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common compile group: 'org.apache.hadoop', name: 'hadoop-common', version: '2.8.1'
// https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-core compile group: 'org.apache.hadoop', name: 'hadoop-core', version: '2.6.0-mr1-cdh5.12.1', ext: 'pom'
// https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs compile group: 'org.apache.hadoop', name: 'hadoop-hdfs', version: '2.8.1'
// https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-mapreduce-client-core compile group: 'org.apache.hadoop', name: 'hadoop-mapreduce-client-core', version: '2.8.1'
// https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-yarn-api compile group: 'org.apache.hadoop', name: 'hadoop-yarn-api', version: '2.8.1'
// https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-mapreduce-client-jobclient provided group: 'org.apache.hadoop', name: 'hadoop-mapreduce-client-jobclient', version: '2.8.1'
compile group: 'org.apache.hadoop', name: 'hadoop-mapreduce', version: '2.8.1', ext: 'pom'
}
group 'yqg'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
// https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common compile group: 'org.apache.hadoop', name: 'hadoop-common', version: '2.8.1'
// https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-core compile group: 'org.apache.hadoop', name: 'hadoop-core', version: '2.6.0-mr1-cdh5.12.1', ext: 'pom'
// https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs compile group: 'org.apache.hadoop', name: 'hadoop-hdfs', version: '2.8.1'
// https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-mapreduce-client-core compile group: 'org.apache.hadoop', name: 'hadoop-mapreduce-client-core', version: '2.8.1'
// https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-yarn-api compile group: 'org.apache.hadoop', name: 'hadoop-yarn-api', version: '2.8.1'
// https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-mapreduce-client-jobclient provided group: 'org.apache.hadoop', name: 'hadoop-mapreduce-client-jobclient', version: '2.8.1'
compile group: 'org.apache.hadoop', name: 'hadoop-mapreduce', version: '2.8.1', ext: 'pom'
}
package wordcount; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.*; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; import java.io.IOException; import java.util.StringTokenizer; /** * @author Ryan */ public class WordCount { public static class Map extends 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, Context context) throws IOException, InterruptedException{ String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreElements()){ word.set(tokenizer.nextToken()); context.write(word, one); } } } public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable>{ public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException{ int sum = 0; for (IntWritable val : values){ sum += val.get(); } context.write(key, new IntWritable(sum)); } } public static void main(String[] args) throws Exception{ Configuration conf = new Configuration(); Job job = new Job(conf, "wordcount"); 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); } }
相关文章推荐
- Hadoop之wordcount源码分析和MapReduce流程分析
- [hadoop源码阅读][9]-mapreduce-从wordcount开始
- Hadoop WordCount源码解读
- Hadoop 2.6 以WordCount为例理解Map Reduce
- hadoop之WordCount源码分析
- hadoop 实战———WordCount源码分析
- hadoop之wordCount程序理解
- Hadoop中wordcount源码分析
- Hadoop示例程序WordCount源码学习
- win7(64位)平台下Cygwin+Eclipse搭建Hadoop单机开发环境 (四) 导入Hadoop源码+wordcount程序+运行
- HADOOP中WORDCOUNT源码分析
- Eclipse 执行成功的 Hadoop-1.2.1 WordCount 源码
- Hadoop学习(二)wordcount源码详解
- Hadoop2.2.0源码分析(一)——Eclipse运行WordCount.java
- Hadoop学习笔记-WordCount源码分析
- Hadoop MapReduce WordCount v2.0结合个人理解进行注释
- Hadoop的Wordcount
- 从源码剖析一个Spark WordCount Job执行的全过程
- SparkStreaming的WordCount示例及源码分析(一)
- hadoop2.7.3 编译运行WordCount.java