記事ディレクトリ
1.コンセプト
Hiveのパーティションテーブルは実際にはHDFSファイルシステムの独立したフォルダーに対応しており、パーティションのすべてのデータファイルはこのフォルダーの下にあります。Hiveのパーティションは、ディレクトリを分割し、ビジネスニーズに応じて大きなデータセットを小さなデータセットに分割します。クエリ中に、WHERE句の式を使用して、クエリに必要な指定のパーティションを選択すると、そのようなクエリの効率が大幅に向上します。
2.パーティションテーブルを作成する
create table dept_partition(
id int, name string
)
partitioned by (month string)
row format delimited fields terminated by '\t'
stored as textfile;
3.パーティションテーブルにデータを読み込む
/ home / hiveの下にsrcdata.txtがあり
ます。データをロードします。
load data local inpath '/home/hive/srcdata.txt' into table default.dept_partition partition(month='202001');
4.パーティションデータクエリ
- 単一パーティションクエリ:
select * from dept_partition where month = '202001';
- マルチパーティション共同クエリ:
select * from dept_partition where month = '202001'
union
select * from dept_partition where month = '202002';
5.パーティションを追加する
- 単一のパーティションを作成します。
alter table dept_partition add partition(month='202008');
- 複数のパーティションを作成します。
alter table dept_partition add partition(month='202005') partition(month='202006');
6.パーティションを削除します
- 単一のパーティションを削除する
alter table dept_partition drop partition (month='202008');
- 複数のパーティションを削除する
alter table dept_partition drop partition(month='202005'),partition(month='202006');
注:パーティションを削除するときは
、複数のパーティションにコンマを追加する必要がありますが、複数のパーティションを追加する必要はありません。
7.パーティションテーブルにパーティションがいくつあるかを確認します
show partitions dept_partition;
8.パーティションテーブルの構造を表示する
show partitions dept_partition;
8.セカンダリパーティションテーブル
- セカンダリパーティションテーブルを作成します。
create table dept_partition2(
id int, name string
)
partitioned by (month string, day string)
row format delimited fields terminated by '\t'
stored as textfile;
- パーティションテーブルにデータを読み込む
load data local inpath '/home/hive/srcdata.txt' into table dept_partition2 partition(month='202011', day='01');
- パーティションデータのクエリ
select * from dept_partition2 where month='202011' and day='01';
9.パーティションテーブルとデータを関連付ける3つの方法で、データをパーティションディレクトリに直接アップロードします。
- データをアップロードした後に修正
まず、パーティションテーブルを作成します。
create table dept_partition2(
id int, name string
)
partitioned by (month string, day string)
row format delimited fields terminated by '\t'
stored as textfile;
パーティションを追加します。
alter table dept_partition2 add partition(month='202001',day='01');
データをアップロード:
hadoop fs -put srcdata.txt /user/hive/warehouse/dept_partition2/month=202001/day=01
直接クエリ:
2.データのアップロード後にパーティションを追加
する手順1に従って、指定した場所にフォルダーを作成し、データをアップロードして最後にパーティションを追加することもできます。
dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=202002/day=02;
hadoop fs -put srcdata.txt /user/hive/warehouse/dept_partition2/month=202002/day=02/
alter table dept_partition2 add partition(month='202002',day='02');
3.フォルダを作成した後、データをパーティションにロードします
alter table dept_partition2 add partition(month='202003',day='03');
load data local inpath '/home/hive/srcdata.txt' into table dept_partition2 partition(month='202003',day='03');