Hive 从具有不同记录格式的文件创建外部配置单元表

Hive 从具有不同记录格式的文件创建外部配置单元表,hive,external,hdfs,Hive,External,Hdfs,我想创建一个利用HDFS中已有数据的外部配置单元表。这些文件位于类似于/hdfs/data/location的目录和年-月格式的子目录中。例如:/hdfs/data/location/2013年12月和/hdfs/data/location/2014年1月 在这些目录中有多个文件,但在这些文件中有不同类型的数据(不同的字段)。不同类型记录的示例如下: A型 type state city population B型 type zipcode registeredvoters 实际数据示例(以

我想创建一个利用HDFS中已有数据的外部配置单元表。这些文件位于类似于
/hdfs/data/location
的目录和
年-月
格式的子目录中。例如:
/hdfs/data/location/2013年12月
/hdfs/data/location/2014年1月

在这些目录中有多个文件,但在这些文件中有不同类型的数据(不同的字段)。不同类型记录的示例如下:

A型

type
state
city
population
B型

type
zipcode
registeredvoters
实际数据示例(以制表符分隔)

数据已经是这种格式,并且正在被配置单元之外的其他进程使用,因此更改它可能不是一个选项。我也不想在HDFS中复制数据

是否有办法仅为上述数据中定义的给定类型创建配置单元表?

以下是我迄今为止为创建所做的工作:

create external table population (
  type string,
  state string,
  city string,
  population int
)
location '/hdfs/data/location';

我不认为你可以有一个表,但我认为你可以创建一个视图,使用str_to_映射UDF来解释行

create external table raw_population( line string ) location '/hdfs/data/location';

create view population_view as
select
    pmap['type'] as type,
    pmap['state'] as state,
    pmap['city'] as city
    pmap['population'] as population
from 
  ( select str_to_map( line, '\t', ':') as pmap from raw_population ) pm;

我还需要where type='A',因为type='B'没有这些列?您可以从其他类型中添加其他列,(例如,pmap['zipcode'],pmap['population']),如果值不在映射中,它应该返回null。或者您也可以添加“pmap['type']='A'”
create external table raw_population( line string ) location '/hdfs/data/location';

create view population_view as
select
    pmap['type'] as type,
    pmap['state'] as state,
    pmap['city'] as city
    pmap['population'] as population
from 
  ( select str_to_map( line, '\t', ':') as pmap from raw_population ) pm;