如何在蜂巢中进行字数统计

How to do word count in hive

我有一个tabletable_a喜欢

query
------
apple
no fruit
this is 
not a number

我想编写配置单元查询以根据 space 点赞

获取字数
query           count
------      ------------
apple           1
no fruit        2
this is         2
not a  number   3

一种方法是拆分成一个数组并计算元素:

select t.*, size(split(query, ' '))

如果您想将多个空格计为一个分隔符:

select t.*, size(split(query, '[ ]+'))

您需要拆分列内容,例如

select  *
       ,size(split(query, '[\s]+'))  as count
from table_a

使用空格 \s shorthand 字符不仅可以忽略多个空格,还可以忽略制表符等。