**
舆情热点分析项目流程
**
数据导入hive表
1.拿到原始爬虫数据(.zip格式)
ls weibo/*.zip | xargs -n1 unzip -d /data1/corpus/weibo_unzip
2.新建weibo_origin,把数据导入weibo_origin
新建weibo_origin表
create table weibo_origin(
mid string,
retweeted_status_mid string,
uid string,
retweeted_uid string,
source string,
image string,
text string,
geo string,
created_at string,
deleted_last_seen string,
permission_denied string
)
comment ‘weibo content table’
partitioned by (week_seq string comment ‘the week sequence’)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘,’ LINES TERMINATED BY ‘\n’
STORED AS textfile;
新建weibo_product表
CREATE TABLE weibo_product(
mid string,
retweeted_status_mid string,
uid string,
retweeted_uid string,
source string,
image string,
text string,
geo string,
created_at string,
deleted_last_seen string,
permission_denied string
)
comment 'weibo content table'
partitioned by (week_seq string comment 'the week sequence')
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
STORED AS rcfile
textfile是Hive默认格式,数据不做压缩,磁盘开销大,数据解析开销大。可结合Gzip、Bzip2、Snappy等使用(系统自动检查,执行查询时自动解压),但使用这种方式,hive不会对数据进行切分,从而无法对数据进行并行操作。
RCFile是Hive推出的一种专门面向列的数据格式。 它遵循“先按列划分,再垂直划分”的设计理念。当查询过程中,针对它并不关心的列时,它会在IO上跳过这些列。需要说明的是,RCFile在map阶段从 远端拷贝仍然是拷贝整个数据块,并且拷贝到本地目录后RCFile并不是真正直接跳过不需要的列,并跳到需要读取的列, 而是通过扫描每一个row group的头部定义来实现的,但是在整个HDFS Block 级别的头部并没有定义每个列从哪个row group起始到哪个row group结束。所以在读取所有列的情况下,RCFile的性能反而没有SequenceFile高。
新建脚本load_to_weibo_origin.sh,把数据导入weibo_origin表
#! /bin/sh
#设置zip源文件所在的本地数据目录
parent_zip_dir= /data1/corpus/weibo_unzip
#/将分区值转换成shell数组
partition_name_array=`ls $parent_zip_dir | xargs -n1 echo | cut -d . -f1`
#设置数据库名称
db_name=wqdb
#设置数据表名称
table_name=weibo_origin
#遍历每个分区数据,加载到对应的hive分区表当中
for one_partition in $partition_name_array; do
#打印当前正在处理的分区
echo $one_partition
#正式执行导入代码
hive -e "
use $db_name;
LOAD DATA LOCAL INPATH '$parent_zip_dir/$one_partition.csv' OVERWRITE INTO
TABLE $table_name PARTITION (week_seq='$one_partition');
"
#break
done
#脚本执行完成
3.数据过滤把表头去掉,新建weibo_product,去掉表头数据导入weibo_product
数据清洗,去掉表头
from weibo_origin
insert overwrite table weibo_product partition(week_seq) select * where mid!='mid';
;
数据清洗,得到有用的数据
1.ansj分词器创建UDF函数
Ansj 分词器应用集成,maven导入依赖jar包,pom.xml修改如下:
<!-- ansj依赖jar配置 ==start -->
<dependency>
<groupId>org.ansj</groupId>
<artifactId>ansj_seg</artifactId>
<version>5.1.1</version>
</dependency>
<!-- ansj依赖jar配置 ==end -->
Ansj分词UDF编码
public class CwsUDF extends UDF {
public String evaluate(String input) {
// 采用nlp分词,具备(用户自定义词典/数字识别/人名识别/机构名识别/新词发现)功能
Result result = NlpAnalysis.parse(input);
//如果处理结果为null,则直接返回一个null即可
if (result == null || result.getTerms() == null) {
return null;
}
// 将分词结果集合返回给变量itemList
List<Term> termList = result.getTerms();
// 存储每次分词完成后的词序列集合,词之间以'\001'分隔
StringBuilder stringBuilder = new StringBuilder();
// 循环记数器,当counter>0的时候,每次添加元素前先添加分隔符
int counter = 0;
// 遍历集合,加入结果集中
for (Term term : termList) {
if (counter > 0) {
stringBuilder.append('\001');
}
// 只要分词的名字结果,不要词性部分
stringBuilder.ap