为了学习多系统配合处理数据,设计了如下的一个练习:
python获取mysql数据库中的网站内容,提出热词生成json格式热词数据,定时任务执行提取并发布到网站,另外一台服务器定期执行java springboot程序,rest获取热词数据,写入数据库,另外提供一个api接口,从数据库读出数据,以json的方式提供api接口,一个html网页的通过jquery的ajax获取数据提供给echarts展示在网页上。大概如下图:
一、 python3获取数据写入网站
pip3 install jieba
pip3 install mysqlclient
建立 hotword.py
import jieba.analyse
import MySQLdb
import json
def is_chinese(uchar):
"""判断一个unicode是否是汉字"""
if (uchar >= u'\u4e00' and uchar <= u'\u9fa5'):
return True
else:
return False
def format_str(content):
#content = unicode(content,'utf-8')
content_str = ''
for i in content:
if is_chinese(i):
content_str = content_str+i
return content_str
targetTxt='/var/www/hotword.html'
db = MySQLdb.connect(host="localhost", user="root", passwd="root", db="websitedb",charset='utf8')
conn = db.cursor()
conn.execute("""select content from news where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(createtime);""")
mycontent = str(conn.fetchall())
mycontent = format_str(mycontent)
#print(mycontent)
try:
jieba.analyse.set_stop_words('/root/wordcloud/stopword.txt')
tags = jieba.analyse.extract_tags(mycontent, topK=100, withWeight=True)
jsonList=[]
for v,n in tags:
bitem={}
bitem["name"]=v
bitem["value"]=n
jsonList.append(bitem)
jsonArr = json.dumps(jsonList, ensure_ascii=False)
with open(targetTxt,'w',encoding="gbk") as f:
f.write(str(jsonArr))
#print('写入成功!')
finally:
f.close()
conn.close()
二、linux定期任务执行
crontab -e
0 0 3 * * python3 /root/hotword.py
三、springboot resttemplate 获取网页数据写入mysql
@Service
public class WordcloudServiceImpl implements WordcloudService{
private static final Logger log = LoggerFactory.getLogger("adminLogger");
@Autowired
private RestTemplate restTemplate;
@Autowired
private WordcloudDao wordcloudDao;
@Value(value = "${serverurl.wordcloud}")
private String wordserverurl;
@Override
public void save() {
List
for (HttpMessageConverter> httpMessageConverter : list) {
if(httpMessageConverter instanceof StringHttpMessageConverter) {
((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(Charset.forName("gbk"));
break;
}}
String responseEntity = restTemplate.getForObject(wordserverurl, String.class, "");
if(responseEntity.length()>200) {
List
wordcloudDao.delete();
for(Wordcloud word:listword){
wordcloudDao.save(word);
}
}else {
log.debug(responseEntity);
}
}
}
四、springboot 提供api数据接口
Api(tags = "热词")
@RestController
@RequestMapping("/hotwords")
public class WordcloudController {
@Autowired
private WordcloudDao wordcloudDao;
@GetMapping
@ApiOperation(value = "热词列表")
List
return wordcloudDao.getwordcloud();
}
}
五、ajax 获取热词
$.ajax({
"url" : "/hotwords",
"type" : "get",
"dataType" : "text",
"success" : callBack,
"error" : function() {
alert("错误");
}
});
六、展示数据
需引入js库
定义在ajax的 "success" : callBack,
function callBack(data) {
data = JSON.parse(data);
var chart = echarts.init(document.getElementById('main'));
var option = {
tooltip: {},
series: [ {
type: 'wordCloud',
gridSize: 2,
sizeRange: [12, 50],
rotationRange: [-90, 90],
shape: 'pentagon',
width: 600,
height: 400,
drawOutOfBound: true,
textStyle: {
normal: {
color: function () {
return 'rgb(' + [
Math.round(Math.random() * 160),
Math.round(Math.random() * 160),
Math.round(Math.random() * 160)
].join(',') + ')';
}
},
emphasis: {
shadowBlur: 10,
shadowColor: '#333'
}
},
data: data
} ]
};
chart.setOption(option);
window.onresize = chart.resize;