CREATE-EXTERNAL-TABLE

Name

CREATE EXTERNAL TABLE

Description

This statement is used to create an external table, see CREATE TABLE for the specific syntax.

Which type of external table is mainly identified by the ENGINE type, currently MYSQL, BROKER, HIVE, ICEBERG, HUDI are optional

  1. If it is mysql, you need to provide the following information in properties:

    1. PROPERTIES (
    2. "host" = "mysql_server_host",
    3. "port" = "mysql_server_port",
    4. "user" = "your_user_name",
    5. "password" = "your_password",
    6. "database" = "database_name",
    7. "table" = "table_name"
    8. )

    and there is an optional propertiy “charset” which can set character fom mysql connection, default value is “utf8”. You can set another value “utf8mb4” instead of “utf8” when you need.

    Notice:

    • “table_name” in “table” entry is the real table name in mysql. The table_name in the CREATE TABLE statement is the name of the mysql table in Doris, which can be different.

    • The purpose of creating a mysql table in Doris is to access the mysql database through Doris. Doris itself does not maintain or store any mysql data.

  2. If it is a broker, it means that the access to the table needs to pass through the specified broker, and the following information needs to be provided in properties:

    1. PROPERTIES (
    2. "broker_name" = "broker_name",
    3. "path" = "file_path1[,file_path2]",
    4. "column_separator" = "value_separator"
    5. "line_delimiter" = "value_delimiter"
    6. )

    In addition, you need to provide the Property information required by the Broker, and pass it through the BROKER PROPERTIES, for example, HDFS needs to pass in

    1. BROKER PROPERTIES(
    2. "username" = "name",
    3. "password" = "password"
    4. )

    According to different Broker types, the content that needs to be passed in is also different.

    Notice:

    • If there are multiple files in “path”, separate them with comma [,]. If the filename contains a comma, use %2c instead. If the filename contains %, use %25 instead
    • Now the file content format supports CSV, and supports GZ, BZ2, LZ4, LZO (LZOP) compression formats.
  3. If it is hive, you need to provide the following information in properties:

    1. PROPERTIES (
    2. "database" = "hive_db_name",
    3. "table" = "hive_table_name",
    4. "hive.metastore.uris" = "thrift://127.0.0.1:9083"
    5. )

    Where database is the name of the library corresponding to the hive table, table is the name of the hive table, and hive.metastore.uris is the address of the hive metastore service.

  4. In case of iceberg, you need to provide the following information in properties:

    1. PROPERTIES (
    2. "iceberg.database" = "iceberg_db_name",
    3. "iceberg.table" = "iceberg_table_name",
    4. "iceberg.hive.metastore.uris" = "thrift://127.0.0.1:9083",
    5. "iceberg.catalog.type" = "HIVE_CATALOG"
    6. )

    Where database is the library name corresponding to Iceberg; table is the corresponding table name in Iceberg; hive.metastore.uris is the hive metastore service address; catalog.type defaults to HIVE_CATALOG. Currently only HIVE_CATALOG is supported, more Iceberg catalog types will be supported in the future.

  5. In case of hudi, you need to provide the following information in properties:

    1. PROPERTIES (
    2. "hudi.database" = "hudi_db_in_hive_metastore",
    3. "hudi.table" = "hudi_table_in_hive_metastore",
    4. "hudi.hive.metastore.uris" = "thrift://127.0.0.1:9083"
    5. )

    Where hudi.database is the corresponding database name in HiveMetaStore; hudi.table is the corresponding table name in HiveMetaStore; hive.metastore.uris is the hive metastore service address;

