All Products
Search
Document Center

Automatically create tables based on OSS files

Last Updated: Jul 25, 2019

Syntax

  1. CREATE EXTERNAL TABLE table_name LIKE MAPPING ('oss://path/to/target/dir')

To create an OSS table, you only need to provide the table name and target file path.

Prerequisites

To facilitate the demonstration, complete the following steps to prepare the test data in OSS:

  1. Sign up for OSS.

  2. Create a bucket.

  3. Upload data files.

    Upload the json1.txt, json2.txt, and json3.txt files to the my_new_table directory in OSS. The json1.txt and json2.txt files have the same structure. The data stored in json1.txt, json2.txt, and json3.txt is as follows.

    1. {"id": 123, "name": "jack", "c3": "2001-02-03 12:34:56"}
    2. {"id": 456, "name": "rose", "c3": "1906-04-18 05:12:00"}
    1. {"id": 789, "name": "tom", "c3": "2001-02-03 12:34:56"}
    2. {"id": 234, "name": "alice", "c3": "1906-04-18 05:12:00"}
    1. {"id": 0122, "name": "bond", "address": "hangzhou", "c3":"2012-02-03 12:34:56"}
    2. {"id": 0133, "name": "saly", "address": "beijing", "c3":"2012-04-18 05:12:00"}
  4. Create an OSS schema.

    All tables in the example use the following OSS schema:

    1. CREATE SCHEMA my_new_table with DBPROPERTIES(
    2. catalog='oss',
    3. location= 'oss://bucker-name/my_new_table/'
    4. );

Example 1: Create a table for all files in the folder

The json1.txt and json2.txt files have the same structure, so you can run CREATE TABLE LIKE MAPPING to create a table for reading data from the two files.

Create a table:

  1. CREATE EXTERNAL TABLE my_new_table LIKE
  2. MAPPING ('oss://bucker-name/my_new_table/');

View table creation details:

  1. SHOW CREATE TABLE my_new_table
  2. +------+-------------------+---+------+
  3. CREATE EXTERNAL TABLE `my_new_table`.`my_new_table` (
  4. `c3` varchar(1024) COMMENT 'from deserializer',
  5. `id` int COMMENT 'from deserializer',
  6. `name` varchar(1024) COMMENT 'from deserializer'
  7. )
  8. STORED AS `JSON`
  9. LOCATION 'oss://bucker-name/my_new_table'
  10. TBLPROPERTIES (
  11. 'create.table.like.file' = 'oss://bucket_name/my_new_table/'
  12. )

Query table data:

  1. SELECT * FROM my_new_table.my_new_table
  2. |----|------|---------------------|
  3. | id | name | c3 |
  4. | 123| jack | 2001-02-03 12:34:560|
  5. | 456| rose | 1906-04-18 05:12:00 |
  6. | 789| tome | 2001-02-03 12:34:56 |
  7. | 234| alice|1906-04-18 05:12:00 |

Example 2: Create a table for a specified file

The json3.txt file has a different structure from json1.txt and json2.txt, so you can create a table for json3.txt only.

Create a table:

  1. CREATE EXTERNAL TABLE my_new_table LIKE
  2. MAPPING ('oss://bucker-name/my_new_table/json3.txt');

View table creation details:

  1. SHOW CREATE TABLE my_new_table
  2. +------+-------------------+---+------+
  3. CREATE EXTERNAL TABLE `my_new_table`.`my_new_table` (
  4. `address` varchar(1024) COMMENT 'from deserializer',
  5. `c3` varchar(1024) COMMENT 'from deserializer',
  6. `id` int COMMENT 'from deserializer',
  7. `name` varchar(1024) COMMENT 'from deserializer'
  8. )
  9. STORED AS `JSON`
  10. LOCATION 'oss://bucker-name/my_new_table/json3.txt'
  11. TBLPROPERTIES (
  12. 'create.table.like.file' = 'oss://bucker-name/my_new_table/json3.txt'
  13. )

Query table data:

  1. SELECT * FROM my_new_table.my_new_table
  2. |-----|------|--------|--------------------|
  3. | id | name | address| c3 |
  4. | 0122| bond |hangzhou|2012-02-03 12:34:560|
  5. | 0133| saly |beijing |2012-04-18 05:12:00 |