一些例子,所用版本为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. MyEclipse打不开jsp文件 报错“Failed to create the part's controls"

    本来写好了一套网站html代码,需要移植到myeclipse的jsp页面中,当复制代码完毕后,也是可以运行的并不报错,但一直有弹框提示有空指针,当关闭页面后再次打开就歇菜了,提示 我预感到这可能是my ...

  2. MySQL学习(一)MySQLWorkbench(MySQL可视化工具)下载,安装,测试连接,以及注意事项

    PS:MySQLWorkbench是MYSQL自带的可视化工具,无论使用哪个可视化工具,其实大同小异,如果想以后走的更远的话,可以考虑使用命令行操作数据库MYSQL.可视化工具让我们初学者更能理解数据 ...

  3. PJAX初体验(主要是利用HTML5 新增API pushState和replaceState+AJAX)

    说在前面 什么是PJAX呢? 站在应用角度的就是既实现了页面无刷新的效果,同时也产生了浏览器的前进和后退,而且url也会变化. 也不是什么新鲜技术,主要是AJAX+html5 pushState和re ...

  4. Windows Phone 8 开发系列(持续更新中)

    1. 从应用列表再次点击应用,如何恢复到上次浏览的页面呢? 2. Windows Phone 文本框的 UpdateSourceTrigger 属性不支持 PropertyChanged 怎么办? 3 ...

  5. Bootstrap 3.0正式版发布!

    Bootstrap是Twitter推出的一个开源的用于前端开发的工具包,包含了丰富的Web组件.根据这些组件,开发者可以快速的搭建一个漂亮.功能完备的网站.在经过Bootstrap 3 RC版的测试和 ...

  6. linux源码分析(三)-start_kernel

    前置:这里使用的linux版本是4.8,x86体系. start_kernel是过了引导阶段,进入到了内核启动阶段的入口.函数在init/main.c中. set_task_stack_end_mag ...

  7. SQL Server 存储过程遇到“表 '#TT' 没有标识属性。无法执行 SET 操作”错误

    创建临时表,往临时表插入数据的时候报的错误. 一开始提示没有打开主键,后来打开主键就提示上述错误异常. 从网上查找资料没有找到,然后又到群里问各位大牛,一位大牛告诉我是没有设置主键. 我又仔细看看提示 ...

  8. 在c#中get同步访问http

    参照文章:http://blog.csdn.net/qianmenfei/article/details/37974767 public static string SendMessage(strin ...

  9. xhtmlConformance与xhtml脚本呈现

    此配置节只有一个属性——mode,该特性为 ASP.NET 应用程序指定 XHTML 呈现模式.它包含三个值 要让此配置生效,需要把<pages>配置节中的controlRendering ...

  10. 积累一下SQL

    开篇先自我检讨一下,写了博客几年以来首次试过连续两个月没出过博文,有客观也有主观原因,但是最近这年里博文数量也越来越少,博文的质量也每况日下.希望自己一直能坚持下来,多写写博文,这月尽量多写几篇来弥补 ...