ITPub博客

首页 > 大数据 > 数据分析 > HIVE实现wordcount过程

HIVE实现wordcount过程

原创 数据分析 作者:loveheping 时间:2017-12-28 14:27:51 0 删除 编辑
1、建表并关联数据:
进入hive命令行:
hive
执行:
create external table wordcounts(line string) row format delimited fields terminated by '\n' stored as textfile location '/input/wordcount';

2、创建ruozedata.txt文件,并上传hdfs中的/input/wordcount目录下。
vi ruozedata.txt
hello,ruoze
hello,jepsondb
hello,www.ruozedata.com
hi,man
hi,gril

上传命令:hdfs dfs -put ruozedata.txt /input/wordcount

3、进入HIVE,查询是否已经将数据关联到表中
select * from wordcounts;

4、进入HIVE,进行拆分,把每行数据拆分成单词,这里需要用到一个hive的内置表生成函数(UDTF):explode(array),参数是array,其实就是行变多列:
split是拆分函数,与java的split功能一样,这里是按照逗号拆分,再对子查询中的结果进行group by word,执行完hql语句如下:
select word, count(*) from (select explode(split(line, ",")) as word from wordcounts) t group by word;

总结:hive比较简便,对于比较复杂的统计操作可以建一些中间表。

 若泽大数据交流群:671914634 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31511218/viewspace-2149439/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2017-12-13

  • 博文量
    41
  • 访问量
    80861