一些例子,所用版本为hadoop 2.6.5

1、统计字数

数据格式如下(单词,频数,以tab分开):

A    100
B 97
C 98
A 98
 package com.mr.test;

 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.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 MRTest { public static class C01Mapper extends Mapper<Object, Text, Text, IntWritable> { @Override
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String[] line = value.toString().split("\t");
if(line.length == 2) {
context.write(new Text(line[0]),new IntWritable(Integer.parseInt(line[1])));
}
}
} public static class C01Reducer extends Reducer<Text, IntWritable, Text, IntWritable> { @Override
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int i =0;
for(IntWritable value : values){
i += value.get();
}
context.write(key, new IntWritable(i));
}
} public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//参数含义: agrs[0]标识 in, agrs[1]标识 out,agrs[2]标识 unitmb,agrs[3]标识 reducer number, int unitmb =Integer.valueOf(args[2]);
String in = args[0];
String out = args[1];
int nreducer = Integer.valueOf(args[3]); Configuration conf = new Configuration();
conf.set("mapreduce.input.fileinputformat.split.maxsize", String.valueOf(unitmb * 1024 * 1024));
conf.set("mapred.min.split.size", String.valueOf(unitmb * 1024 * 1024));
conf.set("mapreduce.input.fileinputformat.split.minsize.per.node", String.valueOf(unitmb * 1024 * 1024));
conf.set("mapreduce.input.fileinputformat.split.minsize.per.rack", String.valueOf(unitmb * 1024 * 1024)); Job job = new Job(conf);
FileInputFormat.addInputPath(job, new Path(in));
FileOutputFormat.setOutputPath(job, new Path(out));
job.setMapperClass(C01Mapper.class);
job.setReducerClass(C01Reducer.class);
job.setNumReduceTasks(nreducer);
job.setCombinerClass(C01Reducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setJarByClass(MRTest.class);
job.waitForCompletion(true);
}
}

2、统计用户在网站的停留时间

数据格式(用户,毫秒数,网站,以tab分开):