Example

  1. Create a MYSQL external table

    Create mysql table directly from outer table information

    1. CREATE EXTERNAL TABLE example_db.table_mysql
    2. (
    3. k1 DATE,
    4. k2 INT,
    5. k3 SMALLINT,
    6. k4 VARCHAR(2048),
    7. k5 DATETIME
    8. )
    9. ENGINE=mysql
    10. PROPERTIES
    11. (
    12. "host" = "127.0.0.1",
    13. "port" = "8239",
    14. "user" = "mysql_user",
    15. "password" = "mysql_passwd",
    16. "database" = "mysql_db_test",
    17. "table" = "mysql_table_test",
    18. "charset" = "utf8mb4"
    19. )

    Create mysql table through External Catalog Resource

    1. # Create Resource first
    2. CREATE EXTERNAL RESOURCE "mysql_resource"
    3. PROPERTIES
    4. (
    5. "type" = "odbc_catalog",
    6. "user" = "mysql_user",
    7. "password" = "mysql_passwd",
    8. "host" = "127.0.0.1",
    9. "port" = "8239"
    10. );
    11. # Then create mysql external table through Resource
    12. CREATE EXTERNAL TABLE example_db.table_mysql
    13. (
    14. k1 DATE,
    15. k2 INT,
    16. k3 SMALLINT,
    17. k4 VARCHAR(2048),
    18. k5 DATETIME
    19. )
    20. ENGINE=mysql
    21. PROPERTIES
    22. (
    23. "odbc_catalog_resource" = "mysql_resource",
    24. "database" = "mysql_db_test",
    25. "table" = "mysql_table_test"
    26. )
  2. Create a broker external table with data files stored on HDFS, the data is split with “|”, and “\n” is newline

    1. CREATE EXTERNAL TABLE example_db.table_broker (
    2. k1 DATE,
    3. k2 INT,
    4. k3 SMALLINT,
    5. k4 VARCHAR(2048),
    6. k5 DATETIME
    7. )
    8. ENGINE=broker
    9. PROPERTIES (
    10. "broker_name" = "hdfs",
    11. "path" = "hdfs://hdfs_host:hdfs_port/data1,hdfs://hdfs_host:hdfs_port/data2,hdfs://hdfs_host:hdfs_port/data3%2c4",
    12. "column_separator" = "|",
    13. "line_delimiter" = "\n"
    14. )
    15. BROKER PROPERTIES (
    16. "username" = "hdfs_user",
    17. "password" = "hdfs_password"
    18. )
  3. Create a hive external table

    1. CREATE TABLE example_db.table_hive
    2. (
    3. k1 TINYINT,
    4. k2 VARCHAR(50),
    5. v INT
    6. )
    7. ENGINE=hive
    8. PROPERTIES
    9. (
    10. "database" = "hive_db_name",
    11. "table" = "hive_table_name",
    12. "hive.metastore.uris" = "thrift://127.0.0.1:9083"
    13. );
  4. Create an Iceberg skin

    1. CREATE TABLE example_db.t_iceberg
    2. ENGINE=ICEBERG
    3. PROPERTIES (
    4. "iceberg.database" = "iceberg_db",
    5. "iceberg.table" = "iceberg_table",
    6. "iceberg.hive.metastore.uris" = "thrift://127.0.0.1:9083",
    7. "iceberg.catalog.type" = "HIVE_CATALOG"
    8. );
  5. Create an Hudi external table

    create hudi table without schema(recommend)

    1. CREATE TABLE example_db.t_hudi
    2. ENGINE=HUDI
    3. PROPERTIES (
    4. "hudi.database" = "hudi_db_in_hive_metastore",
    5. "hudi.table" = "hudi_table_in_hive_metastore",
    6. "hudi.hive.metastore.uris" = "thrift://127.0.0.1:9083"
    7. );

    create hudi table with schema

    1. CREATE TABLE example_db.t_hudi (
    2. `id` int NOT NULL COMMENT "id number",
    3. `name` varchar(10) NOT NULL COMMENT "user name"
    4. )
    5. ENGINE=HUDI
    6. PROPERTIES (
    7. "hudi.database" = "hudi_db_in_hive_metastore",
    8. "hudi.table" = "hudi_table_in_hive_metastore",
    9. "hudi.hive.metastore.uris" = "thrift://127.0.0.1:9083"
    10. );

Keywords

  1. CREATE, EXTERNAL, TABLE

Best Practice