热点搜索词滑动统计
每隔60秒钟,统计最近300秒钟的搜索词的搜索频次,并打印出排名最靠前的5个搜索词以及出现次数。(60秒为数据更新频率,300秒为更新数据的时间范围。)
普通SparkStreaming处理方式,如果将时间间隔设置成300s,无法每隔60s输出一次结果;如果将时间间隔设置成300s,同时使用updatebyKeyState,那么统计的是持续的累加结果,无法做到统计300s之内的结果,此时就需要使用滑动窗口来实现。
**流程:用户行为日志+flume+kafka+sparkstreaming+mysql**
1.0
启动zookeeper:
master,slave1,slave2
启动kafka集群
./bin/kafka-server-start.sh config/server.properties
创建kafka topic:topic_user_watch
./bin/kafka-topics.sh --create --zookeeper localhost:2181 -replication-factor 3 --partitions 5 -topic topic_user_watch
1.1
模拟用户行为日志
将用户行为数据逐条按随机时间写入用户行为日志:
user_watch_log_os.py
代码:
from time import sleep
import random
import os
with open('./data//user_watch_pref.sml','r') as f:
lines = f.read().strip().split('\n')
for line in lines:
time1 = random.randint(0, 10)
print(line)
#new_line = ",".join(line.split('\001'))
#print(new_line)
os.system("ec