Create Managed Table Using Hive Syntax
Function
Create a managed table using Hive 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.
- Hive managed tables do not support specifying multi-character delimiters when creating the table.
- Usage notes for partitioned tables:
- When creating a partitioned table, the partition columns specified in PARTITIONED BY must be columns not in the table, and their data types must be specified. Partition columns support types supported by open-source Hive, such as STRING, BOOLEAN, TINYINT, SMALLINT, SHORT, INT, BIGINT, LONG, DECIMAL, FLOAT, DOUBLE, DATE, TIMESTAMP.
- Multiple partition columns can be specified. Partition columns only need to be specified after the PARTITIONED BY keyword and cannot be specified after the table name like regular columns. 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], ...)]
[COMMENT table_comment]
[PARTITIONED BY (col_name2 col_type2 [COMMENT col_comment2], ...)]
[ROW FORMAT row_format]
STORED AS file_format
[TBLPROPERTIES (key1=val1, key2=val2, ...)]
[AS select_statement];
row_format:
: SERDE serde_cls [WITH SERDEPROPERTIES (key1=val1, key2=val2, ...)]
| DELIMITED [FIELDS TERMINATED BY char [ESCAPED BY char]]
[COLLECTION ITEMS TERMINATED BY char]
[MAP KEYS TERMINATED BY char]
[LINES TERMINATED BY char]
[NULL DEFINED AS char] Keywords
- IF NOT EXISTS: Specify this keyword to avoid an error if the table already exists.
- COMMENT: Column or table description.
- PARTITIONED BY: Specify partition columns.
- ROW FORMAT: Row data format.
- STORED AS: Specify the stored file format. Currently, this keyword only supports the following formats: TEXTFILE, AVRO, ORC, SEQUENCEFILE, RCFILE, PARQUET. This keyword must be specified when creating a managed table.
- TBLPROPERTIES: Used to add key/value properties to the table. When the table storage format is PARQUET, the table compression format can be specified as zstd by specifying TBLPROPERTIES(parquet.compression = 'zstd').
- 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 | Column field name. Column field 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. |
| row_format | No | Row data format. The ROW FORMAT function only supports tables of type TEXTFILE. |
| file_format | Yes | Managed table data storage format. Supports TEXTFILE, AVRO, ORC, SEQUENCEFILE, RCFILE, PARQUET. |
| table_comment | No | Table description. Only string constants are supported. |
| key = value | No | Set specific properties and values for TBLPROPERTIES. When the table storage format is PARQUET, the table compression format can be specified as zstd by specifying TBLPROPERTIES(parquet.compression = 'zstd'). |
| 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. |
Example 1: Create a Basic Hive Format Managed Table
Create a non-partitioned managed table named table1, specifying the storage format as ORC using the STORED AS keyword.
CREATE TABLE IF NOT EXISTS table1 (
col_1 STRING,
col_2 INT
)
STORED AS orc; Example 2: Create a Managed Table with STORED AS PARQUET
Create a PARQUET format managed table named table_parquet, specifying the compression format through TBLPROPERTIES.
CREATE TABLE IF NOT EXISTS table_parquet (
id INT,
name STRING,
score DOUBLE
)
STORED AS parquet
TBLPROPERTIES (
parquet.compression = 'zstd'
); Example 3: Create a Managed Table with ROW FORMAT DELIMITED
Create a TEXTFILE type managed table named table4 with ROW FORMAT set.
CREATE TABLE IF NOT EXISTS table4 (
col_1 STRING,
col_2 INT
)
STORED AS textfile
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '/'
COLLECTION ITEMS TERMINATED BY '$'
MAP KEYS TERMINATED BY '#'
LINES TERMINATED BY '\n'
NULL DEFINED AS 'NULL'; Example 4: Create a Managed Table with SERDE
Create a managed table named table_serde using a custom SERDE class.
CREATE TABLE IF NOT EXISTS table_serde (
id INT,
data STRING
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
WITH SERDEPROPERTIES (
'field.delim' = ',',
'serialization.format' = ','
)
STORED AS textfile; 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