Spark->预热

数据排序

对输入文件中数据进行排序。输入文件中的每行内容均为一个数字,即一个数据。
要求在输出中每行有两个间隔的数字,其中,第一个代表原始数据在原始数据集中的位次,第二个代表原始数据

5956
22
650
92
26
54
6
2
32
654
32
15
756
65223

输出:
1 2
2 6
3 15
4 22
5 26
6 32
7 32
8 54
9 92
10 650
11 654
12 756
13 5956
14 65223

val conf = new SparkConf().setMaster("local").setAppName("reduce")
val sc = new SparkContext(conf)
val fil = sc.textFile("d:/local.txt")
//在Linux里只要下面这一句
fil.map(x=>(x.toInt,"")).sortByKey().map(_._1).zipWithIndex().foreach(rs=>{println(rs._2+1+"\t"+rs._1)})

求平均值

对输入文件中数据进行就算学生平均成绩。输入文件中的每行内容均为一个学生的姓名和他相应的成绩,如果有多门学科,则每门学科为一个文件。
要求在输出中每行有两个间隔的数据,其中,第一个代表学生的姓名,第二个代表其平均成绩。

原数据:
张三,88
李四,99
王五,66
赵六,77
张三,78
李四,89
王五,96
赵六,67
张三,80
李四,82
王五,84
赵六,86

结果:
张三,82
李四,90
王五,82
赵六,76

//这种写死了,扩展不好;用reduceByKey 直接把成绩求总和,但是长度不好拿了
//foreach(rs=>println(rs._1+"\t"+rs._2/3))
//看这
fil.map(x=>{val words = x.split(",");(words(0),words(1).toInt)}).groupByKey().map(y=>(y._1,y._2.reduce(_+_)/y._2.size)).foreach(println)

求最大最小值

数据用的还是第一题里的
所有的排序都是为了查找,所以直接排序查找就行了
后来想想,排序都懒得排了,直接甩给函数做吧

结果:
max:65223 min: 2


val lst=fil.map(x=>(x.toInt)).collect()
    println("max:"+lst.max+" min:"+lst.min)

求top值

求出海量数据中的最大的前5个

原数据:
1,9819,100,121
2,8918,2000,111
3,2813,1234,22
4,9100,10,1101
5,3210,490,111
6,1298,28,1211
7,1010,281,90
8,1818,9000,20
100,3333,10,100
101,9321,1000,293
102,3881,701,20
103,6791,910,30
104,8888,11,39

结果:
9819
9321
9100
9000
8918

fil.flatMap(_.split(",")).map(x=>(x.toInt,1)).sortByKey(false).map(_._1).collect().take(5).foreach(println)
//if  你想 可以在给他们加个编号 
//1 9819
//2 9321
//3 9100
//4 9000
//5 8918

非结构数据处理

根据tomcat日志计算url访问了情况,具体的url如下, 区别统计GET和POST URL访问
输出:量访问方式、URL、访问量

数据:

