我是新手,在hive上使用json数据。我正在研究一个获取json数据并将其存储到hive表中的spark应用程序。我有一个这样的json:
在展开时看起来像这样:
我能够将json读入数据帧并将其保存在HDFS上的某个位置。但是,获取能够读取数据的蜂巢是困难的部分。
我在网上搜索之后,我试图这样做:
使用STRUCT
为所有json字段,然后使用column.element
访问元素。
例如:
web_app_security
将是表格中的一个列(STRUCT
类型)的名称,其中的config_web_cms_authentication, web_threat_intel_alert_external
中的其他jsons也将是Structs(以rating
和rating_numeric
为字段)。
我尝试用json serde创建表。这是我的表定义:
CREATE EXTERNAL TABLE jsons (
web_app_security struct<config_web_cms_authentication: struct<rating: string, rating_numeric: float>, web_threat_intel_alert_external: struct<rating: string, rating_numeric: float>, web_http_security_headers: struct<rating: string, rating_numeric: float>, rating: string, rating_numeric: float>,
dns_security struct<domain_hijacking_protection: struct<rating: string, rating_numeric: float>, rating: string, rating_numeric: float, dns_hosting_providers: struct<rating:string, rating_numeric: float>>,
email_security struct<rating: string, email_encryption_enabled: struct<rating: string, rating_numeric: float>, rating_numeric: float, email_hosting_providers: struct<rating: string, rating_numeric: float>, email_authentication: struct<rating: string, rating_numeric: float>>,
threat_intell struct<rating: string, threat_intel_alert_internal_3: struct<rating: string, rating_numeric: float>, threat_intel_alert_internal_1: struct<rating: string, rating_numeric: float>, rating_numeric: float, threat_intel_alert_internal_12: struct<rating: string, rating_numeric: float>, threat_intel_alert_internal_6: struct<rating: string, rating_numeric: float>>,
data_loss struct<data_loss_6: struct<rating: string, rating_numeric: float>, rating: string, data_loss_36plus: struct<rating: string, rating_numeric: float>, rating_numeric: float, data_loss_36: struct<rating: string, rating_numeric: float>, data_loss_12: struct<rating: string, rating_numeric: float>, data_loss_24: struct<rating: string, rating_numeric: float>>,
system_hosting struct<host_hosting_providers: struct<rating: string, rating_numeric: float>, hosting_countries: struct<rating: string, rating_numeric: float>, rating: string, rating_numeric: float>,
defensibility struct<attack_surface_web_ip: struct<rating: string, rating_numeric: float>, shared_hosting: struct<rating: string, rating_numeric: float>, defensibility_hosting_providers: struct<rating: string, rating_numeric: float>, rating: string, rating_numeric: float, attack_surface_web_hostname: struct<rating: string, rating_numeric: float>>,
software_patching struct<patching_web_cms: struct<rating: string, rating_numeric: float>, rating: string, patching_web_server: struct<rating: string, rating_numeric: float>, patching_vuln_open_ssl: struct<rating: string, rating_numeric: float>, patching_app_server: struct<rating: string, rating_numeric: float>, rating_numeric: float>,
governance struct<governance_customer_base: struct<rating: string, rating_numeric: float>, governance_security_certifications: struct<rating: string, rating_numeric: float>, governance_regulatory_requirements: struct<rating: string, rating_numeric: float>, rating: string, rating_numeric: float>
)ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
STORED AS orc
LOCATION 'hdfs://nameservice1/data/gis/final/rr_current_analysis'
我试图用json serde解析行。在我将一些数据保存到表后,当我尝试查询时出现以下错误:
Error: java.io.IOException: java.lang.ClassCastException: org.apache.hadoop.hive.ql.io.orc.OrcStruct cannot be cast to org.apache.hadoop.io.Text (state=,code=0)
我不确定我是否正确地做到了这一点。
我也欢迎将数据存储到表中的任何其他方式。任何帮助,将不胜感激。谢谢。
这是因为你将ORC混合为存储(STORED AS orc
)和JSON作为SerDe(ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
)覆盖ORC的默认OrcSerde
SerDe,但不是输入(OrcInputFormat
)和输出(OrcOutputFormat
)格式。
您需要使用ORC存储而不覆盖其默认SerDe。在这种情况下,请确保您的Spark应用程序写入ORC表,而不是JSON。
或者,如果您希望将数据存储在JSON中,请将JsonSerDe
与纯文本文件一起用作存储(STORED AS TEXTFILE
)。
Hive开发人员指南解释了SerDe和存储如何工作-https://cwiki.apache.org/confluence/display/Hive/DeveloperGuide#DeveloperGuide-HiveSerDe