从包含 json 字符串的镶木地板文件创建外部配置单元 table
Creating external hive table from parquet file which contains json string
我有一个存储在分区目录中的镶木地板文件。分区格式为
/dates=*/hour=*/something.parquet
.
parquet文件内容如下:
{a:1,b:2,c:3}.
这是 json 数据,我想创建外部配置单元 table。
我的做法:
CREATE EXTERNAL TABLE test_table (a int, b int, c int) PARTITIONED BY (dates string, hour string) STORED AS PARQUET LOCATION '/user/output/';
在那之后我 运行 MSCK REPAIR TABLE test_table;
但我得到以下输出:
hive> select * from test_table;
OK
NULL NULL NULL 2021-09-27 09
其他三列为空。我想我必须以某种方式定义 JSON 架构,但我不知道如何进一步进行。
使用与 parquet 文件相同的架构创建 table:
CREATE EXTERNAL TABLE test_table (value string) PARTITIONED BY (dates string, hour string) STORED AS PARQUET LOCATION '/user/output/';
运行修复table挂载分区:
MSCK REPAIR TABLE test_table;
解析查询中的值:
select e.a, e.b, e.c
from test_table t
lateral view json_tuple(t.value, 'a', 'b', 'c') e as a,b,c
必要时将值转换为 int:cast(e.a as int) as a
我有一个存储在分区目录中的镶木地板文件。分区格式为
/dates=*/hour=*/something.parquet
.
parquet文件内容如下:
{a:1,b:2,c:3}.
这是 json 数据,我想创建外部配置单元 table。
我的做法:
CREATE EXTERNAL TABLE test_table (a int, b int, c int) PARTITIONED BY (dates string, hour string) STORED AS PARQUET LOCATION '/user/output/';
在那之后我 运行 MSCK REPAIR TABLE test_table;
但我得到以下输出:
hive> select * from test_table;
OK
NULL NULL NULL 2021-09-27 09
其他三列为空。我想我必须以某种方式定义 JSON 架构,但我不知道如何进一步进行。
使用与 parquet 文件相同的架构创建 table:
CREATE EXTERNAL TABLE test_table (value string) PARTITIONED BY (dates string, hour string) STORED AS PARQUET LOCATION '/user/output/';
运行修复table挂载分区:
MSCK REPAIR TABLE test_table;
解析查询中的值:
select e.a, e.b, e.c
from test_table t
lateral view json_tuple(t.value, 'a', 'b', 'c') e as a,b,c
必要时将值转换为 int:cast(e.a as int) as a