Hive之WordCount

Hive-SQL练习

通过hive统计一篇文章中WordCount

1. hive创建内部表wc

create table wc (
lie string)
row format delimited fields terminated by '\n';

2. 向wc表中导入hdfs文章的内容

load data inpath '/1.txt' overwrite into table wc;

3. 统计词频

select word,count(1) as word_count 
from
(select explode(split(lie2, " ")) as word   //将文章的每一行(表中的每条记录),按照空格切割为每个元素
from 
(select regexp_replace(lie,'[^a-zA-Z0-9]',' ') as lie2  //过滤掉特殊字符,只保留a-zA-Z0-9,其余全部替换为空格
from wc
) t1
) t
where word REGEXP '[a-zA-Z0-9]'      //过滤掉空格
group by word
order by word_count desc    //按照词频排倒序(desc降序,默认asc升序)
limit 10;

4. 知识点

regexp_replace(source,reg规则,desc)   //字段source内容满足reg规则就替换为desc指定的内容
比如:regexp_replace(123,'^[0-9]',123)  //返回12323



posted @ 2018-07-05 16:30  雪山过客  阅读(1773)  评论(0编辑  收藏  举报