Hive案例实操入门项目-WordCount

在Hive中实现Wordcount
使用到的函数:

函数名使用方法解释
splitsplit(参数,“分隔符”)用于切分数据,也就是将一串字符串按指定的分隔符切割成了一个数组
explodeexplode(数组)用于打散行的函数(将一行的数据拆分成多行,它的参数必须为map或array)。这个函数常和split()并用
coountcount(需要计数的列)返回多条数据中参数出现的总次数

WordCount

数据Wordcount.txt

world hadoop
dog fish
hadoop hello
spark
hello world
dog fish
hadoop spark
spark world
hello world
dog fish
hadoop
spark

创建数据表

create table wordcount1(line string);

导入数据

load data local inpath '/root/wordcount.txt' into table wordcount1

当前表中的数据
在这里插入图片描述

先把数据用split函数空格切割分开
select split(line," ") from wordcount1
在这里插入图片描述

通过函数嵌套配合explode炸裂函数,可以把数组里的内容炸裂成一条一条的数据
select explode(split(line," ")) as c1 from wordcount1
在这里插入图片描述

最后再用分组和计数函数实现
select t.c1,count(t.c1)
from (select explode(split(line," ")) as c1 from wordcount1) t
group by t.c1;
在这里插入图片描述

最后再保存到结果表中
方式1:自动生成表、导入数据

create table wordcount_result1 as 
select t.c1,count(t.c1)
from (select explode(split(line," ")) as c1 from wordcount1) t
group by t.c1;

在这里插入图片描述

方式二:手动建表导入数据
先创建表:

create table wordcount_result2(
	word string,
	num int
);

插入数据

insert into wordcount_result2 
select t.c1,count(t.c1)
from (select explode(split(line," ")) as c1 from wordcount1) t
group by t.c1;

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值