storm热力分布图项目(三)
一、编写python脚本产生日志编写一个python脚本向/home/hadoop/app/data/logs/test.log写入对应的日志#!/usr/bin/env python# -*- coding:utf-8 -*-#@Time: 2019/4/29 12:50#@Author: qianliu#@File: GenerateImooc.py.pyimport ...
·
一、编写python脚本产生日志
编写一个python脚本向/home/hadoop/app/data/logs/test.log写入对应的日志
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#@Time : 2019/4/29 12:50
#@Author: qianliu
#@File : GenerateImooc.py.py
import time
import random
infos = [
"116.397026,39.918058",
"116.389275,39.925818",
"116.287444,39.810742",
"116.481707,39.940089",
"116.410588,39.880172",
"116.394816,39.91181",
"116.416002,39.952917"
]
phones = [
"13111111111","13222222222","13333333333",
"13444444444","13555555555","13666666666",
"13777777777","13888888888","13999999999"
]
# 产生一个随机的info
def sample_infos():
return random.sample(infos,1)[0]
# 产生一个随机的电话
def sample_phone():
return random.sample(phones,1)[0]
def generate_log(count=3):
time_str = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
f = open("/home/hadoop/app/data/logs/test.log","a+")
while count >= 1:
query_log = "{phone}\t{info}\t[{local_time}]".\
format( phone = sample_phone(), local_time = time_str ,info = sample_infos())
print(query_log)
f.write(query_log+"\n")
count = count - 1
if __name__ == '__main__':
generate_log(10)
每次运行python脚本就可以从storm接收到一条条日志信息
二、解析字符串
在LogPoccessBolt类的execute方法中加入解析字符串的代码:
/*
* 进来的字符串形如:13999999999 116.410588,39.880172 [2019-29-04/29/19 13:29:33]
* 进行解析
* */
String[] splits = value.split("\t");
String phone = splits[0];
String[] local = splits[1].split(",");
String longitude = local[0]; //经度
String latitude = local[1]; //纬度
long time = DataUtils.getInstance().getTime(splits[2]); //[2019-29-04/29/19 13:29:33]格式的时间转换成一个时间戳
System.out.println(phone+"\t"+longitude+"\t"+latitude+"\t"+time);
下面是新建的工具类,用于解析时间:
package com.qianliu.Utils;
import org.apache.logging.log4j.core.util.datetime.FastDateFormat;
public class DataUtils {
private DataUtils(){}
private static DataUtils instance;
public static DataUtils getInstance(){
if(instance == null){
instance = new DataUtils();
}
return instance;
}
//将"yyyy-MM-dd HH:mm:ss"格式的字符串格式化为1556522469000这样的时间戳
FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
public long getTime(String time) throws Exception{
return format.parse(time.substring(1,time.length()-1)).getTime();
}
}
测试是否打通所有程序:
更多推荐
已为社区贡献2条内容
所有评论(0)