[Hive_add_6] Hive 实现 Word Count


 0. 说明

  Hive 通过 explode()函数 和 split()函数 实现 WordConut

 

 


 1. Hive 实现 Word Count 方式一

  1.1 思路

  将每一行文本变为 Array 数组的一个元素

  再通过 collection items terminated by ' ' 完成转换单行文本

  最后通过表生成函数 explode 分裂 array 数组中的元素变成多行

 

  1.2 实现

  1. 创建表 wc

create table wc(line array<string>) row format delimited collection items terminated by ' ';

 

  2. 加载数据

load data local inpath '/home/centos/files/wc.txt' into table wc;

 

  3. 编写 SQL 语句

select word, count(*) as count from (select explode(line) word from wc ) a group by word order by count desc;

 

 


 

2. Hive 实现 Word Count 方式二

  2.1 思路

  将一行文本加载为 String ,通过 split 结合正则表达书进行拆分

 

  2.2 实现

  1. 创建表 wc2

create table wc2(line string) row format delimited;

 

  2. 加载数据

load data local inpath '/home/centos/files/wc.txt' into table wc2;

 

  3. 编写 SQL 语句

select word,count(*) as count from (select explode(split(line,' ')) word from wc2 ) a group by word order by count desc;

 


 

posted @ 2019-01-05 11:50  山间一棵松  阅读(221)  评论(0编辑  收藏  举报