代码如下, 后备参考:

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. 在Salesforce中处理Email的发送

    在Salesforce中可以用自带的 Messaging 的 sendEmail 方法去处理Email的发送 请看如下一段简单代码: public boolean TextFormat {get;se ...

  2. ios内购

    1.添加框架,storeKit.framework 需要真机调试 /* 内购五步: 1.请求可销售商品的列表 2.展示课销售的商品 3.点击购买 4.开具小票 5.创建交易对象并添加到交易队列 6.创 ...

  3. 玲珑杯1007-A 八进制大数加法(实现逻辑陷阱与题目套路)

    题目连接:http://www.ifrog.cc/acm/problem/1056 DESCRIPTION Two octal number integers a, b are given, and ...

  4. T-SQL 高级查询

    基本常用查询 --select select * from student; --all 查询所有 select all sex from student; --distinct 过滤重复 selec ...

  5. scrollview嵌套listview 滑动事件冲突的解决方法

    listView.setOnTouchListener(new View.OnTouchListener() {                            @Override       ...

  6. BootSrap学习

    1.当row1里边有2个或多个col的时候,如果位置在后边的col的高度大于位置在前边的col,就可能会导致下一个row2在满足col-xs-*在满足和为12的情况下错乱分行.解决办法是强行给row1 ...

  7. PHP如何实现文件上传

    PHP如何实现文件上传 1.表单部分  允许用户上传文件,在HTML表单的声明中要加上一个上传的属性:  enctype = 'multipart/form-data'  表单的method必须是PO ...

  8. HIT2739 The Chinese Postman Problem(最小费用最大流)

    题目大概说给一张有向图,要从0点出发返回0点且每条边至少都要走过一次,求走的最短路程. 经典的CPP问题,解法就是加边构造出欧拉回路,一个有向图存在欧拉回路的充分必要条件是基图连通且所有点入度等于出度 ...

  9. BZOJ4382 : [POI2015]Podział naszyjnika

    对于每种颜色,可以发现可以切的位置被分割成了若干段独立的区域. 给每个区域一个编号,将$m$种颜色的情况当成字符串来看,如果两个切口的字符串完全匹配,那么可以在这里切两刀. 可以构造hash函数,通过 ...

  10. Spring In Action ②

    初始化和销毁Bean init-method && destory-method <bean id="auditorium" class="test ...