【问题标题】:Hive: Partitioning by part of integer columnHive:按整数列的一部分进行分区
【发布时间】:2017-03-22 11:27:33
【问题描述】:

我想创建一个外部 Hive 表,按记录类型和日期(年、月、日)分区。一个复杂的问题是我的数据文件中的日期格式是单值整数 yyyymmddhhmmss,而不是所需的日期格式 yyyy-mm-dd hh:mm:ss。 我可以仅基于单个数据值指定 3 个新分区列吗?类似于下面的示例(不起作用)

create external table cdrs (
record_id int, 
record_detail tinyint,
datetime_start int
)
partitioned by (record_type int, createyear=datetime_start(0,3) int, createmonth=datetime_start(4,5) int, createday=datetime_start(6,7) int)
row format delimited 
fields terminated by '|' 
lines terminated by '\n'
stored as TEXTFILE
location 'hdfs://nameservice1/tmp/sbx_unleashed.db'
tblproperties ("skip.header.line.count"="1", "skip.footer.line.count"="1");

【问题讨论】:

  • 只有当您的 HDFS 数据已经通过 record_type 和 datetime_start(或它的变体)分隔到不同的目录时,分区才有意义。是这样吗?
  • 还没有。因此,如果我理解正确,那么我需要创建以下形式的目录:hdfs://nameservice1/tmp/sbx_unleashed.db/createyear=2017/createmonth=01/createday=01/recordtype=1,然后物理移动文件到相应的 hdfs 目录,然后使用 'partioned by (createyear int, createmonth int, createday int, record_type int) 运行上面的命令?
  • 为什么不直接使用日期进行分区?

标签: hive


【解决方案1】:

如果您希望能够使用MSCK REPAIR TABLE 根据目录结构为您添加分区,您应该使用以下约定:

  • 目录的嵌套应与分区列的顺序相匹配。
  • 目录名称应为{partition column name}={value}

如果您打算手动添加分区,则该结构没有意义。
任何设置值都可以与任何目录耦合。例如-

alter table cdrs  
add if not exist partition (record_type='TYP123',createdate=date '2017-03-22') 
location 'hdfs://nameservice1/tmp/sbx_unleashed.db/2017MAR22_OF_TYPE_123';

假设目录结构-

.../sbx_unleashed.db/record_type=.../createyear=.../createmonth=.../createday=.../

例如

.../sbx_unleashed.db/record_type=TYP123/createyear=2017/createmonth=03/createday=22/

create external table cdrs 
(
   record_id      int
  ,record_detail  tinyint
  ,datetime_start int
)
partitioned by (record_type int,createyear int, createmonth tinyint, createday tinyint)
row format delimited 
fields terminated by '|' 
lines terminated by '\n'
stored as TEXTFILE
location 'hdfs://nameservice1/tmp/sbx_unleashed.db'
tblproperties ("skip.header.line.count"="1", "skip.footer.line.count"="1")
;

假设目录结构-

.../sbx_unleashed.db/record_type=.../createdate=.../

例如

.../sbx_unleashed.db/record_type=TYP123/createdate=2017-03-22/

create external table cdrs 
(
   record_id      int
  ,record_detail  tinyint
  ,datetime_start int
)
partitioned by (record_type int,createdate date)
row format delimited 
fields terminated by '|' 
lines terminated by '\n'
stored as TEXTFILE
location 'hdfs://nameservice1/tmp/sbx_unleashed.db'
tblproperties ("skip.header.line.count"="1", "skip.footer.line.count"="1")
;

【讨论】:

  • 像魅力一样工作。但是,对于阅读此答案的其他人,请记住您的表格仍然是空的。运行“ALTER TABLE cdrs ADD PARTITION (createyear = '2017', createmonth='01', createday='01');”之后为每个分区显示数据。
  • 如果遵循约定,只需执行MSCK REPAIR TABLE cdrs,这将根据目录结构修改所有分区(添加+删除)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-25
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
相关资源
最近更新 更多