ハイブ静的パーティションテーブル、動的パーティションテーブルの詳細な説明、ケースデモ

ハイブ静的パーティションテーブル、動的パーティションテーブルの詳細な説明、ケースデモ

データテキスト、student.txt

1	zhansgan	12	man
2	lisi	13	man
3	xiaohong	16	woman

静的パーティション:特定のパーティションにデータを割り当てます。
静的パーティションテーブルのケース列を作成する

#创建表
create table student(
id string,
name string,
age string,
sex string
)
PARTITIONED BY(student_age string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'; 
#加载数据
load data local inpath '/root/student.txt' overwrite into table student0
PARTITION  (student_age='12');

静的パーティションテーブルhdfs上のファイルの保存形式は次のとおりです。
ここに画像の説明を挿入
ここに画像の説明を挿入動的パーティション:データの1つまたは複数のフィールドの値に応じて、データは特定のパーティションに動的に分割され
、動的パーティションテーブルが作成されます。ケース1 :複数のパーティションフィールドの場合、すべてが実装され、データを挿入するための動的パーティション

#开启动态分区
set hive.exec.dynamic.partition=true;
#设置为非严格模式 
set hive.exec.dynamic.partition.mode=nonstrict;
#创建元表,导数据到动态分区表用
create table student0(
id string,
name string,
age_partition string,
sex_partition  string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
#导入数据
load data local inpath '/root/student.txt' overwrite into table student0;


#创建动态分区表,分区字段sex_partition ,age_partition。
#注意:分区字段sex_partition ,age_partition, 
#必须在student0表中,但不能在student2表中,不然会导入数据失败。
create table student2(
id2 string,
name2 string,
age2 string,
sex2 string
)
PARTITIONED BY(sex_partition string,age_partition string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'; 

#从student0导入数据到student2
#注意:前四个字段是studet0中字段,后两个字段是分区字段
insert into table student2 PARTITION (sex_partition,age_partition)
select id,name,age_partition,sex_partition,sex_partition,age_partition from student0;

#查询数据
select * from student2 where sex_partition='man' and age_partition='12';

動的パーティションテーブルhdfsのファイルストレージ形式は次のとおりです。
ここに画像の説明を挿入ここに画像の説明を挿入ここに画像の説明を挿入動的パーティションテーブルの作成ケース2:複数のパーティションフィールドの場合、半自動パーティション化を実現します(一部のフィールドは静的にパーティション化されます。静的パーティションフィールドは動的パーティションフィールドの前にある必要があることに注意してください) )

#从student0导入数据到student2
insert into table student2 PARTITION (sex_partition='man',age_partition)
select id,name,age_partition,sex_partition,age_partition from student0;

動的パーティションテーブルhdfsのファイルストレージ形式は次のとおりです。
ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/weixin_43614067/article/details/108637441