需统计的单词
hello hadoop hive spark
java python php c hello
java hadoop
hello java java
需创建的表
--创建表wc,用来存储单词,是外部表,上面的数据在hdfs的目录位置为/root/wc/wc
create external table wc(
line string
)
location '/root/wc/'
--创建表wc_result,词表用来存放单词统计结果
create table wc_result(
wc string,
count int
)
单词统计
--将wc表切割成数组
select split(line,' ') from wc;
--将wc表切割成一个一个字符
select explode(split(line,' ')) from wc;
--对单词切割后存入结果表wc_result中的两种方式
--第一种
from (select explode(split(line,' ')) as word from wc) t
insert into table wc_result
select t.word,count(t.word) group by t.word
--第二种
insert into table wc_result
select t.word,count(t.word) from (select explode(split(line,' ')) as word from wc) t group by t.word
参考文档:https://blog.youkuaiyun.com/qq_35022142/article/details/79801888