Hadoop.2.x_WordCount本地测试示例
代码如下, 后备参考:
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本地测试示例的更多相关文章
- cdh5.7权限测试示例
转载请注明出处:http://www.cnblogs.com/xiaodf/ 本文旨在展示CDH基于Kerberos身份认证和基于Sentry的权限控制功能的测试示例. 1. 准备测试数据 cat / ...
- MapReduce两种执行环境介绍:本地测试环境,服务器环境
本地测试环境(windows):1.在windows下配置hadoop的环境变量2.拷贝debug工具(winutils.exe)到hadoop目录中的bin目录,注意winutils.exe的版本要 ...
- 利用Docker Compose快速搭建本地测试环境
前言 Compose是一个定义和运行多个Docker应用的工具,用一个YAML(dockder-compose.yml)文件就能配置我们的应用.然后用一个简单命令就能启动所有的服务.Compose编排 ...
- 用java开发微信公众号:测试公众号与本地测试环境搭建(一)
本文为原创,原始地址为:http://www.cnblogs.com/fengzheng/p/5023678.html 俗话说,工欲善其事,必先利其器.要做微信公众号开发,两样东西不可少,那就是要有一 ...
- 在本地测试一次成功的AJAX请求
要在本地测试AJAX,首先是环境的搭建,下面以wamp为例. 1.先在wamp的官网下载wamp的安装包,网址 http://www.wampserver.com/. 2.安装wamp.如果安装过程中 ...
- 【转】Oracle索引列NULL值引发执行计划该表的测试示例
有时开发进行表结构设计,对表字段是否为空过于随意,出现诸如id1=id2,如果允许字段为空,因为Oracle中空值并不等于空值,有可能得到意料之外的结果.除此之外,最关键的是,NULL会影响oracl ...
- 本地测试AJAX请求
要在本地测试AJAX,首先是环境的搭建,因为XHR对象的open方法中参数url是指文件在服务器上的文件.下面以WampServer为例. 1. 下载wamp的安装包,下载地址为:http://221 ...
- win10系统iis下部署搭建https (ssl/tls)本地测试环境
有时想要把公司的某些XX项目部署成https站点,是为了在传输层加密传输,防止他人嗅探站点重要数据信息,平常我们使用的http方式都是明文方式传输的很不安全,容易被他人窃取.而有些时候要在本地搭建ht ...
- win7 windows server 2008R2下 https SSL证书安装的搭配(搭配https ssl本地测试环境)
原文:http://www.cnblogs.com/naniannayue/archive/2012/11/19/2776948.html 要想成功架设SSL安全站点关键要具备以下几个条件. 1.需要 ...
随机推荐
- 一种快速刷新richedit中内嵌动画的方法的实现
在IM中使用动画表情是一种非常有趣的方式,然而选择一种合适的方式来实现却并不容易. 一般来说,除了自己去实现一个富文本控件,目前主要的解决方案有3种: 1.使用浏览器做容器. 2.使用QT提供的Ric ...
- IM即时通讯实现原理
即时通讯(Instant Messenger,简称IM)软件多是基于TCP/IP和UDP进行通讯的,TCP/IP和UDP都是建立在更低层的IP协议上的两种通讯传输协议.前 者是以数据流的形式,将传 ...
- Emacs 之窗口管理
// */ // ]]> Emacs 之窗口管理 Table of Contents 1. Emacs 窗口相关 1.1. Emacs 里调整 window 大小 1.2. Emacs winn ...
- BootCDN和npm
稳定.快速.免费的开源项目 CDN 服务 BootCDN: jQuery3 <script type="text/javascript" src="http://c ...
- string、math、random、datetime类
1.string类 变量.Replace("想要替换掉的字符或字符串","转换后的字符或字符串");//替换 练习:判断邮箱格式是否正确 ...
- SQL Server2008函数大全(完整版)
SQL2008 表达式:是常量.变量.列或函数等与运算符的任意组合. 1. 字符串函数 函数 名称 参数 示例 说明 ascii(字符串表达式) select ascii('abc') 返回 97 返 ...
- LoadRunner 脚本学习 -- 使用动态链接库
DLL = Dynamic Link Library DLL最重要的一个特点就扩展应用程序的特性. 再强大的工具也有不是万能的,通过调用动态库的方法极大地增强loadrunner的功能.当你在用loa ...
- SoapUI之webservice接口测试(一)
1.新建soap project 添加后出现接口内容 2.为了方便后续的测试,以防某些参数删除错了,这边需要新建测试集 3.点开新建的测试集可以发现,里面的内容跟原始测试集内容是一样的 然后就可以在这 ...
- Xamarin Android教程如何使用Xamarin开发Android应用
Xamarin Android教程如何使用Xamarin开发Android应用 在了解了Xamarin和Andriod系统之后,下面我们需要了解一下如何使用这些工具和系统来开发我们的应用程序. And ...
- linux 查看机器的cpu,操作系统等命令
看cpu信息,型号,几核 [root@f3 ~]# cat /proc/cpuinfo | grep name | cut -f2 -d:| uniq -c 16 Intel(R) Xeon(R) C ...