A	100	baidu.com
B 900 google.com
C 515 sohu.com
D 618 sina.com
E 791 google.com
B 121 baidu.com
C 915 google.com
D 112 sohu.com
E 628 sina.com
A 681 google.com
C 121 baidu.com
D 215 google.com
E 812 sohu.com
A 128 sina.com
B 291 google.com
 package com.mr.test;

 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.Text;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class MRWeb { public static class C02Mapper extends Mapper<Object, Text, Text, Text> {
@Override
public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
String line[] = value.toString().split("\t");
//格式检查
if(line.length == 3){
String name = line[0];
String time = line[1];
String website = line[2];
context.write(new Text(name + "\t" + time), new Text(time + "\t" + website));
}
}
} public static class C02Partitioner extends Partitioner<Text, Text> { @Override
public int getPartition(Text key, Text value, int number) {
String name = key.toString().split("\t")[0];
int hash =name.hashCode();
//以此实现分区
return Math.abs(hash % number);
} } public static class C02Sort extends WritableComparator {
//必须有的
protected C02Sort() {
super(Text.class,true);
} @Override
public int compare(WritableComparable w1, WritableComparable w2) {
Text h1 = new Text(((Text)w1).toString().split("\t")[0] );
Text h2 = new Text(((Text)w2).toString().split("\t")[0] );
IntWritable m1 =new IntWritable(Integer.valueOf(((Text)w1).toString().split("\t")[1]));
IntWritable m2 =new IntWritable(Integer.valueOf(((Text)w2).toString().split("\t")[1])); int result;
if(h1.equals(h2)){
result = m2.compareTo(m1);
}else {
result =h1.compareTo(h2);
}
return result;
}
} public static class C02Group extends WritableComparator{
protected C02Group() {
super(Text.class,true);
}
@Override
public int compare(WritableComparable w1, WritableComparable w2) {
Text h1 = new Text(((Text)w1).toString().split("\t")[0] );
Text h2 = new Text(((Text)w2).toString().split("\t")[0] ); return h1.compareTo(h2);
}
} public static class C02Reducer extends Reducer<Text, Text, IntWritable, Text> { @Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
int count = 0;
String name =key.toString().split("\t")[0];
//分组排序已经做好了,这里只管打印
for(Text value : values){
count++;
StringBuffer buffer = new StringBuffer();
buffer.append(name);
buffer.append("\t");
buffer.append(value.toString());
context.write(new IntWritable(count), new Text(buffer.toString()));
}
}
} public static void main(String[] args) throws IllegalArgumentException, IOException, ClassNotFoundException, InterruptedException {
//参数含义: agrs[0]标识 in, agrs[1]标识 out,agrs[2]标识 unitmb,agrs[3]标识 reducer number,
if(args.length != 4){
System.out.println("error");
System.exit(0);
} int unitmb =Integer.valueOf(args[2]);
String in = args[0];
String out = args[1];
int nreducer = Integer.valueOf(args[3]); Configuration conf = new Configuration();
conf.set("mapreduce.input.fileinputformat.split.maxsize", String.valueOf(unitmb * 1024 * 1024));
conf.set("mapred.min.split.size", String.valueOf(unitmb * 1024 * 1024));
conf.set("mapreduce.input.fileinputformat.split.minsize.per.node", String.valueOf(unitmb * 1024 * 1024));
conf.set("mapreduce.input.fileinputformat.split.minsize.per.rack", String.valueOf(unitmb * 1024 * 1024)); Job job = new Job(conf);
FileInputFormat.addInputPath(job, new Path(in));
FileOutputFormat.setOutputPath(job, new Path(out));
job.setMapperClass(C02Mapper.class);
job.setReducerClass(C02Reducer.class);
job.setNumReduceTasks(nreducer);
job.setPartitionerClass(C02Partitioner.class);
job.setGroupingComparatorClass(C02Group.class);
job.setSortComparatorClass(C02Sort.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Text.class);
job.setJarByClass(MRWeb.class);
job.waitForCompletion(true);
}
}

运行:hadoop jar ~/c02mrtest.jar com.mr.test.MRWeb TestData/webcount.txt /DataWorld/webresult 128 1

结果的样子:

3、json数组分析

数据格式(前面以tab分开):

1	[{"name":"A","age":16,"maths":100}]
2 [{"name":"B","age":17,"maths":97}]
3 [{"name":"C","age":18,"maths":89}]
4 [{"name":"D","age":15,"maths":98}]
5 [{"name":"E","age":19,"maths":100}]
 package com.mr.test;

 import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class MRString { public static class C03Mapper extends Mapper<Object, Text, Text, Text> {
@Override
protected void map(Object key, Text value, Mapper<Object, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
String[] line = value.toString().split("\t");
if(line.length ==2){
String c = line[0];
String j = line[1];
JSONArray jsonArray =JSONArray.fromObject(j);
int size = jsonArray.size();
for(int i=0;i<size;i++){
String name = "";
String age = "";
String maths = "";
JSONObject jsonObject =jsonArray.getJSONObject(i);
if(jsonObject.containsKey("name")){
name = jsonObject.getString("name");
}
if(jsonObject.containsKey("age")){
age = jsonObject.getString("age");
}
if(jsonObject.containsKey("maths")){
maths = jsonObject.getString("maths");
}
StringBuffer buffer =new StringBuffer();
buffer.append(name);
buffer.append("\t");
buffer.append(age);
buffer.append("\t");
buffer.append(maths);
context.write(new Text(c), new Text(buffer.toString()));
}
}
}
} public static void main(String[] args) throws IllegalArgumentException, IOException, ClassNotFoundException, InterruptedException {
//参数含义: agrs[0]标识 in, agrs[1]标识 out,agrs[2]标识 unitmb,agrs[3]
if(args.length != 3){
System.out.println("error");
System.exit(0);
} int unitmb =Integer.valueOf(args[2]);
String in = args[0];
String out = args[1]; Configuration conf = new Configuration();
conf.set("mapreduce.input.fileinputformat.split.maxsize", String.valueOf(unitmb * 1024 * 1024));
conf.set("mapred.min.split.size", String.valueOf(unitmb * 1024 * 1024));
conf.set("mapreduce.input.fileinputformat.split.minsize.per.node", String.valueOf(unitmb * 1024 * 1024));
conf.set("mapreduce.input.fileinputformat.split.minsize.per.rack", String.valueOf(unitmb * 1024 * 1024)); Job job = new Job(conf);
job.addFileToClassPath(new Path("TestData/json-lib-2.4-jdk15.jar"));
job.addFileToClassPath(new Path("TestData/ezmorph-1.0.6.jar"));
FileInputFormat.addInputPath(job, new Path(in));
FileOutputFormat.setOutputPath(job, new Path(out));
job.setMapperClass(C03Mapper.class);
//没有reducer的情况下必须设置
job.setNumReduceTasks(0);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setJarByClass(MRString.class);
job.waitForCompletion(true);
}
}

