用Hive实现wordcount

一、实验内容:用hive实现wordcount

二、实验步骤:

1.准备数据
首先利用vi编辑器,编写一个word.txt文件,内容如下图所示:
用Hive实现wordcount_第1张图片

2.启动并创建测试数据库,命名test,输入如下命令,如图所示

bin/hive
create database test;

用Hive实现wordcount_第2张图片
3.在test数据库中创建表wordcount

create table wordcount(rowdata string);

用Hive实现wordcount_第3张图片
4.加载数据
代码如下

load data local inpath '/home/hadoop/word.txt' into table wordcount;

在这里插入图片描述
5. SQL编程实现WordCount
代码如下:

select word ,count(1) cnt 
from wordcount lateral view explode(split(rowdata," ")) alaistable as  word
 group by word 
 order by cnt ;

用Hive实现wordcount_第4张图片

三、实验结果

如图所示
用Hive实现wordcount_第5张图片

你可能感兴趣的:(hadoop)