从包含空格的路径加载hdfs文件到hive表

问题描述 投票:0回答:1

我正在尝试从带有分区的镶木地板文件创建一个配置单元表。

Create External table a(col1 string, col2 string) partitioned by (col3 string) stored as parquet location '/qa/app/project/';
msck repair table a;

/qa/app/project/col3=all eq 10%35
路径中的这个空格会导致 ddl 错误。

我无法从路径中删除空间,因为这些值是由客户端决定的。有没有办法加载路径中带有空格的文件?

hive hdfs space
1个回答
0
投票

您可以解决这个问题。我建议首先创建一个临时表来导入数据,然后将其从临时表加载到目标表中。请按照以下步骤操作:

创建临时表:

CREATE EXTERNAL TABLE staging_table (col1 STRING, col2 STRING, col3 
STRING)
STORED AS PARQUET
LOCATION '/qa/app/project/';

从暂存表中读取数据(请注意,

col3
包括带空格的值):

> SELECT * FROM staging_table;
OK
value1a value1b all eq 10%35
value2a value2b another value
value3a value3b yet another value

设置目标表:

CREATE EXTERNAL TABLE a (col1 STRING, col2 STRING)
PARTITIONED BY (col3 STRING)
STORED AS PARQUET
LOCATION '/tables/a';

启用动态分区(需要执行此步骤以避免数据插入期间出现语义错误):

SET hive.exec.dynamic.partition=true;
SET hive.exec.dynamic.partition.mode=nonstrict;

将数据从暂存表插入目标表:

INSERT INTO TABLE a PARTITION (col3)
SELECT col1, col2, col3 FROM staging_table;

验证最终结果:

> hadoop fs -ls /tables/a
Found 3 items
drwxr-xr-x   - mapr mapr          1 2024-04-03 18:33 /tables/a/col3=all eq 10%2535
drwxr-xr-x   - mapr mapr          1 2024-04-03 18:33 /tables/a/col3=another value
drwxr-xr-x   - mapr mapr          1 2024-04-03 18:33 /tables/a/col3=yet another value
© www.soinside.com 2019 - 2024. All rights reserved.