接上篇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. 爬虫多次爬取时候cookie的存储用于登入

    一.用requests模块自动保存(保存缓存中) 构建一个session对象session = requests.session() 用构建的session代替requests进行访问他就会自动存啦 ...

  2. Date类的相关方法记录

    1.Date类中的时间单位是毫秒,System.currentTimeMills()方法就是获取当前时间到1970年1月1日0时0分0秒(西方时间)的毫秒数. public class Test6 { ...

  3. JavaScript之控制标签css

    控制标签css标签.style.样式='样式具体的值'如果样式出现中横线,如border-radius,将中横线去掉,中横线后面的单词首字母大写,写成borderRadius如果原来就要该样式,表示修 ...

  4. HTTP协议学习总结

    一个web应用程序,往往是通过http协议进行前后端通信的.而作为一个web工程师,掌握HTTP协议因此也是Web开发必备的一项技能了,尤其是在工作了一定年限之后,更是深感该知识点的重要性.因此,将以 ...

  5. Android 下拉列表Spinner 使用

    1.布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...

  6. jstorm了解—应用场景

    JStorm处理数据的方式是基于消息的流水线处理, 因此特别适合无状态计算,也就是计算单元的依赖的数据全部在接受的消息中可以找到, 并且最好一个数据流不依赖另外一个数据流. 因此,常常用于: 日志分析 ...

  7. 【python】文件下载---基础版

    基于TCP协议的基础版本,不支持大文件 Client.py import socket def main(): # 1. 创建套接字 tcp_socket = socket.socket(socket ...

  8. 腾讯微服务框架Tars的初体验

    最近研究了一下腾讯的微服务体系开发框架. 官方的搭建过程:https://github.com/TarsCloud/Tars/blob/master/Install.zh.md 自己填的坑: 不得不说 ...

  9. mysql导入数据和导出数据

    导入数据: 首页进入mysql命令行界面: use 数据库名: source d:/data/test.sql; 如果是windows系统必须使用d:/,如果使用d:\会报语法错误. 那么如何导出(备 ...

  10. spring cloud turbine 监控不到其它机器上的hystrix.stream 的解决方法 指定监控ip

    turbine多台机器熔断聚合的时候  turbine控制台一直寻找的是localhost下的监控熔断数据. c.n.t.monitor.instance.InstanceMonitor   : Ur ...