接上篇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数据处理——统计排序的更多相关文章

  1. 一脸懵逼学习Hadoop中的序列化机制——流量求和统计MapReduce的程序开发案例——流量求和统计排序

    一:序列化概念 序列化(Serialization)是指把结构化对象转化为字节流.反序列化(Deserialization)是序列化的逆过程.即把字节流转回结构化对象.Java序列化(java.io. ...

  2. Hadoop学习笔记—11.MapReduce中的排序和分组

    一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排 ...

  3. MapReduce 单词统计案例编程

    MapReduce 单词统计案例编程 一.在Linux环境安装Eclipse软件 1.   解压tar包 下载安装包eclipse-jee-kepler-SR1-linux-gtk-x86_64.ta ...

  4. mapreduce 实现数子排序

    设计思路: 使用mapreduce的默认排序,按照key值进行排序的,如果key为封装int的IntWritable类型,那么MapReduce按照数字大小对key排序,如果key为封装为String ...

  5. Hadoop学习笔记: MapReduce二次排序

    本文给出一个实现MapReduce二次排序的例子 package SortTest; import java.io.DataInput; import java.io.DataOutput; impo ...

  6. (转)MapReduce二次排序

    一.概述 MapReduce框架对处理结果的输出会根据key值进行默认的排序,这个默认排序可以满足一部分需求,但是也是十分有限的.在我们实际的需求当中,往往有要对reduce输出结果进行二次排序的需求 ...

  7. Mysql 分别按月, 日为组group,进行统计排序order

    在数据库中我们经经常使用sql语句对表进行简单的统计排序,对于日期字段.我们能够简单的对其进行order. 对于复杂一点的能够按日期中的年.月,日分别进行group,order. 按年份进行group ...

  8. 详细讲解MapReduce二次排序过程

    我在15年处理大数据的时候还都是使用MapReduce, 随着时间的推移, 计算工具的发展, 内存越来越便宜, 计算方式也有了极大的改变. 到现在再做大数据开发的好多同学都是直接使用spark, hi ...

  9. 【Cloud Computing】Hadoop环境安装、基本命令及MapReduce字数统计程序

    [Cloud Computing]Hadoop环境安装.基本命令及MapReduce字数统计程序 1.虚拟机准备 1.1 模板机器配置 1.1.1 主机配置 IP地址:在学校校园网Wifi下连接下 V ...

随机推荐

  1. kali之nmap

    nmap简介 Nmap,也就是Network Mapper,最早是Linux下的网络扫描和嗅探工具包.可以扫描主机.端口.并且识别端口所对应的协议,以及猜测操作系统 Ping扫描(-sP参数) TCP ...

  2. MySQL数据库汇总

    -- mysql的最大连接数:默认为 100   -- mysql的增删改查   -- mysql统计各个字段(case when 用法 注:也可以使用其他的)   select (case when ...

  3. 一张图带你看懂原始dao与SQL动态代理开发的区别-Mybatis

    //转载请注明出处:https://www.cnblogs.com/nreg/p/11156167.html 1.项目结构区别: 2.开发区别: 注:其中原始dao开发的实现类UserDaoImpl ...

  4. CSS-宽度自适应和浏览器兼容笔记

    自适应 宽度自适应:网页元素根据窗口或子元素自动调整宽度 适用百分比进行设置,例如:100% 铺满:50% 占据一般宽度 块元素如果不设置宽度,默认为100% 自适应中可以设置最大或者最小宽度和高度 ...

  5. 钻石diamaund外语

    Whenever I fail as a father or husband... a toy and a diamond always works. I never worry about diet ...

  6. 小程序中的数据请求sessionid,保持登陆状态。

    版权声明:本文为CSDN博主「weixin_43964779」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net ...

  7. etcd数据备份和恢复--转发

    对于etcd api v3数据备份与恢复方法 # export ETCDCTL_API=3 # etcdctl --endpoints localhost:2379 snapshot save sna ...

  8. MySQL数据库扫盲篇

    MySQL数据库扫盲篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一.MySQL概述 1>.什么是MySQL MySQL是瑞典的MySQL AB公司开发的一个可用于各 ...

  9. C++(四十九) — set、multiset 容器的基本操作

     1.set的基础知识 set的特性是:所有元素都会根据元素的键值自动排序,set的元素不像map那样可以同时拥有实值(value)和键值(key),set元素的键值就是实值,实值就是键值.set不允 ...

  10. 图森未来一道笔试题-迷宫难题【BFS找S->E的最短步数】

    时间限制:3秒 空间限制:262144K 图森未来的自动驾驶小卡车今天被派到了一个陌生的迷宫内部运输一些货物. 工程师小图已经提前拿到了这个迷宫的地图,地图是一个n*m的字符矩阵,上面包含四种不同的字 ...