运行 hadoop jar ~/c03mrtest.jar com.mr.test.MRString TestData/jsonarray.txt /DataWorld/jsonoutput 128

结果:

这个例子还有一点值得注意(Path中的目录是HDFS中的目录):

job.addFileToClassPath(new Path("TestData/json-lib-2.4-jdk15.jar")); //jar文件下载地址:http://json-lib.sourceforge.net/

job.addFileToClassPath(new Path("TestData/ezmorph-1.0.6.jar")); //jar文件下载地址:http://ezmorph.sourceforge.net/
使用这两句,在程序中动态添加了用于json解析的jar文件,而利用服务器中的ClassPath是访问不到这两个文件的。在编程的时候,在windows客户端下,为了语法书写方便,导入了json-lib-2.4-jdk15.jar,但是并没有导入ezmorph-1.0.6.jar 。

也就是说,可以在程序中动态的加入jar文件,只要知道了它在HDFS中的位置。

MapRedue开发实例的更多相关文章

  1. ecshop二次开发 给商品添加自定义字段【包含我自己进一步的开发实例详解】

    本文包含商品自定义添加教程及进一步的开发实例: 教程: 说起自定义字段,我想很多的朋友像我一样会想起一些开源的CMS(比如Dedecms.Phpcms.帝国)等,他们是可以在后台直接添加自定义字段的. ...

  2. RDIFramework.NET -.NET快速信息化系统开发整合框架 【开发实例 EasyUI】之产品管理(WebForm版)

    RDIFramework.NET—.NET快速开发整合框架 [开发实例]之产品管理(WebForm版) 接上篇:RDIFramework.NET (.NET快速信息化系统开发整合框架) [开发实例]之 ...

  3. RDIFramework.NET-.NET快速信息化系统开发整合框架 【开发实例 EasyUI】之产品管理(MVC版)

    RDIFramework.NET—.NET快速开发整合框架 [开发实例]之产品管理(MVC版) 接上篇:RDIFramework.NET (.NET快速信息化系统开发整合框架) [开发实例]之产品管理 ...

  4. Cocos2d-x 3.X手游开发实例详解

    Cocos2d-x 3.X手游开发实例详解(最新最简Cocos2d-x手机游戏开发学习方法,以热门游戏2048.卡牌为例,完整再现手游的开发过程,实例丰富,代码完备,Cocos2d-x作者之一林顺和泰 ...

  5. 免费的HTML5连载来了《HTML5网页开发实例详解》连载(二)

    最近新浪.百度.腾讯.京东.大众点评.淘宝等流行的网站都加大了招聘HTML5的力度,HTML5开发人员成了抢手货,本次连载的是由大众点评前端工程师和一淘网前端工程师基情奉献的<HTML5网页开发 ...

  6. RDIFramework.NET开发实例━表约束条件权限的使用-Web

    RDIFramework.NET开发实例━表约束条件权限的使用-Web 在上一篇文章“RDIFramework.NET开发实例━表约束条件权限的使用-WinForm”我们讲解了在WinForm下表约束 ...

  7. RDIFramework.NET开发实例━表约束条件权限的使用-WinForm

    RDIFramework.NET开发实例━表约束条件权限的使用-WinForm 在实际的应用中,客户常有这样的需求,指定用户或角色可以看指定条件下的数据,这里的“指定条件”在RDIFramework. ...

  8. RDIFramework.NET V2.8版本 ━ 开发实例之产品管理(WinForm)

    RDIFramework.NET V2.8版本 ━ 开发实例之产品管理(WinForm) 现在,我们使用.NET快速开发整合框架(RDIFramework.NET)来开发一个应用,此应用皆在说明如何使 ...

  9. Android音乐播放器的开发实例

    本文将引导大家做一个音乐播放器,在做这个Android开发实例的过程中,能够帮助大家进一步熟悉和掌握学过的ListView和其他一些组件.为了有更好的学习效果,其中很多功能我们手动实现,例如音乐播放的 ...

随机推荐

  1. Sql Server函数全解(一)字符串函数

    字符串函数用于对字符和二进制字符进行各种操作 1.ASCII()函数  ASCII(character_expression)函数用于返回字符串表达式中最左侧的ASCII代码值.参数character ...

  2. byte为什么要与上0xff?

    无意间翻看之间的代码,发现了一段难以理解的代码. byte[] bs = digest.digest(origin.getBytes(Charset.forName(charsetName))) ; ...

  3. GUI 和 GUILayout 的区别

    GUI 和 GUILayout 的区别 A~ GUI是Unity中的基础控件类,其中包含了常用的GUI控件,列如Button,Label,PasswordField,slider,Window等等~ ...

  4. Net设计模式实例之桥接模式( Bridge Pattern)

    一.桥接模式简介(Brief Introduction) 桥接模式(Bridge Pattern),将抽象部分与它的实现部分分离,使的抽象和实现都可以独立地变化. Decouple an abstra ...

  5. iOS查看3D效果的手势交互

    公司项目中用到的,仿的人家厂子的效果,看起来还是挺高大上的,其实实现起来很简单,是一种伪3D;用手势滑动查看一个商品的3D展示. 在手机上手指左右滑动可以360°无死角查看这个商品,有兴趣的可以下de ...

  6. clr enabled Server Configuration Option

    在SQL Server中启用CLR,可以执行下面SQL语句: EXEC sp_configure 'clr enabled'; '; RECONFIGURE; Source Code

  7. mobileControls与移动控件适配

    此配置节的作用在于指定各种控件在不同类型的移动设备显示的适配器,以达到适应各种设备不同的展示形式.例子如下, <mobileControls sessionStateHistorySize=&q ...

  8. wampsever在线模式和离线模式有什么区别

    我们在开发网站的时候经常会使用到wampsever服务器,在测试项目的时候我们会经常发现,wampsever服务器在线模式和离线模式都可以使用并且测试,还有一个现象就是我们在测试无线网络,用手机访问的 ...

  9. Eclipse切换SVN用户

    1. 点击windows --> preference --> Team --> SVN,查看当前的SVN接口. 2. 如果SVN接口是JavaHL,那么找到C:\Documents ...

  10. java.lang.Exception: Could not determine the type of file "smb://zhangsan:123456@10.77.44.222/o/mmfiles_2016/40094/25556/2130.avi".

    1. 使用smb协议上传文件 报上述错误 2. 解决方法 开启administrator超级管理员 smb地址改为smb://administrator:123456@10.10.10.10/o/.. ...