Function
After an OBS partitioned table is created, no partition information is generated for the table. Partition information is generated only after you:
- Insert data to the OBS partitioned table. After the data is inserted successfully, the partition metadata can be queried, for example, by partition columns.
- Copy the partition directory and data into the OBS path of the partitioned table, and run the partition adding statements described in this section to generate partition metadata. Then you can perform operations such as table query by partition columns.
The following describes how to use the ALTER TABLE statement to add a partition.
Syntax
| ALTER TABLE table_name ADD [IF NOT EXISTS]
PARTITION partition_specs1
[LOCATION 'obs_path1']
PARTITION partition_specs2
[LOCATION 'obs_path2'];
|
Keyword
- IF NOT EXISTS: prevents errors when partitions are repeatedly added.
- PARTITION: specifies a partition.
- LOCATION: specifies the partition path.
Parameters
Table 1 Parameter descriptionParameter
|
Description
|
table_name
|
Table name
|
partition_specs
|
Partition fields
|
obs_path
|
OBS path
|
Example
- The following example shows you how to add partition data when the OBS table is partitioned by a single column.
- Use the DataSource syntax to create an OBS table, and partition the table by column external_data. The partition data is stored in obs://bucketName/datapath.
create table testobstable(id varchar(128), external_data varchar(16)) using JSON OPTIONS (path 'obs://bucketName/datapath') PARTITIONED by (external_data);
- Copy the partition directory to obs://bucketName/datapath. In this example, copy all files in the partition column external_data=22 to obs://bucketName/datapath.
- Run the following command to add partition data:
ALTER TABLE testobstable ADD
PARTITION (external_data='22')
LOCATION 'obs://bucketName/datapath/external_data=22';
- After the partition data is added successfully, you can perform operations such as data query based on the partition column.
select * from testobstable where external_data='22';
- The following example shows you how to add partition data when the OBS table is partitioned by multiple columns.
- Use the DataSource syntax to create an OBS table, and partition the table by columns external_data and dt. The partition data is stored in obs://bucketName/datapath.
| create table testobstable(
id varchar(128),
external_data varchar(16),
dt varchar(16)
) using JSON OPTIONS (path 'obs://bucketName/datapath') PARTITIONED by (external_data, dt);
|
- Copy the partition directories to obs://bucketName/datapath. In this example, copy files in external_data=22 and its subdirectory dt=2021-07-27 to obs://bucketName/datapath.
- Run the following command to add partition data:
| ALTER TABLE
testobstable
ADD
PARTITION (external_data = '22', dt = '2021-07-27') LOCATION 'obs://bucketName/datapath/external_data=22/dt=2021-07-27';
|
- After the partition data is added successfully, you can perform operations such as data query based on the partition columns.
| select * from testobstable where external_data = '22';
select * from testobstable where external_data = '22' and dt='2021-07-27';
|