Add Partition
Function
Add one or more partitions to an existing OBS partitioned table. This command registers the partition metadata information in the metadata database, enabling subsequent queries to retrieve data based on partition columns.
There are two main scenarios for generating partition information in an OBS partitioned table:
- Insert corresponding partition data into the OBS partitioned table. After the data is inserted successfully, the partition metadata information is automatically generated.
- Manually copy the partition directory and data to the OBS partitioned table path, then execute this command to add the partition metadata information.
Syntax
ALTER TABLE table_name ADD [IF NOT EXISTS] PARTITION partition_specs1 [LOCATION 'location_path1'] PARTITION partition_specs2 [LOCATION 'location_path2'] ...;
Parameters
| Parameter | Description |
|---|---|
| table_name | Table name. |
| IF NOT EXISTS | Optional keyword. When specified, no error is reported if the partition already exists. It is silently skipped. |
| partition_specs | Partition specification, in the format (col1=val1, col2=val2, ...). If the table has multiple partition columns, all partition column values must be specified when adding a partition. The order can be arbitrary. |
| location_path | Optional. The storage path of the partition. If specified, the path must already exist. Otherwise, an error will be reported. |
Precautions
- When adding a partition to a table, the table and partition columns (columns specified by PARTITIONED BY when creating the table) must already exist. The partition to be added must not be a duplicate. Otherwise, an error will occur. Use IF NOT EXISTS to avoid errors for existing partitions.
- If the partitioned table is partitioned by multiple fields, all partition fields must be specified when adding a partition. The order of the specified fields can be arbitrary.
- Parameters in partition_specs are enclosed in parentheses by default, for example: PARTITION (dt='2009-09-09', city='xxx').
- If a path is specified when adding a partition, the path must already exist; otherwise, an error will occur.
- When adding multiple partitions, separate each group of PARTITION partition_specs LOCATION 'location_path' with a space.
- If the path specified for a new partition contains subdirectories (or nested subdirectories), all file types and contents under the subdirectories will also be considered records of that partition. Ensure that all file types and file contents under the partition directory are consistent with the table fields; otherwise, the query will report an error.
Example 1: Add a Single Partition
Add a partition to the table student with partition columns facultyNo=10, classNo=101.
ALTER TABLE student ADD PARTITION (facultyNo=10, classNo=101);
Example 2: Add Multiple Partitions
Add two partitions to the table student simultaneously.
ALTER TABLE student ADD PARTITION (facultyNo=10, classNo=101) PARTITION (facultyNo=20, classNo=102);
Example 3: Add a Partition Using IF NOT EXISTS
Use IF NOT EXISTS when adding a partition to avoid errors if the partition already exists.
ALTER TABLE student ADD IF NOT EXISTS PARTITION (facultyNo=10, classNo=101);
Example 4: Add a Partition and Specify LOCATION
Add a partition to an OBS table and specify the storage path for the partition data.
ALTER TABLE testobstable ADD PARTITION (external_data='22') LOCATION 'obs://bucketName/datapath/external_data=22';
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot