数据包

百度网盘

链接:https://pan.baidu.com/s/1v9M3jNdT4vwsqup9N0mGOA
提取码:hs9c
复制这段内容后打开百度网盘手机App,操作更方便哦

1、     数据清洗说明:

(1)      第一列是时间;

(2)      第二列是卖出方;

(3)      第三列是买入方;

(4)      第四列是票的数量;

(5)      第五列是金额。

卖出方,买入方一共三个角色,机场(C开头),代理人(O开头)和一般顾客(PAX)

2、     数据清洗要求:

(1)统计最繁忙的机场Top10(包括买入卖出);

(2)统计最受欢迎的航线;(起点终点一致(或相反))

(3)统计最大的代理人TOP10;

(4)统计某一天的各个机场的卖出数据top10。

3、     数据可视化要求:

(1)上述四中统计要求可以用饼图、柱状图等显示;

(2)可用关系图展示各个机场之间的联系程度(以机票数量作为分析来源)。

 

实验关键部分代码(列举统计最繁忙机场的代码,其他代码大同小异):

数据初步情理,主要是过滤出各个机场个总票数

1.    package mapreduce;
2. import java.io.IOException;
3. import java.net.URI;
4. import org.apache.hadoop.conf.Configuration;
5. import org.apache.hadoop.fs.Path;
6. import org.apache.hadoop.io.LongWritable;
7. import org.apache.hadoop.io.Text;
8. import org.apache.hadoop.mapreduce.Job;
9. import org.apache.hadoop.mapreduce.Mapper;
10. import org.apache.hadoop.mapreduce.Reducer;
11. import org.apache.hadoop.mapreduce.lib.chain.ChainMapper;
12. import org.apache.hadoop.mapreduce.lib.chain.ChainReducer;
13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
14. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
15. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
16. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
17. import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;
18. import org.apache.hadoop.fs.FileSystem;
19. import org.apache.hadoop.io.IntWritable;
20. public class ChainMapReduce {
21. private static final String INPUTPATH = "hdfs://localhost:9000/mapreducetest/region.txt";
22. private static final String OUTPUTPATH = "hdfs://localhost:9000/mapreducetest/out1";
23. public static void main(String[] args) {
24. try {
25. Configuration conf = new Configuration();
26. FileSystem fileSystem = FileSystem.get(new URI(OUTPUTPATH), conf);
27. if (fileSystem.exists(new Path(OUTPUTPATH))) {
28. fileSystem.delete(new Path(OUTPUTPATH), true);
29. }
30. Job job = new Job(conf, ChainMapReduce.class.getSimpleName());
31. FileInputFormat.addInputPath(job, new Path(INPUTPATH));
32. job.setInputFormatClass(TextInputFormat.class);
33. ChainMapper.addMapper(job, FilterMapper1.class, LongWritable.class, Text.class, Text.class, IntWritable.class, conf);
34. ChainReducer.setReducer(job, SumReducer.class, Text.class, IntWritable.class, Text.class, IntWritable.class, conf);
35. job.setMapOutputKeyClass(Text.class);
36. job.setMapOutputValueClass(IntWritable.class);
37. job.setPartitionerClass(HashPartitioner.class);
38. job.setNumReduceTasks(1);
39. job.setOutputKeyClass(Text.class);
40. job.setOutputValueClass(IntWritable.class);
41. FileOutputFormat.setOutputPath(job, new Path(OUTPUTPATH));
42. job.setOutputFormatClass(TextOutputFormat.class);
43. System.exit(job.waitForCompletion(true) ? 0 : 1);
44. } catch (Exception e) {
45. e.printStackTrace();
46. }
47. }
48. public static class FilterMapper1 extends Mapper<LongWritable, Text, Text, IntWritable> {
49. private Text outKey = new Text();
50. private IntWritable outValue = new IntWritable();
51. @Override
52. protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
53. throws IOException,InterruptedException {
54. String line = value.toString();
55. if (line.length() > 0) {
56. String[] arr = line.split(",");
57. int visit = Integer.parseInt(arr[3]);
58. if(arr[1].substring(0, 1).equals("C")||arr[2].substring(0, 1).equals("C")){
59. outKey.set(arr[1]);
60. outValue.set(visit);
61. context.write(outKey, outValue);
62. }
63. }
64. }
65. }
66.
67. public static class SumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
68. private IntWritable outValue = new IntWritable();
69. @Override
70. protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context)
71. throws IOException, InterruptedException {
72. int sum = 0;
73. for (IntWritable val : values) {
74. sum += val.get();
75. }
76. outValue.set(sum);
77. context.write(key, outValue);
78. }
79. }
80.
81.
82. }

