用hive做一个简单的单词统计

1,开始学习Hadoop的时候为了练习单词统计,排序,每次都得用java编写MapReduce程序,常常一个单词统计的代码都得写很久,所以我就提前练习了一下hive语法,做一个单词的统计。
2,首先本地构造数据,数据内容如下:

[hadoop@master ~]$ cat count.txt
hello,world,welcome
hello,welcome
world,hello,hi
[hadoop@master ~]$ 

3,然后在hive上创建一张表,再将本地数据导入:

hive> create table count(   //创建一张名字为count的表
    > sentence string);
OK
Time taken: 0.092 seconds
hive> load data local inpath '/home/hadoop/count.txt' into table count;   //将本地数据导入到hive数据库中
Loading data to table default.count
Table default.count stats: [numFiles=1, totalSize=49]
OK
Time taken: 0.465 seconds
hive> select * from count;     //查询表的内容
OK
hello,world,welcome
hello,welcome
world,hello,hi
Time taken: 0.09 seconds, Fetched: 3 row(s)

4,利用MapReduce思想,先把每行数据转化为一个数组的形式:

hive> select split(sentence,',') from count;
OK
["hello","world","welcome"]
["hello","welcome"]
["world","hello","hi"]
Time taken: 0.141 seconds, Fetched: 3 row(s)

5,然后将数组的每一个单词拆分出来每行就只有一个单词:

hive> select explode(split(sentence,',')) from count;
OK
hello
world
welcome
hello
welcome
world
hello
hi
Time taken: 0.081 seconds, Fetched: 8 row(s)

6,组合语句写一个单词统计,并排序:

hive> select word ,count(1) as c
    > from (
    > select explode(split(sentence,',')) as word from count
    > ) t group by word
    > order by c desc;

最后跑完MapReduce,结果如下:

hello	3
world	2
welcome	2
hi	1
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值