hive 实现word count 例子

首先启动hive : bin/hive

创建表:create table wordcount(context string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\n';

 

hive 实现word count 例子

 准备要统计的数据

hive 实现word count 例子

我的文件名叫:hello.txt 

将数据加载到 wordcount 表中 

hive 实现word count 例子

 根据’ ‘切分数据,切分出来的每个单词作为一行 记录到结果表。

  • 创建结果表用来存放单词
  • hive 实现word count 例子

 切分数据,把单词放入words2t表中。

hive 实现word count 例子

context:是wordcount表中的字段名

,:是根据什么切割数据  

wordcount: 数据存放的表名

 

查询 words :select * from words;

hive 实现word count 例子

使用聚合函数count进行统计

select word, count(word)as count from words group by wordr;

hive 实现word count 例子

将结果输出到本地文件:

先将reducer的数量设置为1:

set mapreducer.job.reducers=1;

hive 实现word count 例子

输出到本地:

insert overwrite local directory "/opt/module/datas/wordcount2"
select word, count(word)as count from words group by word;

hive 实现word count 例子

hive 实现word count 例子