数据二次清理,进行排序

package mapreduce;
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 OneSort {
public static class Map extends Mapper<Object , Text , IntWritable,Text >{
private static Text goods=new Text();
private static IntWritable num=new IntWritable();
public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
String line=value.toString();
String arr[]=line.split("\t");
num.set(Integer.parseInt(arr[1]));
goods.set(arr[0]);
context.write(num,goods);
}
}
public static class Reduce extends Reducer< IntWritable, Text, IntWritable, Text>{
private static IntWritable result= new IntWritable();
public void reduce(IntWritable key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
for(Text val:values){
context.write(key,val);
}
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
Configuration conf=new Configuration();
Job job =new Job(conf,"OneSort");
job.setJarByClass(OneSort.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Text.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
Path in=new Path("hdfs://localhost:9000/mapreducetest/out1/part-r-00000");
Path out=new Path("hdfs://localhost:9000/mapreducetest/out2");
FileInputFormat.addInputPath(job,in);
FileOutputFormat.setOutputPath(job,out);
System.exit(job.waitForCompletion(true) ? 0 : 1); }
}

从hadoop中读取文件

  1. package mapreduce;  
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.URI;
    import java.util.ArrayList;
    import java.util.List; import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FSDataInputStream;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path; public class ReadFile {
    public static List<String> ReadFromHDFS(String file) throws IOException
    {
    //System.setProperty("hadoop.home.dir", "H:\\文件\\hadoop\\hadoop-2.6.4");
    List<String> list=new ArrayList();
    int i=0;
    Configuration conf = new Configuration();
    StringBuffer buffer = new StringBuffer();
    FSDataInputStream fsr = null;
    BufferedReader bufferedReader = null;
    String lineTxt = null; try
    {
    FileSystem fs = FileSystem.get(URI.create(file),conf);
    fsr = fs.open(new Path(file));
    bufferedReader = new BufferedReader(new InputStreamReader(fsr));
    while ((lineTxt = bufferedReader.readLine()) != null)
    {
    String[] arg=lineTxt.split("\t");
    list.add(arg[0]);
    list.add(arg[1]);
    }
    } catch (Exception e)
    {
    e.printStackTrace();
    } finally
    {
    if (bufferedReader != null)
    {
    try
    {
    bufferedReader.close();
    } catch (IOException e)
    {
    e.printStackTrace();
    }
    }
    }
    return list; } public static void main(String[] args) throws IOException {
    List<String> ll=new ReadFile().ReadFromHDFS("hdfs://localhost:9000/mapreducetest/out2/part-r-00000");
    for(int i=0;i<ll.size();i++)
    {
    System.out.println(ll.get(i));
    } } }

前台网页代码

<%@page import="mapreduce.ReadFile"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="org.apache.hadoop.fs.FSDataInputStream" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<% List<String> ll= ReadFile.ReadFromHDFS("hdfs://localhost:9000/mapreducetest/out2/part-r-00000");%>
<script src="../js/echarts.js"></script>
</head>
<body>
<div id="main" style="width: 900px;height:400px;"></div>
<script type="text/javascript">
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main')); // 指定图表的配置项和数据
var option = {
title: {
text: '最繁忙的机场TOP10'
},
tooltip: {},
legend: {
data:['票数']
},
xAxis: {
data:["<%=ll.get(ll.size()-1)%>"<%for(int i=ll.size()-3;i>=ll.size()-19;i--){
if(i%2==1){
%>,"<%=ll.get(i)%>"
<%
}
}
%>] },
yAxis: {},
series: [{
name: '票数',
type: 'bar',
data: [<%=ll.get(ll.size()-2)%>
<%for(int i=ll.size()-1;i>=ll.size()-19;i--){
if(i%2==0){
%>,<%=ll.get(i)%>
<%
}
}
%>]
}]
}; // 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
</script>
<h2 color="red"><a href="NewFile.jsp">返回</a></h2>
</body>

结果截图:

Mapreduce数据分析实例的更多相关文章

