-1

创建name_tab:

CREATE TABLE name_tab (country string,
new_item ARRAY<STRUCT<ngram:array<string>,
estfrequency:double>>);

插入语句:

INSERT OVERWRITE TABLE name_tab
SELECT country, ngrams(sentences(var2),3,100) as word_map
FROM bdd 
GROUP BY country;

创建 name_tab_new :

CREATE TABLE name_tab_new (country string, ngram1 string, ngram2 string,  ngram3 string, estfrequency double);

插入语句:

INSERT OVERWRITE TABLE name_tab_new
SELECT country , X.ngram[0], X.ngram[1], X.ngram[2], X.estfrequency
FROM name_tab
LATERAL VIEW explode(new_item) Z as X;

Hive 中的这些请求有效。它按国家/地区创建 ngram。{aa, bb, cc}问题:对于一个国家来说,这个 ngram和这个 ngram是有区别的{bb, aa, cc}

我想要一个单词命令无关紧要的解决方案。{aa, bb, cc}对于一个国家,我希望和之间没有差异{bb, aa, cc}。我只想要其中一个。

非常感谢

结果示例:

*英格兰,面包,三明治,果汁,120

英格兰,桌子,椅子,工具,54

英格兰,三明治,面包,果汁,32

意大利,海,罗马,Coliseo,47*

其实,我想要的是:

*英格兰,面包,三明治,果汁,152

英格兰,桌子,椅子,工具,54

意大利,海,罗马,Coliseo,47*

我希望 ngrams 函数中有一个选项可以不考虑订单。

在表 bdd 中,变量“var2”是由空格分隔的几个单词的列表。

在表 name_tab 中,我们有:

英格兰第一行 ,{"ngram":["bread","sandwich","juice"],"estfrequency":120.0}, {"ngram":["desk","chair","tool"]," estfrequency":54.0}, {"ngram":["sandwich","bread","juice"],"estfrequency":32.0}

意大利二线 ,{"ngram":["sea","Roma","Coliseo"],"estfrequency":47.0}

4

1 回答 1

0

演示

with t as (select 'a  b a c c a b b a a a a c c b c a b c a b' as mycol)

select      sort_array(e.ngram) as ngram
           ,sum(e.estfrequency) as estfrequency

from       (select  explode(ngrams(sentences(mycol),2,1000)) e

            from    t
            ) t

group by    sort_array(e.ngram)
;

+-----------+--------------+
|   ngram   | estfrequency |
+-----------+--------------+
| ["a","a"] | 3.0          |
| ["a","b"] | 6.0          |
| ["a","c"] | 5.0          |
| ["b","b"] | 1.0          |
| ["b","c"] | 3.0          |
| ["c","c"] | 2.0          |
+-----------+--------------+
于 2017-08-31T09:21:08.397 回答