-
写程序MyWordCount.java
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 void main(String[] args) throws Exception { JobConf conf = new JobConf(WordCount.class); conf.setJobName("wordcount"); //conf.setNumReduceTasks(0); 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); } 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)); } } }
源文档 <http://hadoop.apache.org/docs/r0.18.3/mapred_tutorial.html>注:conf.setInputFormat(TextInputFormat.class); //TextInputFormat是默认的InputFormat。这说明Map类的键是LongWritable类型,存储整个文件的字节偏移量,值是Text类型,是一行内容。StringTokenizer 把行按空格拆分成单词。
conf.setOutputFormat(TextOutputFormat.class); //输出格式为TextOutputFormat,把输出记录写成文本行。键值可以使任何类型,因为可以用toString()方法转成字符串。这里的输出键是Text类型,值是IntWritable类型。
2. 编译
Mkdir wordcountsource
hduser@ubuntu:/usr/local/hadoop$ javac -classpath hadoop-core-1.1.1.jar -d wordcountsource MyWordCount.java
编译java到wordcountsource文件夹下
如果出现错误:error while writing Map: could not create parent directories
说明没有写入input文件的权限
3.生成jar
hduser@ubuntu:/usr/local/hadoop$ sudo jar -cvf MyWordCount.jar -C wordcountsource/ .
在当前目录下生成MyWordCount.jar
4.在input文件夹下创建file0和file1
hduser@ubuntu:/usr/local/hadoop/input$ mkdir input //input文件夹为输入
hduser@ubuntu:/usr/local/hadoop/input$ sudo gedit file0
hduser@ubuntu:/usr/local/hadoop/input$ sudo gedit file1
5.运行MyWordCount.jar
hduser@ubuntu:/usr/local/hadoop$ sudo bin/hadoop jar MyWordCount.jar org.myorg.MyWordCount input output
确保有创建output的权限
6.查看结果
hduser@ubuntu:/usr/local/hadoop$ cat output/part-00000
Bye 1
Hello 1
World 2
转载
相关推荐
Hadoop 用mapreduce实现Wordcount实例,绝对能用
mapreduce的Wordcount案例将main生成jar可直接在hdfs上运行
4 分别在自编 MapReduce 程序 WordCount 运行过程中和运行结束后查看 MapReduce Web 界面。 5. 分别在自编 MapReduce 程序 WordCount 运行过程中和运行结束后练习 MapReduce Shell 常用命令。 。。
使用python实现MapReduce的wordcount实例
MapReduce的wordcount的jar包
Hadoop搭建 MapReduce之Wordcount代码实现 代码讲解,通俗易懂。
云计算与大数据 MapReduce实验 Wordcount实验中所需数据包 WordCount.jar 不需要封装,centos7 linux hadoop实验上传所需
03.mapreduce实现wordcount的逻辑代码.mp4
wordcount, mapreduce经典,文字计数
Python实现MapReduce的WordCount(hadoop平台)在进行大数据处理时,JAVA程序用的较多,但是,想要把深度学习算法用到MapReduce中,Python是深度学习和数据挖掘处理数据较为容易的语言,所以基于以上考虑,本文介绍...
hadoop 框架下 mapreduce源码例子 wordcount ,eclipse下,hadoop 2.2 可以运行
Hadoop初学MapReduce最经典的范例
Ubuntu环境下新手学习Hadoop,从配置Hadoop开始,实现mapreduce,过程详细,也是自己学习过程的一个记录。
使用Java实现Hadoop的基础WordCount案例,附带本地提交和远程调用源代码参考。
#资源达人分享计划#
<groupId>com.hadoop.mapreduce</groupId> <artifactId>wordcount <version>0.0.1-SNAPSHOT <packaging>jar <name>wordcount <url>http://maven.apache.org</url> <project.build.sourceEncoding>UTF-8 ...
代码实现了 MapReduce的 WordCount,演示了MapReduce的基本编程模型,并且pom.xml也已经配置好,eclispse和IDEA皆可使用,JDK版本1.7即可。
MapReduce 字数 Go 和 Cassandra DB (Java) 中的简单 MapReduce 字数实现
介绍如何在Intellij Idea中通过创建maven工程配置MapReduce的编程环境,WordCount代码。
15、MapReduce介绍及wordcount 网址:https://blog.csdn.net/chenwewi520feng/article/details/130431900 本文主要介绍mapreduce的编程模型及wordcount实现、运行环境介绍。 前提依赖:hadoop环境可用,且本地的编码...