  1. Hadoop数据分析实例:P2P借款人信用风险实时监控模型设计

    Hadoop数据分析实例:P2P借款人信用风险实时监控模型设计 一提到hadoop相信熟悉IT领域或者经常关注互联网新闻的朋友都应该很熟悉了,当然,这种熟悉可能也只是听着名字耳熟,但并不知道它具体是什 ...

  2. MapReduce编程实例6

    前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...

  3. MapReduce编程实例5

    前提准备: 1.hadoop安装运行正常.Hadoop安装配置请参考:Ubuntu下 Hadoop 1.2.1 配置安装 2.集成开发环境正常.集成开发环境配置请参考 :Ubuntu 搭建Hadoop ...

  4. MapReduce编程实例4

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  5. MapReduce编程实例3

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  6. MapReduce编程实例2

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  7. 三、MapReduce编程实例

    前文 一.CentOS7 hadoop3.3.1安装(单机分布式.伪分布式.分布式 二.JAVA API实现HDFS MapReduce编程实例 @ 目录 前文 MapReduce编程实例 前言 注意 ...

  8. hadoop2.2编程:使用MapReduce编程实例(转)

    原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...

  9. Python实现MapReduce,wordcount实例,MapReduce实现两表的Join

    Python实现MapReduce 下面使用mapreduce模式实现了一个简单的统计日志中单词出现次数的程序: from functools import reduce from multiproc ...

随机推荐

  1. RabbitMQ消息队列(五)-安装amqp扩展并订阅/发布Demo(.Net Core版)

    publish发布消息 新建一个Asp.Net Core控制台项目:PublishDemo 安装Nuget包 Install-Package RabbitMQ.Client 添加命名空间引用 usin ...

  2. 【面试】我是如何面试别人List相关知识的,深度有点长文

  3. centos6.7 配置外网端口映射

    目的: 为节省公司外网ip,现需要把部分没有外网ip的服务器做端口映射. 服务器节点: 118.192.66.66(外网服务器) em1 内网 em2 外网 192.168.32.124(内网服务器) ...

  4. oracle数据库密码过期修改注意事项

    近期的工作中,因数据库密码临近过期,需要进行修改,因对oracle数据库底层结构不了解,导致安装网上的教程操作是出现一些问题,特记录下来 传统的修改语句为 输入:win+R进入cmd  输入sqlpl ...

  5. Spring Cloud Alibaba基础教程:Nacos配置的多环境管理

    前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...

  6. Python之celery的简介与使用

    celery的简介   celery是一个基于分布式消息传输的异步任务队列,它专注于实时处理,同时也支持任务调度.它的执行单元为任务(task),利用多线程,如Eventlet,gevent等,它们能 ...

  7. Sql 语句拼接 多条件分页查询

    Create PROCEDURE [dbo].[Proc_B2B_GetBatchMainPaging] @StationNo AS varchar() , --m @StationName AS v ...

  8. WPF TabControl Unload俩次的解决方案

    WPF中,有些控件会多次触发Unload,有点莫名其妙~ Unload的多次触发 TabControl的内容,我是这么设置的: 在TabItem的CacheSettingView中,监听Loaded/ ...

  9. Redis面试点

      redis的数据结构有那些 字符串 String 字典:Hash 列表:List 集合:set 有序集合:sortedSet 如果大量的key设置在同一时间过期,一般需要注意什么 大量的key过期 ...

  10. 从URL到看到网页的过程

    从我们输入URL并按下回车键到看到网页结果之间发生了什么?换句话说,一张网页,要经历怎样的过程,才能抵达用户面前?下面来从一些细节上面尝试一下探寻里面的秘密. 前言:键盘与硬件中断 说到输入URL,当 ...