hadoop的统计单词程序WordCount
来源:互联网 发布:阿里云域名管理登录 编辑:程序博客网 时间:2024/06/05 07:58
hadoop 是用 java 的实现的一个分布式系统框架,最主要的两个部分可能就是 MapReduce 和 HDFS 了,前者是个编程模型,后者是存储模型。算了,不多说了,拣重点。WordCount 是 MapReduce 的经典程序, Hadoop 官网有这个程序,现在 me 们就是要跑通这个程序。当然首先的第一步就是安装 Hadoop,这一点,本篇不多做介绍。其次,就是按照指定的步骤去执行就好,不过中途会出一些问题,这是本篇要介绍的重点。
假定 hadoop 已经安装了,of course JAVA 也安装了,然后按照下面的执行,应该就可以跑通程序了。
- $ vim WordCount.java : 编辑 WordCount.java 文件,文件内容可以看后面的代码;
- $ vim input.txt : 编辑输入文件 input.txt,内容随便了,现在程序就是要统计单词数;
- $ mkdir class : 创建一个放 class 文件的目录;
- $ javac -classpath /opt/hadoop/hadoop/hadoop-core-1.2.1.jar -d class WordCount.java : 编译 WordCount 源文件,class 文件放在 class 目录下;
- $ jar -cvf wordcount.jar -C class . : 将程序打包;
- $ hadoop jar wordcount.jar test.WordCount file:///home/hadoop/input.txt /tmp/output : 运行程序,file:/// 可以指定本地的文件作为输入文件, /tmp/output 是 HDFS 路径,存放输出结果;
- $ hadoop fs -cat /tmp/output/part-r-00000 : 查看程序的结果;
如果是第一次按照上面的步骤执行,应该没有神马太大问题,不过不是第一次,或是程序 me 们有改动的话,上面的执行可能就要稍微变一下了,下面记录三个可能出现的问题,以及解决方案:
- Exception in thread "main" java.lang.ClassNotFoundException: WordCount : 执行 WordCount 程序,也就是上面的第 6 步,需要添加 test 包名限制;
- java.lang.ClassNotFoundException: test.WordCount$Map : 执行 WordCount 程序,抛出找不到 WordCount$Map 的异常,实际上该类和 WordCount.class 是在同一个文件夹下丫;官网的 hadoop 程序没有上面代码的有注释的那一行,就会抛出这个异常,加上去就可以了;
- Output path already exists : Output director : 这个易理解,就是输出文件夹 /tmp/output 已经存在,可以指定一个其他的,或是删除该文件夹 $ hadoop fs -rmr /tmp/output;
程序代码
- package test;
- 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.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;
- 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.hasMoreTokens()) {
- 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.setJarByClass(WordCount.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);
- }
- }
- hadoop的统计单词程序WordCount
- storm程序-单词统计wordcount
- Hadoop Demo 1 ——WordCount 统计文章中单词的个数
- 和我一起学Hadoop(五):MapReduce的单词统计,wordcount
- hadoop基础----hadoop实战(三)-----hadoop运行MapReduce---对单词进行统计--经典的自带例子wordcount
- Hadoop入门实例——WordCount统计单词
- Hadoop编程入门,统计单词出现数目wordcount
- Hadoop2.2.0下通过编译单词统计例子WordCount.java熟悉如何编译自己的程序
- 改写Hadoop的wordcount程序
- 入门Hadoop的WordCount程序
- 云计算学习笔记006---运行hadoop的例子程序:统计字符--wordcount例子程序
- Hadoop最基本的wordcount(统计词频)
- Hadoop示例程序之单词统计MapReduce
- Hadoop示例程序之单词统计MapReduce
- Hadoop示例程序之单词统计MapReduce
- Hadoop示例程序之单词统计MapReduce
- 启动Spark Shell,在Spark Shell中编写WordCount程序,在IDEA中编写WordCount的Maven程序,spark-submit使用spark的jar来做单词统计
- Hadoop自带字数统计程序wordcount异常
- C++头文件的包含顺序
- 如何在csdn贴代码
- java文件和文件夹复制、删除、移动操作
- ARM获取动态IP
- SAP Afaria短信网关的选择
- hadoop的统计单词程序WordCount
- 【leetcode】Pow(x, n)
- [Google] RMQ 区间最值问题
- HTTP代理实现请求报文的拦截与篡改2--功能介绍+源码下载
- 与IO相关的等待事件troubleshooting-系列8
- php study 4 array
- 编写App的开场Activity 和 扩展ImageView使可旋转
- oracle ebs Customer Info
- 【js学习笔记-054】标准转换方法