代码如下, 后备参考:

package com.bigdata.hadoop.hdfs;

import java.io.IOException;

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.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 WordCountTest { //step 1 Mapper Class
public static class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{ private Text mapOutPutKey = new Text();
private final static IntWritable mapOutPutValue = new IntWritable(1);
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
//get lines value
String lineValue = value.toString();
String[] strs = lineValue.split(" ");
for(String str : strs){
mapOutPutKey.set(str);
context.write(mapOutPutKey, mapOutPutValue);
}
}
} //step 2 Reducer Class
public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{ private IntWritable outPutVlaue = new IntWritable();
@Override
public void reduce(Text key, Iterable<IntWritable> values,Context context)
throws IOException, InterruptedException { //temp : sum
int sum = 0;
for(IntWritable value : values){
sum += value.get();
}
outPutVlaue.set(sum);
context.write(key, outPutVlaue);
}
} //step 3 Driver
public int run(String[] args) throws Exception, InterruptedException{ //get configuration
Configuration configuration = new Configuration();
//get a job
Job job = Job.getInstance(configuration,this.getClass().getName());
job.setJarByClass(getClass());
//get a input path
Path inPath = new Path(args[0]);
FileInputFormat.addInputPath(job, inPath);
//get a output path
Path outPath = new Path(args[1]);
FileOutputFormat.setOutputPath(job, outPath); //Mapper
job.setMapperClass(WordCountMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); //Reducer
job.setReducerClass(WordCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); //submit job
boolean isSUccess = job.waitForCompletion(true); return isSUccess ? 0 : 1;
} public static void main(String[] args) throws Exception { args = new String[]{
"hdfs://linux-66-64.liuwl.com:8020/user/liuwl/tmp/input",
"hdfs://linux-66-64.liuwl.com:8020/user/liuwl/tmp/output"
};
int status = new WordCountTest().run(args); System.exit(status);
}
}

  

Hadoop.2.x_WordCount本地测试示例的更多相关文章

  1. cdh5.7权限测试示例

    转载请注明出处:http://www.cnblogs.com/xiaodf/ 本文旨在展示CDH基于Kerberos身份认证和基于Sentry的权限控制功能的测试示例. 1. 准备测试数据 cat / ...

  2. MapReduce两种执行环境介绍:本地测试环境,服务器环境

    本地测试环境(windows):1.在windows下配置hadoop的环境变量2.拷贝debug工具(winutils.exe)到hadoop目录中的bin目录,注意winutils.exe的版本要 ...

  3. 利用Docker Compose快速搭建本地测试环境

    前言 Compose是一个定义和运行多个Docker应用的工具,用一个YAML(dockder-compose.yml)文件就能配置我们的应用.然后用一个简单命令就能启动所有的服务.Compose编排 ...

  4. 用java开发微信公众号:测试公众号与本地测试环境搭建(一)

    本文为原创,原始地址为:http://www.cnblogs.com/fengzheng/p/5023678.html 俗话说,工欲善其事,必先利其器.要做微信公众号开发,两样东西不可少,那就是要有一 ...

  5. 在本地测试一次成功的AJAX请求

    要在本地测试AJAX,首先是环境的搭建,下面以wamp为例. 1.先在wamp的官网下载wamp的安装包,网址 http://www.wampserver.com/. 2.安装wamp.如果安装过程中 ...

  6. 【转】Oracle索引列NULL值引发执行计划该表的测试示例

    有时开发进行表结构设计,对表字段是否为空过于随意,出现诸如id1=id2,如果允许字段为空,因为Oracle中空值并不等于空值,有可能得到意料之外的结果.除此之外,最关键的是,NULL会影响oracl ...

  7. 本地测试AJAX请求

    要在本地测试AJAX,首先是环境的搭建,因为XHR对象的open方法中参数url是指文件在服务器上的文件.下面以WampServer为例. 1. 下载wamp的安装包,下载地址为:http://221 ...

  8. win10系统iis下部署搭建https (ssl/tls)本地测试环境

    有时想要把公司的某些XX项目部署成https站点,是为了在传输层加密传输,防止他人嗅探站点重要数据信息,平常我们使用的http方式都是明文方式传输的很不安全,容易被他人窃取.而有些时候要在本地搭建ht ...

  9. win7 windows server 2008R2下 https SSL证书安装的搭配(搭配https ssl本地测试环境)

    原文:http://www.cnblogs.com/naniannayue/archive/2012/11/19/2776948.html 要想成功架设SSL安全站点关键要具备以下几个条件. 1.需要 ...

随机推荐

  1. win7-32 系统 + VS2010 配置 glew

    网上下载的程序,运行时报错:  C1083: 无法打开包括文件:“gl\glew.h”: No such file or directory. 百度一下,发现需要配置 glew 库. 方法如下: 下载 ...

  2. Microshaoft WinDbg cmdtree

    windbg ANSI Command Tree 1.0 title {"Microshaoft Commands"} body {"cmdtree"} {&q ...

  3. 用c语言写一个函数把十进制转换成十六进制(转)

    #include "stdio.h" int main() { int num=0;int a[100]; int i=0; int m=0;int yushu; char hex ...

  4. LoadRunner中截取字符串

    LoadRunner中截取字符串 /*strchr和strrchr的区别*/ char *strTest1="citms citms"; char *strTest2,*strTe ...

  5. MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践

    主服务器上(注:应该是允许从机访问)  GRANT REPLICATION SLAVE ON *.* to ‘rep1’@’192.168.10.131’ identified by ‘passwor ...

  6. 记一次小团队Git实践(中)

    对于初学者,从使用上先入手,往往学的最快,并从中汲取教训,再回头更深入的学习,效果尤佳. 安装git 安装git自不必说,mac已经内置了git,linux下一个命令就能搞定,windows下需要下载 ...

  7. mac OS X操作 -- 常用

    显示/隐藏默认隐藏文件:defaults write com.apple.finder AppleShowAllFiles -bool true/false 重置ROOT密码: http://www. ...

  8. iOS UITextField的returnkey点击事件

      关于隐藏软键盘,网上的办法良莠不齐,大多是通过实现UITextFieldDelegate来隐藏软键盘,该方法代码较多,且在文本框很多的时不好处理.我经过搜索与摸索,找到了最佳的处理办法.(引用的) ...

  9. 疯狂java学习笔记之面向对象(四) - this关键字

    Java中this关键字主要有以下两个方法: 1.this引用 - 可用于任何非static修饰的方法和构造器中,当this用于方法中时,它代表调用该方法的实例/对象;当this用于构造器中时,它代表 ...

  10. JavaScript实现li隔行变色

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...