在配置单元的外部 table 中创建分区

creating partition in external table in hive

我已在配置单元的 内部 table 中成功创建并添加了 动态分区 。即通过使用以下步骤:

1-创建了一个来源 table

2-从本地加载数据到源table

3- 使用分区创建了另一个 table - partition_table

4- 将来自源 table 的数据插入此 table,导致动态创建所有分区

我的问题是,如何在外部执行此操作 table?我读了很多这方面的文章,但我很困惑,我是否必须指定现有分区的路径才能为外部创建分区 table??

示例: 第 1 步:

create external table1 ( name string, age int, height int)
location 'path/to/dataFile/in/HDFS';

第 2 步:

alter table table1 add partition(age) 
location 'path/to/already/existing/partition'

我不确定如何在外部 table 中进行分区。有人可以帮忙给出相同的步骤描述吗?

提前致谢!

是的,您必须明确告诉 Hive 您的分区字段是什么。

假设您有一个要在其上创建外部 table.

的 HDFS 目录
/path/to/dataFile/

假设此目录已经存储(分区)部门数据如下:

/path/to/dataFile/dept1
/path/to/dataFile/dept2
/path/to/dataFile/dept3

这些目录中的每一个都有一堆文件,每个文件 包含字段的实际逗号分隔数据,例如姓名、年龄、身高。

e.g.
    /path/to/dataFile/dept1/file1.txt
    /path/to/dataFile/dept1/file2.txt

现在让我们在上面创建外部 table:

步骤 1. 创建外部 table:

CREATE EXTERNAL TABLE testdb.table1(name string, age int, height int)
PARTITIONED BY (dept string)
ROW FORMAT DELIMITED
STORED AS TEXTFILE
LOCATION '/path/to/dataFile/';

步骤 2. 添加分区:

ALTER TABLE testdb.table1 ADD PARTITION (dept='dept1') LOCATION '/path/to/dataFile/dept1';
ALTER TABLE testdb.table1 ADD PARTITION (dept='dept2') LOCATION '/path/to/dataFile/dept2';
ALTER TABLE testdb.table1 ADD PARTITION (dept='dept3') LOCATION '/path/to/dataFile/dept3';

完成,运行 select 查询一次以验证数据是否加载成功。

1.下面设置属性

set hive.exec.dynamic.partition=true

set hive.exec.dynamic.partition.mode=nonstrict

2。创建外部分区 table

create external table1 ( name string, age int, height int) location 'path/to/dataFile/in/HDFS';

3。将数据从源 table.

插入分区 table

基本上,过程是一样的。只是您创建了外部分区 table 并提供了 table 的 HDFS 路径,它将在该路径下创建和存储分区。

希望对您有所帮助。

按照以下步骤操作:

  1. 创建临时 table/Source table

    create table source_table(name string,age int,height int) row format delimited by ',';
    

    使用文件中的分隔符代替“,”;

  2. 将数据加载到源中table

    load data local inpath 'path/to/dataFile/in/HDFS';
    
  3. 使用分区

    创建外部table
    create external table external_dynamic_partitions(name string,height int) 
    partitioned by (age int) 
    location 'path/to/dataFile/in/HDFS';
    
  4. 将动态分区模式启用为非严格

    set hive.exec.dynamic.partition.mode=nonstrict
    
  5. 使用源文件

    的分区将数据加载到外部table
    insert into table external_dynamic partition(age) 
    select * from source_table;
    

就是这样。 您可以使用

检查分区信息
show partitions external_dynamic;

您甚至可以检查它是否是外部 table 或不使用

describe formatted external_dynamic;

External table 是Hive中的一种table,数据不移动到hive仓库。这意味着即使你删除 table,数据仍然存在,你将始终获得最新数据,而托管 table 则不是这样。

正确的做法。

  1. 创建 table 并提及它已分区。

    创建外部 table1(名称字符串、年龄整数、身高整数) 按(年龄整数)划分 存储为 ****(您的格式) 位置 'path/to/dataFile/in/HDFS';

  2. 现在您必须刷新 hive metastore 中的分区。

    msck修复tabletable1

这将负责将所有分区加载到配置单元元存储中。

您可以在过程中的任何时候使用 msck repair table 来更新 Metastore。