Create Managed Table Using DataSource Syntax
Function
Create a managed table using DataSource syntax. The main differences between DataSource syntax and Hive syntax lie in the supported table data storage format range and the supported number of partitions. For details, refer to the syntax format and precautions.
Precautions
- CTAS (CREATE TABLE AS SELECT) statements cannot specify table properties.
- If no delimiter is specified, the default is a comma (,).
- Usage notes for partitioned tables:
- When creating a partitioned table, the partition columns specified in PARTITIONED BY must be columns in the table, and their types must be specified in the column list. Partition columns only support the following types: STRING, BOOLEAN, TINYINT, SMALLINT, SHORT, INT, BIGINT, LONG, DECIMAL, FLOAT, DOUBLE, DATE, TIMESTAMP.
- When creating a partitioned table, the partition columns must be the last column or columns in the table field list, and the order of multiple partition columns must also correspond. Otherwise, an error will occur.
- The maximum number of partitions allowed for a single table is 200,000.
Syntax
CREATE TABLE [IF NOT EXISTS] [db_name.]table_name [(col_name1 col_type1 [COMMENT col_comment1], ...)] USING file_format [OPTIONS (key1=val1, key2=val2, ...)] [PARTITIONED BY (col_name1, col_name2, ...)] [COMMENT table_comment] [AS select_statement];
Keywords
- IF NOT EXISTS: Specify this keyword to avoid an error if the table already exists.
- USING: Specify the storage format.
- OPTIONS: Specify property names and values when creating the table.
- COMMENT: Column or table description.
- PARTITIONED BY: Specify partition columns.
- AS: Create a table using CTAS.
Parameters
| Parameter | Mandatory | Description |
|---|---|---|
| db_name | No | Database name. Only letters, digits, and underscores (_) are allowed. It cannot be purely numeric and cannot start with an underscore. |
| table_name | Yes | Table name in the database. Only letters, digits, and underscores (_) are allowed. It cannot be purely numeric and cannot start with an underscore. The matching rule is ^(?!_)(?![0-9]+$)[A-Za-z0-9_$]*$. Special characters must be enclosed in single quotes (''). Table names are case-insensitive. |
| col_name | Yes | Comma-separated column names with data types. Column names consist of letters, digits, and underscores (_). It cannot be purely numeric and must contain at least one letter. Column names are case-insensitive. |
| col_type | Yes | Data type of the column field. The data type is a primitive type. Refer to Primitive Data Types. |
| col_comment | No | Column field description. Only string constants are supported. |
| file_format | Yes | Managed table data storage format. PARQUET, ORC, and JSON formats are supported. |
| table_comment | No | Table description. Only string constants are supported. |
| select_statement | No | Used for CTAS commands to insert the SELECT query results or specific data from the source table into the newly created managed table. |
| Parameter | Mandatory | Description | Default Value |
|---|---|---|---|
| multiLevelDirEnable | No | Whether to iteratively query data in subdirectories. When set to true, all files in the table path, including files in subdirectories, are read iteratively when querying the table. | false |
| compression | No | Specify the compression format. Typically specified for the Parquet format. The zstd compression format is recommended. | - |
Example 1: Create a Basic Managed Table
Create a non-partitioned managed table named table1, specifying the storage format as ORC using the USING keyword.
CREATE TABLE IF NOT EXISTS table1 (
col_1 STRING,
col_2 INT
)
USING orc; Example 2: Create a Managed Table with Default Values and Comments
Create a managed table named employee with column comments and a table comment.
CREATE TABLE IF NOT EXISTS employee (
id INT COMMENT 'Employee ID',
name STRING COMMENT 'Employee name',
salary DECIMAL(10,2) COMMENT 'Employee salary'
)
USING parquet
COMMENT 'Employee information management table', Example 3: Create Managed Tables Using Different Formats (ORC, JSON)
Create managed tables using different data formats.
-- Using ORC format
CREATE TABLE IF NOT EXISTS table_orc (
id INT,
data STRING
)
USING orc;
-- Using JSON format
CREATE TABLE IF NOT EXISTS table_json (
id INT,
info STRING
)
USING json; Example 4: Create a Partitioned Managed Table
Create a partitioned managed table named student, partitioned by faculty number (facultyNo) and class number (classNo), with OPTIONS properties configured.
CREATE TABLE IF NOT EXISTS student (
Name STRING,
facultyNo INT,
classNo INT
)
USING parquet
PARTITIONED BY (facultyNo, classNo)
OPTIONS (
compression = 'zstd'
); 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