Create tables
Syntax
DLA supports table creation syntax based on OSS source files and is compatible with the syntax CREATE EXTERNAL TABLE of Hive.
CREATE EXTERNAL TABLE [IF NOT EXISTS] [db_name.]table_name
[(col_name data_type [COMMENT col_comment], ... [constraint_specification])]
[COMMENT table_comment]
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
[ROW FORMAT row_format]
[STORE AS file_format]
| STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...)]
LOCATION oss_path
data_type
: primitive_type
| array_type
| map_type
| struct_type
| union_type
primitive_type
: TINYINT
| SMALLINT
| INT
| BIGINT
| BOOLEAN
| FLOAT
| DOUBLE
| STRING
| BINARY
| TIMESTAMP
| DECIMAL
| DECIMAL(precision, scale)
| DATE
| VARCHAR
| CHAR
array_type
: ARRAY < data_type >
map_type
: MAP < primitive_type, data_type >
struct_type
: STRUCT < col_name : data_type [COMMENT col_comment], ...>
union_type
: UNIONTYPE < data_type, data_type, ... >
row_format
: 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]
| SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, ...)]
file_format:
: SEQUENCEFILE
| TEXTFILE
| RCFILE
| ORC
| PARQUET
| AVRO
| INPUTFORMAT input_format_classname OUTPUTFORMAT output_format_classname
oss_path:
The directory of the OSS bucket where the data file is located, instead of the data file. For example, oss://test-bucket-julian/test_csv indicates the directory of the data file, rather than the data file. The data file is in the directory test_csv. Do not place the file directly in the root bucket. Tables that you created by using Open Analytics identify all files in oss://test-bucket-julian/test_csv, but do not scan files in child folders.
Example: 'oss://your-bucket/parent/dir/to/data'
Example
- Create tables without partitions
```
CREATE EXTERNAL TABLE nation_text_string (
N_NATIONKEY INT COMMENT 'column N_NATIONKEY',
N_NAME STRING,
N_REGIONKEY INT,
N_COMMENT STRING
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '|'
STORED AS TEXTFILE LOCATION 'oss://your-bucket/path/to/nation_text';
```
Create tables with partitions
CREATE EXTERNAL TABLE primitives_text_p (
id INT COMMENT 'default',
bool_col BOOLEAN COMMENT 'default',
tinyint_col TINYINT COMMENT 'default',
smallint_col SMALLINT COMMENT 'default',
int_col INT COMMENT 'default',
bigint_col BIGINT COMMENT 'default',
float_col FLOAT COMMENT 'default',
double_col DOUBLE COMMENT 'default',
date_string_col STRING COMMENT 'default',
string_col STRING COMMENT 'default',
timestamp_col TIMESTAMP COMMENT 'default')
PARTITIONED BY (year INT COMMENT 'default', month INT COMMENT 'default')
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
ESCAPED BY '\\'
STORED AS TEXTFILE
LOCATION 'oss://your-bucket/path/to/primitives_text_p';
After you create a partitioned table, run the following SQL statement to refresh the metadata:
MSCK REPAIR TABLE