Hive实现WordCount

1. 创建一个存放源数据的表(外部表)t_words_src, 表中字段line为string类型, 存放一行单词

源数据示列: 

hello,tom
hello,jerry
hello,kitty
hello,world
hello,tom
hive> create external table t_words_src (line string)
row format delimited
fields terminated by '\n'    # 按\n来切分字段, 一行就是一个字段
location '/wc/input';    # 源数据路径为 'hdfs://node1:9000/wc/input' 

hive> select * from t_words;
OK
hello,tom
hello,jerry
hello,kitty
hello,world
hello,tom

2. 创建一个存放所有单词的表t_words, 表中字段word为string类型, 存放单词

hive> create table t_words (word string);
hive> insert into table t_words select explode(split(line,',')) as word from t_words_src;

hive> select * from t_words;
OK
hello
tom
hello
jerry
hello
kitty
hello
world
hello
tom


3. 创建一个存放WordCount结果的表t_wc_result, 表中字段word为string类型, 存放单词, counts为int类型, 存放单词出现次数

hive> create table t_wc_result (word string, counts int);
hive> insert into table t_wc_result select word as word, count(word) as counts from t_words;

hive> select * from t_wc_result;
OK
hello	5
jerry	1
kitty	1
tom	2
world	1

相对MapReduce来说, Hive的HQL版WordCount写起来代码量少很多, 但他们的思想都是一样的

转载于:https://my.oschina.net/u/2503731/blog/665252

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值