【问题标题】:update partitioned hive table if we already have some data in it如果我们已经有一些数据,则更新分区的配置单元表
【发布时间】:2017-03-05 02:08:14
【问题描述】:

我有一个按天分区的配置单元表,并且与 03-02-2017 相关的数据已加载到其中,但是第二天我有与 03-03-2017 相关的数据。现在如何用我的新数据更新 hive 表,以便我的 hive 仓库目录看起来像这样

hive/warehouse/sample_database/sample_table/day=03-02-2017/data_part_0000
hive/warehouse/sample_database/sample_table/day=03-03-2017/data_part_0000

所以请提供创建表的代码以及当新数据集添加到此表时如何更新表。

【问题讨论】:

标签: hadoop hive


【解决方案1】:

这是一个完整的演示,描述了 2 个基本选项:

  1. 向表中添加分区。 HDFS 目录将自动创建。
  2. 将目录添加到 HDFS 并在表上应用 msck repair 以添加分区。

请注意,我对分区使用 DATE 类型。
日期文字的 ANSI/ISO 表示法是 date 'YYYY-MM-DD'

即使对于不支持 DATE 类型的旧版本,我也强烈建议避免使用除 YYYY-MM-DD 之外的任何日期格式,原因有两个:
1. 这是日期函数唯一支持的格式。
2. 这种格式允许正确的字母比较,例如-
'2017-01-22' > '2016-09-22' 但 '01-22-2017'

演示

重击

hdfs dfs -mkdir -p /hive/warehouse/sample_database/sample_table

蜂巢

create external table sample_table
(
    i int
)
partitioned by (day date)
location '/hive/warehouse/sample_database/sample_table'
;

选项 1 - 更改表 ... 添加分区 ...

蜂巢

alter table sample_table add partition (day=date '2017-03-02');
alter table sample_table add partition (day=date '2017-03-03');

hive> show partitions sample_table;
OK
day=2017-03-02
day=2017-03-03
Time taken: 0.067 seconds, Fetched: 2 row(s)
hive> dfs -ls /hive/warehouse/sample_database/sample_table;
Found 2 items
... 2017-03-04 23:31 /hive/warehouse/sample_database/sample_table/day=2017-03-02
... 2017-03-04 23:31 /hive/warehouse/sample_database/sample_table/day=2017-03-03
hive> 

选项 2 - hdfs dfs -mkdir ... + msck 修复表 ...

重击

hdfs dfs -mkdir /hive/warehouse/sample_database/sample_table/day=2017-03-02
hdfs dfs -mkdir /hive/warehouse/sample_database/sample_table/day=2017-03-03

蜂巢

msck repair

hive> show partitions sample_table;
OK
Time taken: 0.187 seconds
hive> msck repair table sample_table;
OK
Partitions not in metastore:    sample_table:day=2017-03-02 sample_table:day=2017-03-03
Repair: Added partition to metastore sample_table:day=2017-03-02
Repair: Added partition to metastore sample_table:day=2017-03-03
Time taken: 0.143 seconds, Fetched: 3 row(s)
hive> show partitions sample_table;
OK
day=2017-03-02
day=2017-03-03
Time taken: 0.076 seconds, Fetched: 2 row(s)
hive> dfs -ls /hive/warehouse/sample_database/sample_table;
Found 2 items
... 2017-03-04 23:31 /hive/warehouse/sample_database/sample_table/day=2017-03-02
... 2017-03-04 23:31 /hive/warehouse/sample_database/sample_table/day=2017-03-03
hive> 

【讨论】:

    【解决方案2】:

    首先,请记住 Hive 表指向 HDFS 中的某个位置,因此我不确定您对 Hive 数据仓库的含义。

    为了创建表,您应该必须按天(字符串)创建一个分区表,例如:

    CREATE EXTERNAL TABLE myTable
    partitioned by (day STRING)
    ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.avro.AvroSerDe'
    WITH SERDEPROPERTIES ('avro.schema.url'='/path/to/my/avro/schema/avro_schema.avsc')
    STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.avro.AvroContainerInputFormat'
    OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.avro.AvroContainerOutputFormat'
    LOCATION '/hive/warehouse/sample_database/sample_table';
    

    在上述解决方案中,我假设您正在使用 avro 数据进行处理。重要的部分实际上是:partitioned by (day STRING)。在这里,您告诉 Hive 该表将按天分区。

    当一个新的数据集出现在目录结构中(hive/warehouse/sample_database/sample_table/day=03-03-2017/data_part_0000)并且它不是由insert hive statement添加时,你将不得不运行这个命令:

    msck repair table myTable;
    

    这样,一个新的分区将被添加到表的元存储中。请注意,您还有另一个选择:

    alter table myTable add partition (day = '03-03-2017');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多