196.168.2.1 - - [03/Jul/2014:23:36:38 +0800] “GET /course/detail/3.htm HTTP/1.0” 200 38435 0.038
182.131.89.195 - - [03/Jul/2014:23:37:43 +0800] “GET /html/notes/20140617/888.html HTTP/1.0” 301 - 0.000
196.168.2.1 - - [03/Jul/2014:23:38:27 +0800] “POST /service/notes/addViewTimes_23.htm HTTP/1.0” 200 2 0.003
196.168.2.1 - - [03/Jul/2014:23:39:03 +0800] “GET /html/notes/20140617/779.html HTTP/1.0” 200 69539 0.046
196.168.2.1 - - [03/Jul/2014:23:43:00 +0800] “GET /html/notes/20140318/24.html HTTP/1.0” 200 67171 0.049
196.168.2.1 - - [03/Jul/2014:23:43:59 +0800] “POST /service/notes/addViewTimes_779.htm HTTP/1.0” 200 1 0.003
196.168.2.1 - - [03/Jul/2014:23:45:51 +0800] “GET /html/notes/20140617/888.html HTTP/1.0” 200 70044 0.060
196.168.2.1 - - [03/Jul/2014:23:46:17 +0800] “GET /course/list/73.htm HTTP/1.0” 200 12125 0.010
196.168.2.1 - - [03/Jul/2014:23:46:58 +0800] “GET /html/notes/20140609/542.html HTTP/1.0” 200 94971 0.077
196.168.2.1 - - [03/Jul/2014:23:48:31 +0800] “POST /service/notes/addViewTimes_24.htm HTTP/1.0” 200 2 0.003
196.168.2.1 - - [03/Jul/2014:23:48:34 +0800] “POST /service/notes/addViewTimes_542.htm HTTP/1.0” 200 2 0.003
196.168.2.1 - - [03/Jul/2014:23:49:31 +0800] “GET /notes/index-top-3.htm HTTP/1.0” 200 53494 0.041
196.168.2.1 - - [03/Jul/2014:23:50:55 +0800] “GET /html/notes/20140609/544.html HTTP/1.0” 200 183694 0.076
196.168.2.1 - - [03/Jul/2014:23:53:32 +0800] “POST /service/notes/addViewTimes_544.htm HTTP/1.0” 200 2 0.004
196.168.2.1 - - [03/Jul/2014:23:54:53 +0800] “GET /service/notes/addViewTimes_900.htm HTTP/1.0” 200 151770 0.054
196.168.2.1 - - [03/Jul/2014:23:57:42 +0800] “GET /html/notes/20140620/872.html HTTP/1.0” 200 52373 0.034
196.168.2.1 - - [03/Jul/2014:23:58:17 +0800] “POST /service/notes/addViewTimes_900.htm HTTP/1.0” 200 2 0.003
196.168.2.1 - - [03/Jul/2014:23:58:51 +0800] “GET /html/notes/20140617/888.html HTTP/1.0” 200 70044 0.057
186.76.76.76 - - [03/Jul/2014:23:48:34 +0800] “POST /service/notes/addViewTimes_542.htm HTTP/1.0” 200 2 0.003
186.76.76.76 - - [03/Jul/2014:23:46:17 +0800] “GET /course/list/73.htm HTTP/1.0” 200 12125 0.010
8.8.8.8 - - [03/Jul/2014:23:46:58 +0800] “GET /html/notes/20140609/542.html HTTP/1.0” 200 94971 0.077

结果:
(POST /service/notes/addViewTimes_779.htm,1)
(POST /service/notes/addViewTimes_24.htm,1)
(GET /service/notes/addViewTimes_900.htm,1)
(POST /service/notes/addViewTimes_900.htm,1)
(GET /notes/index-top-3.htm,1)
(GET /course/detail/3.htm,1)
(GET /html/notes/20140318/24.html,1)
(GET /course/list/73.htm,2)
(GET /html/notes/20140617/779.html,1)
(GET /html/notes/20140609/544.html,1)
(POST /service/notes/addViewTimes_542.htm,2)
(GET /html/notes/20140620/872.html,1)
(POST /service/notes/addViewTimes_544.htm,1)
(GET /html/notes/20140609/542.html,2)
(POST /service/notes/addViewTimes_23.htm,1)
(GET /html/notes/20140617/888.html,3)

//过滤器过滤一下 里面数据 该是有空的
fil.filter(_.length>0).map(x=>{val lst=x.trim.split("“")(1).split(" ");(lst(0)+" "+lst(1),1)}).reduceByKey(_+_).foreach(println)

倒排索引统计新增用户

根据记录 统计出 新增用户的个数

数据:
2017-01-01 a
2017-01-01 b
2017-01-01 c
2017-01-02 a
2017-01-02 b
2017-01-02 d
2017-01-03 b
2017-01-03 e
2017-01-03 f

结果:
(2017-01-03,2)
(2017-01-02,1)
(2017-01-01,3)


//题 有点难理解 过程不难 
fil.map(x=>{val lst=x.split("\\s+");(lst(1),lst(0))}).groupByKey().map(y=>(y._2.toList(0),1)).reduceByKey(_+_).foreach(println)

就到这把!

猜你喜欢

转载自blog.csdn.net/qq_42898087/article/details/83116434
今日推荐