Help Center/ AI DataLake/ Spark reference/ Table Related/ Create Table/ Create External Table Using DataSource Syntax
Updated on 2026-11-06 GMT+08:00

Create External Table Using DataSource Syntax

Function

Create an external 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.

You are advised to use the OBS parallel file system for storage. The parallel file system is a high-performance file system that provides millisecond-level access latency, TB/s-level bandwidth, and millions of IOPS, making it suitable for interactive big data analytics scenarios.

Precautions

  • To view the table size, check it through OBS.
  • CTAS (CREATE TABLE AS SELECT) statements cannot specify table properties.
  • Scenario where the OBS directory contains subdirectories: When creating a table, if the specified path is a directory on OBS and that directory contains subdirectories (or nested subdirectories), all file types and their contents under the subdirectories are also part of the table content. You must ensure that all file types under the specified directory and its subdirectories are consistent with the storage format specified in the CREATE TABLE statement, and that all file contents are consistent with the fields in the table. Otherwise, the query will report an error. You can set multiLevelDirEnable to true in the OPTIONS of the CREATE TABLE statement to query content under subdirectories. The default value for this parameter is false (Note: This configuration item is a table property; configure it with caution. Hive tables do not support this configuration item).
  • 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.
  • Differences between external table and managed table creation:

    Difference Item

    External Table

    Managed Table

    LOCATION

    Mandatory. Specifies the OBS storage path.

    Not specified

    Data deletion

    DROP TABLE only deletes metadata.

    DROP TABLE deletes both metadata and data.

    EXTERNAL keyword

    Not used (different from open-source)

    Not used

    USING / STORED AS

    Must be specified.

    Must be specified.

Difference from open-source Spark: Open-source Spark uses CREATE EXTERNAL TABLE ... LOCATION to create external tables. The platform does not use the EXTERNAL keyword. Instead, it distinguishes external tables from managed tables by specifying a LOCATION path.

Syntax

CREATE TABLE [IF NOT EXISTS] [db_name.]table_name 
  [(col_name1 col_type1 [COMMENT col_comment1], ...)]
  USING file_format 
  [OPTIONS (path 'obs_path', 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

Table 1 Basic 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 to be created in the database. Only letters, digits, and underscores (_) are allowed. It cannot be purely numeric and cannot start with a digit or 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. 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

Input format for creating the table. Supports ORC, PARQUET, JSON, CSV, AVRO types.

path

Yes

OBS storage path where the data file is located. You are advised to use the OBS parallel file system for storage. Format: obs://bucketName/tblPath, where bucketName is the bucket name and tblPath is the directory name. The file name does not need to be specified after the directory. When a folder and a file have the same name under an OBS directory, the path pointed to by the OBS table creation will prioritize the file over the folder.

table_comment

No

Table description information. 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 OBS table.

Table 2 OPTIONS parameters

Parameter

Mandatory

Description

Default Value

path

No

Specified table path, i.e., the OBS storage path.

-

multiLevelDirEnable

No

In nested subdirectory scenarios, 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

dataDelegated

No

Whether to clear data under the path when deleting a table or partition.

false

compression

No

Specify the compression format. Typically specified for the Parquet format. The zstd compression format is recommended.

-

Table 3 CSV data format OPTIONS parameters

Parameter

Mandatory

Description

Default Value

delimiter

No

Data delimiter.

,

quote

No

Quote character.

"

escape

No

Escape character.

\

multiLine

No

Whether column data contains carriage returns or line breaks. true means it contains them; false means it does not.

false

dateFormat

No

Specifies the date format for DATE fields in the CSV file.

yyyy-MM-dd

timestampFormat

No

Specifies the date format for TIMESTAMP fields in the CSV file.

yyyy-MM-dd HH:mm:ss

mode

No

Specifies the mode for parsing CSV. PERMISSIVE: Permissive mode, sets erroneous fields to NULL. DROPMALFORMED: Drops the entire row when encountering erroneous fields. FAILFAST: Error mode, reports an error directly when encountering erroneous fields.

PERMISSIVE

header

No

Whether the CSV contains a header. true means it contains a header; false means it does not.

false

nullValue

No

Sets the character representing NULL. For example, nullValue="nl" means nl represents NULL.

-

comment

No

Sets the character representing the start of a comment. For example, comment='#' means lines starting with # are comments.

-

compression

No

Sets the compression format for the data. Currently supports gzip, bzip2, deflate compression formats. Enter none if compression is not desired.

none

encoding

No

Encoding format for the data. Supports utf-8, gb2312, gbk.

utf-8

Example 1: Create a Basic OBS Non-Partitioned Table

Create a non-partitioned OBS 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
OPTIONS (path 'obs://bucketName/filePath');

Example 2: Create an OBS Partitioned Table

Create a partitioned table named student, partitioned by faculty number (facultyNo) and class number (classNo).

CREATE TABLE IF NOT EXISTS student (
    Name        STRING,
    facultyNo   INT,
    classNo     INT
)
USING csv
OPTIONS (path 'obs://bucketName/filePath')
PARTITIONED BY (facultyNo, classNo);

Example 3: Create an OBS Partitioned Table with Comments and Properties

Create an OBS partitioned table named table3 with column comments, table comments, and OPTIONS property configuration.

CREATE TABLE IF NOT EXISTS table3 (
    col_1   STRING COMMENT 'Username',
    col_2   INT COMMENT 'User ID'
)
USING parquet
OPTIONS (
    path 'obs://bucketName/filePath',
    multiLevelDirEnable = true,
    dataDelegated = true,
    compression = 'zstd'
)
PARTITIONED BY (col_2)
COMMENT 'User information table'

Example 4: Create OBS Tables Using Different Data Formats

Create OBS tables using different data formats such as JSON, AVRO.

-- Using JSON format
CREATE TABLE IF NOT EXISTS table_json (
    id      INT,
    name    STRING
)
USING json
OPTIONS (path 'obs://bucketName/jsonPath');

-- Using AVRO format
CREATE TABLE IF NOT EXISTS table_avro (
    id      INT,
    data    STRING
)
USING avro
OPTIONS (path 'obs://bucketName/avroPath');