2017-11-13 8 views
0

ハイブのSTRUCTSのARRAYを選択する際に問題があります。ハイブSTRETSのARRAYを作成するSELECTステートメント

私のソース・テーブルは次のようになります。

+-------------+--+ 
| field | 
+-------------+--+ 
| id   | 
| fieldid  | 
| fieldlabel | 
| fieldtype | 
| answer_id | 
| unitname | 
+-------------+--+ 

これは、idは、調査のIDである調査データ、で、真ん中の4つのフィールドが応答データであり、のunitNameは、そのビジネスユニットであり、調査はに関係しています。

調査IDごとにすべての回答の構造体の配列を作成する必要があります。私はこれが働くだろうと思ったが、それはしていません:

select id, 
array( 
    named_struct(
     "field_id", 
     fieldid, 
     "field_label", 
     fieldlabel, 
     "field_type", 
     fieldtype, 
     "answer_id", 
     answer_id,)) as answers, 
unitname 
from new_answers; 

何を返すことは、このようなことの答えのための1つの構造体の配列として各調査の回答(FIELD_ID)、次のとおりです。

id | answers | unitname 
1 | [{"field_id":175877,"field_label":"Comment","field_type":"COMMENT","answer_id":8990947803}] | Location1 
2 | [{"field_id":47824,"field_label":"Language","field_type":"MULTIPLE_CHOICE","answer_id":8990950069}] | Location2 
2 | [{"field_id":48187,"field_label":"Language Type","field_type":"MULTIPLE_CHOICE","answer_id":8990950070}] | Location2 
2 | [{"field_id":47829,"field_label":"Trans #","field_type":"TEXT","answer_id":8990950071}] | Location2 

しかし、私は検索と検索が、私は見つけることだすべての答えは、INSERT INTOを使用して行う必要があるように見える.... VALUES()クエリまし

id | answers | unitname  
1 | [{"field_id":175877,"field_label":"Comment","field_type":"COMMENT","answer_id":8990947803}] | Location1 
2 | [{"field_id":47824,"field_label":"Language","field_type":"MULTIPLE_CHOICE","answer_id":8990950069}, 
    {"field_id":48187,"field_label":"Language Type","field_type":"MULTIPLE_CHOICE","answer_id":8990950070}, 
    {"field_id":47829,"field_label":"Trans #","field_type":"TEXT","answer_id":8990950071}] | Location2 

:私はこれをであることを得るために必要なもの。私はすでにテーブル構造を持っています。私はちょうどARRAYをARRAYまで上げることができません。

ご協力いただければ幸いです。

再生目的のために

、必要であれば:あなたが探しているように見える

CREATE TABLE `new_answers`( 
`id` bigint, 
`fieldid` bigint, 
`fieldlabel` string, 
`fieldtype` string, 
`answer_id` bigint, 
`unitname` string) 

INSERT INTO new_answers VALUES 
(1,175877,"Comment","COMMENT",8990947803,"Location1"), 
(2,47824,"Language","MULTIPLE_CHOICE",8990950069,"Location2"), 
(2,48187,"Language Type","MULTIPLE_CHOICE",8990950070,"Location2"), 
(2,47829,"Trans #","TEXT",8990950071,"Location2"); 

答えて

0

機能は配列に構造体を収集することです。 Hiveには、collect_setとcollect_listのようなものを配列に集める2つの関数があります。ただし、これらの関数は基本型の配列を作成するためにのみ機能します。

brickhouseプロジェクト(https://github.com/klout/brickhouse/wiki/Downloads)用の瓶には、複合型を収集する機能を含む多くの機能があります。

add jar hdfs://path/to/your/jars/brickhouse-0.6.0.jar 

は、その後、あなたが好きな名前を使用して collect機能を追加することができます。

create temporary function collect_struct as 'brickhouse.udf.collect.CollectUDAF'; 

次のクエリ:

id answers unitname 
1 [{"field_id":175877,"field_label":"Comment","field_type":"COMMENT","answer_id":8990947803}] Location1 
2 [{"field_id":47824,"field_label":"Language","field_type":"MULTIPLE_CHOICE","answer_id":8990950069},{"field_id":48187,"field_label":"Language Type","field_type":"MULTIPLE_CHOICE","answer_id":8990950070},{"field_id":47829,"field_label":"Trans #","field_type":"TEXT","answer_id":8990950071}] Location2 
+0

ありがとう:

select id , collect_struct( named_struct( "field_id", fieldid, "field_label", fieldlabel, "field_type", fieldtype, "answer_id", answer_id)) as answers , unitname from new_answers group by id, unitname ; 

は、次の結果を提供します、@Gabe。それがうまくいくので、私は答えを受け入れています。その環境でのセキュリティは問題だったので、ブリックハウスジャーを注入できたかどうかはわかりません。一方で、私が質問したときに私が働いていた会社ではもう働きません。 –

関連する問題