如何在带有原始json字段的 hive 中定义表(不进行解析,以纯文本格式)?
对于以下s3文件:
{"first_field":1, "json_field": {"a":{"b":"c"}, "d":"e"}, "sec_field":4}
{"first_field":2, "json_field": {"m":"cv", "d":"e"}, "sec_field":5}
{"first_field":3, "json_field": {"k":1, "d":"e"}, "sec_field":6}
我希望以下查询:
select first_field, json_field, sec_field from web.json_table;
将返回:
1 {"a":{"b":"c"}, "d":"e"} 4
2 {"m":"cv", "d":"e"} 5
3 {"k":1, "d":"e"} 6
我试图将表定义如下:
CREATE EXTERNAL TABLE web.json_table(
first_field integer,
json_field string,
sec_field integer
)
ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe'
location 's3n://my-bucket';
但是查询返回:
hive> select first_field, json_field, sec_field from web.json_table;
OK
1 { NULL
2 { NULL
3 { NULL
最佳答案
我认为您无法以通用的方式在字符串中映射任何JSON。
您需要将json_field
定义为固定结构:
{"a":{"b":"c"}, "d":"e"} ==> STRUCT<a:STRUCT<b:STRING>,d:STRING>
{"m":"cv", "d":"e"} ==> STRUCT<m:STRING,d:STRING>
{"k":1, "d":"e"} ==> STRUCT<k:STRING,d:STRING>
为您的实际JSON:
CREATE EXTERNAL TABLE web.json_table (
first_field integer,
json_field STRUCT<a:STRUCT<b:STRING>,d:STRING,m:STRING,k:STRING>,
sec_field integer
)
ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe'
location 's3n://my-bucket';
警告:旧的Hive版本不支持JSON Key中的大写字母。
编辑:
ext_table
列作为字符串创建外部CSV表json_data
(使用不出现的特殊分隔符,例如0x00
或0x01
)get_json_object
的ext_table
创建 View ,以提取所有固定和动态字段关于json - hive 中的原始json字段类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47183520/