hive插入用json格式覆盖目录

bvhaajcl  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(423)

如何使用json模式插入覆盖目录?
有原始Hiveavro表(这实际上有很多领域)

tb_test--------
name string
kickname string
-----------------

然后我想通过jsonserde将查询结果保存到hdfs中的某个目录中。
我试过了。

insert overwrite directory '/json/'
ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe'
WITH SERDEPROPERTIES (
 "stat_name"="$._col0",
 "stat_interval"="$._col1"
)
STORED AS TEXTFILE 
select name, nickname
from tb_test limit 100

但是,在/json/中写入的json具有\u colxx字段名,而不是源字段名。

{"_col0":"basic_qv"," _col1":"h"}
{"_col0":"basic_qv","_col1 ":"h"}
{"_col0":"basic_qv","_col1 ":"h"}
{"_col0":"basic_qv"," _col1":"h"}
{"_col0":"basic_qv","_col1 ":"h"}

我预料到了

{"name":"basic_qv","nickname":"h"}
{"name":"basic_qv","nickname":"h"}
{"name":"basic_qv","nickname":"h"}
{"name":"basic_qv","nickname":"h"}
{"name":"basic_qv","nickname":"h"}

有什么帮助?
谢谢!!

643ylb08

643ylb081#

您的解决方案(使用jsonudf和命名的\u struct)的问题如下所述:https://github.com/rcongiu/hive-json-serde/issues/151

extract.hql:
add jar /home/myuser/lib/json-udf-1.3.8-SNAPSHOT-jar-with-dependencies.jar;
create temporary function tjson as 'org.openx.data.udf.JsonUDF';

insert overwrite local directory '/json/'
select
tjson(named_struct("name", t.name,"nickname", t.nickname))
from tb_test t
;

您还可以创建基于jsonserde的表,并定义列, insert overwrite 它使用表位置而不是目录。

相关问题