package com.uniclick.dapa.dstest;

import java.io.IOException;
import java.net.URI; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WordCount {
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
String inputFilePath = "/user/zhouyuanlong/wordcount/input/wordTest*.txt";
String outputFilePath = "/user/zhouyuanlong/wordcount/output/";
String queue = "default";
String jobName = "wordCount";
if(args == null || args.length < 2){
System.out.println("[-INPUT <inputFilePath>"
+ "[-OUTPUT <outputFilePath>");
}else{
for(int i=0;i<args.length;i++){
if("-Q".equals(args[i])){
queue = args[++i];
}
}
}
Configuration conf = new Configuration();
conf.set("mapred.job.queue.name", queue);
Job job = new Job(conf, jobName);
job.setJarByClass(WordCount.class);
job.setMapperClass(WordCountMapper.class);
// job.setCombinerClass(cls);
job.setReducerClass(WordCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(inputFilePath));
Path path = new Path(outputFilePath);
FileSystem fs = FileSystem.get(URI.create(outputFilePath), conf);
if(fs.exists(path)){
// fs.delete(path);
fs.delete(path, true);
}
FileOutputFormat.setOutputPath(job, new Path(outputFilePath));
System.exit(job.waitForCompletion(true) ? 1 : 0);
} public static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
private Text kt = new Text();
private final static IntWritable vt = new IntWritable(1); public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String[] arr = value.toString().split("\t");
for(int i = 0; i < arr.length; i++){
kt.set(arr[i]);
context.write(kt, vt);
}
}
} public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
private IntWritable vt = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException{
int sum = 0;
for(IntWritable intVal : values){
sum += intVal.get();
}
vt.set(sum);
context.write(key, vt);
}
} }

input目录中文件wordTest1.txt的内容(每行以table键分隔):

hello    world
hello    hadoop
hello    mapredruce

input目录中文件wordTest2.txt的内容(每行以table键分隔):

hello    world
hello    hadoop
hello    mapredruce

hdfs输出结果:

web     2
mapredruce      1
python  1
hadoop  1
hello   6
clojure 2
world   1
java    2

PS:对Hadoop自带的wordcount的例子略有改变

Hadoop最基本的wordcount(统计词频)的更多相关文章

  1. Hadoop上的中文分词与词频统计实践 (有待学习 http://www.cnblogs.com/jiejue/archive/2012/12/16/2820788.html)

    解决问题的方案 Hadoop上的中文分词与词频统计实践 首先来推荐相关材料:http://xiaoxia.org/2011/12/18/map-reduce-program-of-rmm-word-c ...

  2. Hadoop入门实例——WordCount统计单词

    首先要说明的是运行Hadoop需要jdk1.6或以上版本,如果你还没有搭建好Hadoop集群,请参考我的另一篇文章: Linux环境搭建Hadoop伪分布模式 马上进入正题. 1.启动Hadoop集群 ...

  3. 使用SparkSQL编写wordCount的词频统计

    # 使用SparkSQL编写wordCount的词频统计 ## word.txt```hello hello scala sparkjava sql html java hellojack jack ...

  4. Hadoop实战3:MapReduce编程-WordCount统计单词个数-eclipse-java-ubuntu环境

    之前习惯用hadoop streaming环境编写python程序,下面总结编辑java的eclipse环境配置总结,及一个WordCount例子运行. 一 下载eclipse安装包及hadoop插件 ...

  5. Hadoop版Helloworld之wordcount运行示例

    1.编写一个统计单词数量的java程序,并命名为wordcount.java,代码如下: import java.io.IOException; import java.util.StringToke ...

  6. 执行hadoop自带的WordCount实例

    hadoop 自带的WordCount实例可以统计一批文本文件中各单词出现的次数.下面介绍如何执行WordCount实例. 1.启动hadoop [root@hadoop ~]# start-all. ...

  7. Excel中COUNTIFS函数统计词频个数出现次数

    Excel中COUNTIFS函数统计词频个数出现次数   在Excel中经常需要实现如下需求:在某一列单元格中有不同的词语,有些词语相同,有的不同(如图1所示).需要统计Excel表格中每个词语出现的 ...

  8. hadoop自带例子wordcount的具体运行步骤

    1.在hadoop所在目录“usr/local”下创建一个文件夹input root@ubuntu:/usr/local# mkdir input 2.在文件夹input中创建两个文本文件file1. ...

  9. HADOOP :: java.lang.ClassNotFoundException: WordCount

    I am using eclipse to export the jar file of a map-reduce program. When i am run the jar using comma ...

随机推荐

  1. 滑动开关效果 css3滑动开关】纯CSS3代码实现滑动开关效果-css3滑动效果-css3左右滑动

    今天看到一篇有关 css3事件的博文,一时兴起便整理下相关的资料. 点击按钮,可以实现开关的滑动效果. 今天看到一篇有关 css3事件的博文,一时兴起便整理下相关的资料. 点击按钮,可以实现开关的滑动 ...

  2. WF学习笔记(四)

    1.使用WorkflowApplication.OnUnhandledException 处理Code异常 ,用于产生异常的Activity: public class ExceptionActivi ...

  3. Jquery 实现瀑布流功能

    实现展示地址:http://sandbox.runjs.cn/show/mbojrgag 源码地址:http://runjs.cn/code/qps1jebl 效果截图:

  4. ES6-个人学习笔记二--解构赋值

    第二期,解构赋值如果能够熟练应用确实是个十分方便的功能,但是过分的依赖和嵌套只会让代码理解和维护起来十分困难,是个体现高逼格的表达式呢~ 1,解构赋值的基础 //定义:es6运行按照一定模式,从数组或 ...

  5. centos7 systemctl grub2

    centos最小好化安装没有ifconfig命令 刚安装了centos7.0,最小化安装,发现没有ifconfig命令,虚拟机里面的网卡显示ens32,这是centos7.0的特点,要使用 ifcon ...

  6. 常用webservice接口案例

    商业和贸易: 1.股票行情数据 WEB 服务(支持香港.深圳.上海基金.债券和股票:支持多股票同时查询) Endpoint: http://webservice.webxml.com.cn/WebSe ...

  7. Uva_11462 GCD - Extreme (II)

    题目链接 题意: 给定一个n, 求:GCD(1, 2) + GCD(1, 3) + GCD(2, 3) + …… + GCD(1, n) + GCD(2, n) + …… + GCD(n-1, n); ...

  8. The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files

    最近在做J2ME开发项目,配置环境一切OK,但是打开项目时某些文件提示: The type java.lang.String cannot be resolved. It is indirectly ...

  9. 【POJ1021】Intervals (最短路解差分约束)

    题目: Sample Input 5 3 7 3 8 10 3 6 8 1 1 3 1 10 11 1 Sample Output 6 题意: 我们选数,每个数只能选一次.给定n个条件[ai,bi]和 ...

  10. java webservice服务器端获取request对象的三种方式

    有的时候在webservice里我们需要获取request对象和response对象,比如想要获得客户端的访问ip的时候就需要这么做,下面说三种方式,当然三种方式可能是针对不同方式部署webservi ...