2017-02-20 40 views
1

我有一張桌子。其中一列是array<string>。我嘗試在該表上運行查詢,然後將數據加載到文件中。在配置單元中使用數組和集合

這是查詢

Select concat(key, '|' 
    , code, '|' 
    , round(sum(amt), 4), '|' 
    , count(*) 
    , collect_set(comment)) 
from test_agg 
where TIME_KEY = '2017-02-19' 
group by key, code; 

但得到一個錯誤

FAILED: UDFArgumentTypeException Only primitive type arguments are accepted but array<string> was passed as parameter 1. 

我明白,我不能傳遞功能的array<string>,但我能做些什麼?

commentarray<string>

類型的列這是我如何運行它。

hive -f CALC_FILE.sql > 20170220.txt 
+0

附: - 獲得一個快速的好答案的最好方法是提供'show create table'和一個數據樣本。 –

回答

2

使用concat_wscomment數組轉換爲字符串,拼接的collect_set結果比

select  concat_ws 
      (
       '|' 
       ,key 
       ,code 
       ,round(sum(amt),4) 
       ,count(*) 
       ,concat_ws('<<<>>>',collect_set(ws_concat('~~~',comment))) 
      ) 

from  test_agg 

where  time_key = '2017-02-19' 

group by key 
      ,code 
; 
+0

這很有用,但沒有解決問題。問題是'comment'是數組。它是低音的'collect_set(collect_set(some_column))'。所以錯誤仍然彈出。我想知道是否有一種將'collect_set(some_column)'拆分爲字符串的方法,以便我可以再次收集這些值。或者也許有一種方法來分割字符串val1,val2,這樣當通過鍵和代碼進行分組時,我只能得到單值。 –

+0

查看編輯答案(ws_concat) –

+0

P.s.你可能會考慮使用'collect_list'而不是'collect_set' –

0

嘗試使用它串聯到其餘列:

Select concat(key, '|' 
    , code, '|' 
    , round(sum(amt), 4), '|' 
    , count(*) 
    , collect_set(comment[0])) 
from test_agg 
where TIME_KEY = '2017-02-19' 
group by key, code; 
相關問題