0

嗨,我有一个带有结构数组的 avro 模式,我能够将数据保存为 avro。但是在从中检索数据时

array<struct<string, string>>

我无法排成一行。我在单行中获得的所有数据。

这是表定义

CREATE EXTERNAL TABLE meterevents ROW FORMAT SERDE org.apache.hadoop.hive.serde2.avro.AvroSerDe' STORED as INPUTFORMAT org.apache.hadoop.hive.ql.io.avro.AvroContainerInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.avro.AvroContainerOutputFormat' LOCATION '/......' TBLPROPERTIES ('avro.schema.url'='/..../schema.avsc');

蜂巢表结构

nametype                struct<nametypedescription:string,nametypename:string,nametypeauthority:struct<nametypeauthorityname:string,nametypeauthoritydescription:string>>       from deserializer
names                   struct<name:string,nametype:struct<nametypedescription:string,nametypename:string,nametypeauthority:struct<nametypeauthorityname:string,nametypeauthoritydescription:string>>>  from deserializer
enddeviceeventdetails   struct<enddeviceeventdetailsname:string,enddeviceeventdetailsvalue:string>      from deserializer
enddeviceevent          struct<mrid:string,createddatetime:string,issuerid:string,issuertrackingid:string,reason:string,severity:string,userid:string,asset:struct<assetmrid:string,assetnames:array<struct<name:string,nametype:struct<nametypedescription:string,nametypename:string,nametypeauthority:struct<nametypeauthorityname:string,nametypeauthoritydescription:string>>>>>,enddeviceeventdetails:array<struct<enddeviceeventdetailsname:string,enddeviceeventdetailsvalue:string>>,enddeviceeventtype:string,enddeviceeventnames:array<struct<name:string,nametype:struct<nametypedescription:string,nametypename:string,nametypeauthority:struct<nametypeauthorityname:string,nametypeauthoritydescription:string>>>>,status:struct<statusdatetime:string,statusreason:string,statusremark:string,statusvalue:string>,usagepoint:struct<usagepointmrid:string,usagepointnames:array<struct<name:string,nametype:struct<nametypedescription:string,nametypename:string,nametypeauthority:struct<nametypeauthorityname:string,nametypeauthoritydescription:string>>>>>>       from deserializer
enddeviceeventtype      struct<enddeviceeventtypemrid:string,enddeviceeventtypedomain:string,enddeviceeventtypeeventoraction:string,enddeviceeventtypesubdomain:string,type:string,enddeviceeventtypenames:array<struct<name:string,nametype:struct<nametypedescription:string,nametypename:string,nametypeauthority:struct<nametypeauthorityname:string,nametypeauthoritydescription:string>>>>>       from deserializer
header                  struct<noun:string,context:string,verb:string,value:string,source:string,timestamp:string,correlationid:string,name:string,messageid:string,property:struct<propertyname:array<string>,propertyvalue:array<string>>>    from deserializer
payload                 struct<enddeviceevents:array<struct<mrid:string,createddatetime:string,issuerid:string,issuertrackingid:string,reason:string,severity:string,userid:string,asset:struct<assetmrid:string,assetnames:array<struct<name:string,nametype:struct<nametypedescription:string,nametypename:string,nametypeauthority:struct<nametypeauthorityname:string,nametypeauthoritydescription:string>>>>>,enddeviceeventdetails:array<struct<enddeviceeventdetailsname:string,enddeviceeventdetailsvalue:string>>,enddeviceeventtype:string,enddeviceeventnames:array<struct<name:string,nametype:struct<nametypedescription:string,nametypename:string,nametypeauthority:struct<nametypeauthorityname:string,nametypeauthoritydescription:string>>>>,status:struct<statusdatetime:string,statusreason:string,statusremark:string,statusvalue:string>,usagepoint:struct<usagepointmrid:string,usagepointnames:array<struct<name:string,nametype:struct<nametypedescription:string,nametypename:string,nametypeauthority:struct<nametypeauthorityname:string,nametypeauthoritydescription:string>>>>>>>,enddeviceeventtype:array<struct<enddeviceeventtypemrid:string,enddeviceeventtypedomain:string,enddeviceeventtypeeventoraction:string,enddeviceeventtypesubdomain:string,type:string,enddeviceeventtypenames:array<struct<name:string,nametype:struct<nametypedescription:string,nametypename:string,nametypeauthority:struct<nametypeauthorityname:string,nametypeauthoritydescription:string>>>>>>>

我在查询中使用“LATERAL VIEW explode”选项

select eddetails.enddeviceeventdetailsname,	eddetails.enddeviceeventdetailsvalue 
FROM meterevents_tmp 
LATERAL VIEW explode(payload.enddeviceevents.enddeviceeventdetails)  ed AS eddetails 
limit 1;

但我仍然在单行中获取数据。

enddeviceeventdetailsname       enddeviceeventdetailsvalue
["EventSequenceNumber","EventSequenceNumber","EventSequenceNumber","EventSequenceNumber"]       ["683","684","685","686"

我想将这些数据作为

enddeviceeventdetailsname       enddeviceeventdetailsvalue
EventSequenceNumber              683
EventSequenceNumber              684
EventSequenceNumber              685
EventSequenceNumber              686

我已经阅读了关于 stackoverflow 的另一个问题:Exploding Array of Struct using HiveQL

但无法获得预期的输出。因为在那篇文章中它的配置单元外部表而不是我无法指定“MAP KEYS TERMINATED BY”和“COLLECTION ITEMS TERMINATED BY”的 serde

任何帮助深表感谢。

谢谢

4

1 回答 1

0

我能够解决这个问题---

我无法按行获得输出,因为

array<struct<string,string>> 

是父数组的一部分

array<struct<array<struct<string, string>>>

我更新了我的查询并使用了嵌套的爆炸

select eddetails.enddeviceeventdetailsname, eddetails.enddeviceeventdetailsvalue from (select ede.enddeviceeventdetails FROM meterevents_tmp LATERAL VIEW explode(payload.enddeviceevents)  e AS ede) t LATERAL VIEW explode(t.enddeviceeventdetails)  ed AS eddetails limit 10;

我得到了想要的输出 -

enddeviceeventdetailsname       enddeviceeventdetailsvalue
EventSequenceNumber     683
EventSequenceNumber     684
EventSequenceNumber     685
EventSequenceNumber     686

于 2017-01-21T00:45:26.167 回答