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. 原始的JDBC操作

    -----------------------------根据配置文件---------------------------- package cn.gdpe.jdbc; import java.io ...

  2. python模块学习 logging

    1.简单的将日志打印到屏幕 import logging logging.debug('This is debug message') logging.info('This is info messa ...

  3. C语言中float,double类型,在内存中的结构(存储方式)

    C语言中float,double类型,在内存中的结构(存储方式)从存储结构和算法上来讲,double和float是一样的,不一样的地方仅仅是float是32位的,double是64位的,所以doubl ...

  4. C# C/S系统软件开发平台架构图(原创)

    企业版V4.0 - 架构图 企业版V4.0 - 桥接功能.后台连接策略 桥接功能是指应用策略模式,由用户配置本地INI文件选择ADO直连(ADO-Direct)或者调用WCF服务接口访问远程服务器后台 ...

  5. paramiko学习

    1. ssh简介 2. ssh私有key/共有key的区别 3. paramiko的常规使用

  6. 浅谈Exchange 2013开发-如何操作邮件的附件

    因为项目中客户有一个的要求,所以这个Exchange前段时间搞的我很是头疼,没接触过这个东西,但是现在看来,纸老虎一个.希望我的经验可以帮助初次接触它的人少走一些弯路! 简单介绍一下:客户要求在自己的 ...

  7. Swift(一,创建对象,类型推导,基本运算,逻辑,字符串,数组,字典)

    swift用起来刚开始感觉有点怪怪的,但用了一段时间觉得还是挺好用的,哈哈.毕竟都是要有一个过程的嘛. 我就写一些自己在使用swift的时候的注意点吧,如有不正之处,还请指正! 一.在开发中优先使用常 ...

  8. Linux脚本(二)

    1.for循环以及加法的使用 portStr=`lsof -i:56801 | head -2`count=0for str in `lsof -i:56801 | head -2`do ((coun ...

  9. 邮件协议POP3/IMAP/SMTP服务的区别

    2016年09月09日 09時51分 wanglinqiang整理 通过网上查找的资料和自己的总结完成了下面的文章,看完之后相信大家对这三种协议会有更深入的理解.如有错误的地方望指正. POP3 PO ...

  10. Nodejs 集成到IIS

    http://www.hanselman.com/blog/WebMatrixAndNodejsTheEasiestWayToGetStartedWithNodeOnWindows.aspx http ...