使用Spark计算PV、UV

日志字段格式:

id,ip,url,ref,cookie,time_stamp

把日志文件放到HDFS。仅取了1000行。

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. hadoop fs -put 1000_log hdfs://localhost:9000/user/root/input  

直接在Scala Shell中读取文件并计算PV。

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. scala> val textFile = sc.textFile("hdfs://localhost:9000/user/root/input/1000_log")  
  2. scala> val textRDD = textFile.map(_.split("\t")).filter(_.length == 6)  
  3. scala> val result = textRDD.map(w => ((new java.net.URL(w(2))).getHost,1)).reduceByKey(_ + _).map(item => item.swap).sortByKey(false).map(item => item.swap)  
  4. scala> result.saveAsTextFile("hdfs://localhost:9000/user/root/out8.txt")  
从HDFS上取回结果:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. hadoop fs -get hdfs://localhost:9000/user/root/out8.txt  
查看结果:
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. $ more out8.txt/part-00000   
  2. (www.j1.com,126)  
  3. (tieba.baidu.com,65)  
  4. (113.105.174.5,60)  
  5. (www.baidu.com,54)  
  6. (mp.weixin.qq.com,46)  
  7. (119.147.106.188,40)  
  8. (bbs.caoav.net,31)  
  9. (i.ifeng.com,19)  
  10. (www.amazon.de,18)  
  11. (m.zhiyoula.com,18)  
  12. (www.360doc.com,16)  
  13. (br.pps.tv.iqiyi.com,14)  
  14. (www.1905.com,14)  
  15. (xa.btfs.ftn.qq.com,14)  


如果是生成 .snappy压缩格式的文件,则可以按如下方法重定向到本地文本文件。
hadoop fs -text part-r-00001.snappy > filename.txt


下面对同一日志文件计算UV(Unique View)。

UV一般认为不同cookie的访问则算不同的独立用户。

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package org.asiainfo  
  2.   
  3. import org.apache.spark.SparkConf  
  4. import org.apache.spark.SparkContext  
  5. import org.apache.spark.SparkContext._  
  6.   
  7. /**  
  8.  * @author:zhaohf@asiainfo.com  
  9.  * @date:2015年1月27日 下午5:54:39  
  10.  * @Description: TODO  
  11.  */  
  12. object UniqueViewCount {  
  13.   
  14.   def main(args: Array[String]): Unit = {  
  15.     if(args.length < 4){  
  16.       System.err.println("Usage:<input_file> <url_column_index> <output_file>")  
  17.       System.exit(1)  
  18.     }  
  19.     val conf = new SparkConf().setAppName("UniqueViewApp")  
  20.     val sc = new SparkContext(conf)  
  21.     val url_index = args(1).toInt  
  22.     val cookie_index = args(2).toInt  
  23.     val textRDD = sc.textFile(args(0))  
  24.         .map(_.split("\t"))  
  25.         .map(line => (new java.net.URL(line(url_index)).getHost) + "\t" + line(cookie_index))  
  26.         .distinct()  
  27.         .map(line => (line.split("\t")(0),1))  
  28.         .reduceByKey(_ + _)  
  29.         .map(item => item.swap)  
  30.         .sortByKey(false)  
  31.         .map(item => item.swap)  
  32.     textRDD.saveAsTextFile(args(3))  
  33.   }  
  34.   
  35. }  


sbt package 编译打包。
生成jar文件,提交spark应用。

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. spark-submit --class main.UniqueViewCount target/scala-2.11/spark_2.11-1.0.jar hdfs://localhost:9000/user/root/intput/1000_log 2 4 hdfs://localhost:9000/user/root/output  
结果:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. more result.txt  
  2. (bbs.caoav.net,31)  
  3. (www.baidu.com,28)  
  4. (www.amazon.de,15)  
  5. (m.zhiyoula.com,15)  
  6. (www.360doc.com,14)  
  7. (m.sohu.com,11)  
  8. (mp.weixin.qq.com,11)  
  9. (www.kaixin001.com,10)  
  10. (www.zhiyoula.com,7)  
  11. (lolbox.duowan.com,7)  

下面用shell来验证正确性:

先用python解析出url中的host:

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #!/usr/bin/python  
  2. from urlparse import urlparse  
  3. import sys  
  4. with open(sys.argv[1],'r') as f:  
  5.         for line in f.readlines():  
  6.                 splits = line.split('\t')  
  7.                 url,cookie = urlparse(splits[2]).netloc,splits[4]  
  8.                 print url + '\t' + cookie  

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. $ python check.py 1000_log > 1000_log_pre  
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. $ cat 1000_log_pre | sort | uniq | awk -F '\t' '{print $1}' | sort | uniq -c | sort -nr -k1|  head  
  2.     31 bbs.caoav.net  
  3.     28 www.baidu.com  
  4.     15 www.amazon.de  
  5.     15 m.zhiyoula.com  
  6.     14 www.360doc.com  
  7.     11 m.sohu.com  
  8.     11 mp.weixin.qq.com  
  9.     10 www.kaixin001.com  
  10.      7 www.zhiyoula.com  
结果正确!
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值