mapreduce数据处理——统计排序
接上篇https://www.cnblogs.com/sengzhao666/p/11850849.html
2、数据处理:
·统计最受欢迎的视频/文章的Top10访问次数 (id)
·按照地市统计最受欢迎的Top10课程 (ip)
·按照流量统计最受欢迎的Top10课程 (traffic)
分两步:
统计;排序
初始文件部分样例:
1.192.25.84 2016-11-10-00:01:14 10 54 video 5551
1.194.144.222 2016-11-10-00:01:20 10 54 video 3589
1.194.187.2 2016-11-10-00:01:05 10 54 video 2212
1.203.177.243 2016-11-10-00:01:18 10 6050 video 7361
1.203.177.243 2016-11-10-00:01:19 10 72 video 7361
1.203.177.243 2016-11-10-00:01:22 10 6050 video 7361
1.30.162.63 2016-11-10-00:01:46 10 54 video 3639
1.84.205.195 2016-11-10-00:01:12 10 54 video 1412
统计:
package priv.tzk.mapreduce.dataProcess.visits; 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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; public class DataVisits {
public static String INPUT_PATH="/home/hadoop/out";
public static String OUTPUT_PATH="hdfs://localhost:9000/mapReduce/mymapreduce1/out"; public static class Map extends Mapper<Object,Text,Text,IntWritable>{ //将输入输出作为string类型,对应Text类型
private static Text newKey=new Text(); //每一行作为一个数据
public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
String line=value.toString();//转为字符串类型
//System.out.println(line);
if(!("".equals(line)))//增加控制语句,使得line为”“时能够停止。否则不符合reduce接受的数据不会执行reduce
{
String arr[]=line.split("\t");//splite是按照输入的值拆分成数组
newKey.set(arr[5]);
int click=1;
context.write(newKey,new IntWritable(click));
//System.out.println(newKey+" "+new IntWritable(click));
}
}
} public static class Reduce extends Reducer<Text,IntWritable,Text,IntWritable>{
public void reduce(Text key,Iterable<IntWritable> values,Context context) throws IOException,InterruptedException{
int count=0;
for(IntWritable val:values) {
//Iterable迭代器
count++;
}
context.write(key,new IntWritable(count));
//System.out.println("reduceStart");
}
} public static void main(String[] args) throws IOException,ClassNotFoundException,InterruptedException{
Configuration conf=new Configuration();
System.out.println("start");
Job job=Job.getInstance(conf);
job.setJobName("MyAverage");
//Job job =new Job(conf,"MyAverage");
job.setJarByClass(DataVisits.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);//设置map的输出格式
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
Path outputpath=new Path(OUTPUT_PATH);
Path inputpath=new Path(INPUT_PATH);
FileInputFormat.addInputPath(job,inputpath );
FileOutputFormat.setOutputPath(job,outputpath);
boolean flag = job.waitForCompletion(true);
System.out.println(flag);
System.exit(flag? 0 : 1);
} }
统计部分结果样例:
10061 1
10077 1
10198 1
10290 1
10314 1
10324 1
1034 1
10400 1
10421 1
10427 1
10450 1
10505 1
10506 7
10511 1
针对统计结果排序:
package priv.tzk.mapreduce.dataProcess.visits; 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.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; public class visitsSort {
public static String INPUT_PATH="/home/hadoop/visits_out";
public static String OUTPUT_PATH="hdfs://localhost:9000/mapReduce/mymapreduce1/out1"; public static class Sort extends WritableComparator {
public Sort(){
//这里就是看你map中填的输出key是什么数据类型,就给什么类型
super(IntWritable.class,true);
}
@Override
public int compare(WritableComparable a, WritableComparable b) {
return -a.compareTo(b);//加个负号就是倒序,把负号去掉就是正序。
}
} public static class Map extends Mapper<Object,Text,IntWritable,Text>{ //将输入输出作为string类型,对应Text类型
private static Text mid=new Text();
private static IntWritable num=new IntWritable();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
String line=value.toString();//转为字符串类型
if(!("".equals(line)))//增加控制语句,使得line为”“时能够停止。否则不符合reduce接受的数据不会执行reduce
{
String arr[]=line.split("\t");//splite是按照输入的值拆分成数组
mid.set(arr[0]);
num.set(Integer.parseInt(arr[1]));
context.write(num,mid);
}
}
}
//MapReduce框架默认排序规则。它是按照key值进行排序的
public static class Reduce extends Reducer<IntWritable,Text,IntWritable,Text>{
private static int i=0;
public void reduce(IntWritable key,Iterable<Text> values,Context context) throws IOException,InterruptedException{ for(Text val:values) {
//Iterable迭代器
if(i<10) {
i++;
context.write(key, val);
}
}
//System.out.println("reduceStart");
}
} public static void main(String[] args) throws IOException,ClassNotFoundException,InterruptedException{
Configuration conf=new Configuration();
System.out.println("start");
Job job=Job.getInstance(conf);
//Job job =new Job(conf,"");
job.setJarByClass(visitsSort.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setSortComparatorClass(Sort.class);
//设置map的输出格式
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Text.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
Path outputpath=new Path(OUTPUT_PATH);
Path inputpath=new Path(INPUT_PATH);
FileInputFormat.addInputPath(job,inputpath );
FileOutputFormat.setOutputPath(job,outputpath);
boolean flag = job.waitForCompletion(true);
System.out.println(flag);
System.exit(flag? 0 : 1);
} }
排序结果:
31 2402
19 1309
18 3078
18 2801
16 5683
16 3369
16 1336
16 4018
15 11239
15 13098
mapreduce数据处理——统计排序的更多相关文章
- 一脸懵逼学习Hadoop中的序列化机制——流量求和统计MapReduce的程序开发案例——流量求和统计排序
一:序列化概念 序列化(Serialization)是指把结构化对象转化为字节流.反序列化(Deserialization)是序列化的逆过程.即把字节流转回结构化对象.Java序列化(java.io. ...
- Hadoop学习笔记—11.MapReduce中的排序和分组
一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排 ...
- MapReduce 单词统计案例编程
MapReduce 单词统计案例编程 一.在Linux环境安装Eclipse软件 1. 解压tar包 下载安装包eclipse-jee-kepler-SR1-linux-gtk-x86_64.ta ...
- mapreduce 实现数子排序
设计思路: 使用mapreduce的默认排序,按照key值进行排序的,如果key为封装int的IntWritable类型,那么MapReduce按照数字大小对key排序,如果key为封装为String ...
- Hadoop学习笔记: MapReduce二次排序
本文给出一个实现MapReduce二次排序的例子 package SortTest; import java.io.DataInput; import java.io.DataOutput; impo ...
- (转)MapReduce二次排序
一.概述 MapReduce框架对处理结果的输出会根据key值进行默认的排序,这个默认排序可以满足一部分需求,但是也是十分有限的.在我们实际的需求当中,往往有要对reduce输出结果进行二次排序的需求 ...
- Mysql 分别按月, 日为组group,进行统计排序order
在数据库中我们经经常使用sql语句对表进行简单的统计排序,对于日期字段.我们能够简单的对其进行order. 对于复杂一点的能够按日期中的年.月,日分别进行group,order. 按年份进行group ...
- 详细讲解MapReduce二次排序过程
我在15年处理大数据的时候还都是使用MapReduce, 随着时间的推移, 计算工具的发展, 内存越来越便宜, 计算方式也有了极大的改变. 到现在再做大数据开发的好多同学都是直接使用spark, hi ...
- 【Cloud Computing】Hadoop环境安装、基本命令及MapReduce字数统计程序
[Cloud Computing]Hadoop环境安装.基本命令及MapReduce字数统计程序 1.虚拟机准备 1.1 模板机器配置 1.1.1 主机配置 IP地址:在学校校园网Wifi下连接下 V ...
随机推荐
- Docker/Dockerfile debug调试技巧
『重用』容器名 但我们在编写/调试Dockerfile的时候我们经常会重复之前的command,比如这种docker run --name jstorm-zookeeper zookeeper:3.4 ...
- 2019-07-24 PHP中mysql_fetch_assoc 和 mysql_fetch_array 有什么区别?
mysql_fetch_assoc() 函数从结果集中取得一行作为关联数组 来看下面的例子: 数据库中有上述几条数据,一般我们想取用就要按照如下代码: $con = mysql_connect('12 ...
- pandas-08 pd.cut()的功能和作用
pandas-08 pd.cut()的功能和作用 pd.cut()的作用,有点类似给成绩设定优良中差,比如:0-59分为差,60-70分为中,71-80分为优秀等等,在pandas中,也提供了这样一个 ...
- CSS3扇形进度效果
.coutdown-animate { position: absolute; top: 0; left: 0; right: 0; bottom: 0; ...
- JAVA基础之会话技术-Cookie及Session
至此,学习Servlet三个域对象:ServletContext(web项目).request(一次请求).Session(一个客户端)!均有相同的方法! 从用户开始打开浏览器进行操作,便开始了一次会 ...
- JavaWeb 之 EL表达式
EL 表达式 一.概述 1.概念 EL 表达式:Expression Language 表达式语言. 2.作用 替换和简化 jsp 页面中 java 代码的编写. 3.语法格式 ${表达式} 4.注意 ...
- 使用sudo进行Linux权限升级技巧
0x00 前言 在我们之前的文章中,我们讨论了如何使用SUID二进制文件和/etc/passwd 文件的Linux权限提升技巧,今天我们发布了另一种“使用Sudoers文件进行Linux权限提示技巧” ...
- Java 参数个数可变的函数
示例: package my_package; public class Test { public static void main(String[] args) { out("重庆师范大 ...
- Eclipse不支持tomcat8_compiler编译级别选不到1.8
-------------------------------------------------------------- Eclipse不支持tomcat8 如果你要使用tomcat8.0+版本的 ...
- 【故障处理】分布式事务ORA-01591错误解决
[故障处理]分布式事务ORA-01591错误解决 1 BLOG文档结构图 2 前言部分 2.1 导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你 ...