Hive中的字数统计程序

编程入门 行业动态 更新时间:2024-10-20 13:44:02
本文介绍了Hive中的字数统计程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在学习Hive。令人惊讶的是,我找不到如何编写简单的字数统计工作的例子。是否正确?

假设我有一个输入文件 input.tsv :

hello,world 这是一个输入文件示例

我在Python中创建了一个分隔符,将每一行转换为单词:

import sys $ b.b 为sys.stdin中的行:为line.split()中的单词:打印字

然后在我的Hive脚本中有以下内容:

CREATE TABLE input (line STRING); LOAD DATA LOCAL INPATH'input.tsv'OVERWRITE INTO TABLE input; - 用于存放单词的临时表... CREATE TABLE单词(单词STRING); 添加文件splitter.py; INSERT OVERWRITE TABLE单词 SELECT TRANSFORM(文本)使用'python splitter.py' AS字 FROM input; SELECT word,count(*)AS FROM FROM GROUP GROUP BY word;

我不确定我是否错过了某些东西,或者确实如此复杂。 (特别是,我是否需要临时的 words 表,并且是否需要编写外部分隔符函数?)

SELECT word,COUNT

(*)FROM输入LATERAL VIEW explode(split(text,''))lTable as word GROUP BY word;

我使用横向视图来启用表值函数(explode)来自分割函数并为每个值输出一个新行。在实践中,我使用UDF来封装IBM的ICU4J分词器。我通常不使用转换脚本,并使用UDF来处理所有事情。你不需要临时单词表。

I'm trying to learn Hive. Surprisingly, I can't find an example of how to write a simple word count job. Is the following correct?

Let's say I have an input file input.tsv:

hello, world this is an example input file

I create a splitter in Python to turn each line into words:

import sys for line in sys.stdin: for word in line.split(): print word

And then I have the following in my Hive script:

CREATE TABLE input (line STRING); LOAD DATA LOCAL INPATH 'input.tsv' OVERWRITE INTO TABLE input; -- temporary table to hold words... CREATE TABLE words (word STRING); add file splitter.py; INSERT OVERWRITE TABLE words SELECT TRANSFORM(text) USING 'python splitter.py' AS word FROM input; SELECT word, count(*) AS count FROM words GROUP BY word;

I'm not sure if I'm missing something, or if it really is this complicated. (In particular, do I need the temporary words table, and do I need to write the external splitter function?)

解决方案

If you want a simple one see the following:

SELECT word, COUNT(*) FROM input LATERAL VIEW explode(split(text, ' ')) lTable as word GROUP BY word;

I use a lateral view to enable the use of a table valued function (explode) which takes the list that comes out of split function and outputs a new row for every value. In practice I use a UDF that wraps IBM's ICU4J word breaker. I generally don't use transform scripts and use UDFs for everything. You don't need a temporary words table.

更多推荐

Hive中的字数统计程序

本文发布于:2023-11-11 07:12:00,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字数   程序   Hive

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!