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

Create External Table Using Hive Syntax

Function

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

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.
  • 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.
  • Setting multi-character delimiters when creating a table:
    • Multi-character field delimiters are only supported when ROW FORMAT SERDE is specified as org.apache.hadoop.hive.contrib.serde2.MultiDelimitSerDe.
    • Only Hive OBS tables support specifying multi-character delimiters when creating the table. Hive managed tables do not support specifying multi-character delimiters when creating the table.
    • Tables with multi-character delimiters do not support write statements such as INSERT and IMPORT. To add data, place the data file directly in the corresponding OBS path of the table.
  • 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

    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], ...)]
  [COMMENT table_comment] 
  [PARTITIONED BY (col_name2 col_type2 [COMMENT col_comment2], ...)] 
  [ROW FORMAT row_format]
  [STORED AS file_format] 
  LOCATION 'obs_path'
  [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.
  • LOCATION: Specify the OBS path. This keyword must be specified when creating an OBS table.
  • TBLPROPERTIES: Allows users to add key/value properties to the table.
  • 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 a digit or 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 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

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. 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

OBS table storage format. Supports TEXTFILE, AVRO, ORC, SEQUENCEFILE, RCFILE, PARQUET.

table_comment

No

Table description. Only string constants are supported.

obs_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. 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.

key = value

No

Set specific properties and values for TBLPROPERTIES.

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 TBLPROPERTIES main parameters

Parameter

Mandatory

Description

comment

No

Table description information.

orc.compress

No

A property for ORC storage format tables, used to specify the compression method for ORC storage. Supported values: ZLIB, SNAPPY, NONE.

auto.purge

No

When set to true, deleted or overwritten data will be directly deleted without going through the recycle bin.

Example 1: Create a Basic Hive Format OBS Non-Partitioned Table

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

Example 2: Create an OBS Table with STORED AS TEXTFILE

Create a TEXTFILE format OBS table named table_text.

CREATE TABLE IF NOT EXISTS table_text (
    id      INT,
    name    STRING
)
STORED AS textfile
LOCATION 'obs://bucketName/textFilePath';

Example 3: Create an OBS Table with ROW FORMAT

Create a TEXTFILE type non-partitioned OBS table named table4 with ROW FORMAT set.

CREATE TABLE IF NOT EXISTS table4 (
    col_1   STRING,
    col_2   INT
)
STORED AS textfile
LOCATION 'obs://bucketName/filePath'
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 an OBS Table with SERDE

Create an OBS table named table5 using a custom SERDE with multi-character delimiters.

CREATE TABLE IF NOT EXISTS table5 (
    col_1   STRING,
    col_2   INT
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.contrib.serde2.MultiDelimitSerDe'
WITH SERDEPROPERTIES (
    'field.delim' = '/#'
)
STORED AS textfile
LOCATION 'obs://bucketName/filePath';