大数据全系列 教程
1869个小节阅读:464.8k
目录
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
数据准备
xxxxxxxxxx
[root@node4 data]# vim wc.txt
hello tom
andy joy
hello rose
hello joy
mark andy
hello tom
andy rose
hello joy
创建原始数据表
xxxxxxxxxx
create table words(line string);
将数据文件wc.txt load到words表中:
xxxxxxxxxx
hive> load data local inpath '/root/data/wc.txt' into table words;
Loading data to table default.words
OK
Time taken: 1.117 seconds
hive> select * from words;
OK
hello tom
andy joy
hello rose
hello joy
mark andy
hello tom
andy rose
hello joy
Time taken: 0.364 seconds, Fetched: 8 row(s)
编写sql语句,先将每行内容按照空格拆分
xxxxxxxxxx
hive> select split(line,' ') from words;
OK
["hello","tom"]
["andy","joy"]
["hello","rose"]
["hello","joy"]
["mark","andy"]
["hello","tom"]
["andy","rose"]
["hello","joy"]
Time taken: 0.352 seconds, Fetched: 8 row(s)
将数组的元素再次进行拆解
xxxxxxxxxx
select explode(split(line,' ')) from words;
执行效果:
xxxxxxxxxx
hive> select explode(split(line,' ')) from words;
OK
hello
tom
andy
joy
hello
rose
hello
joy
......
按照单词进行分组,并统计数量
xxxxxxxxxx
select word,count(word)
from (select explode(split(line,' ')) word from words) tmp
group by word;
创建结果表:
xxxxxxxxxx
create table wc_count(word string,count int);
将统计出来的结果添加结果表中
xxxxxxxxxx
from (select explode(split(line,' ')) word from words) tmp
insert into wc_count
select word,count(word)
group by word;
查询结果表:
xxxxxxxxxx
hive> select * from wc_count;
OK
andy 3
hello 5
joy 3
mark 1
rose 2
tom 2
Time taken: 0.345 seconds, Fetched: 6 row(s)