SnowConvert AI – Hive – CREATE TABLE

Applies to
  • Hive SQL

  • Spark SQL

  • Databricks SQL

Beschreibung

Erzeugt eine neue Tabelle in der aktuellen Datenbank. Sie definieren eine Liste von Spalten, die jeweils Daten eines bestimmten Typs enthalten. Der Eigentümer der Tabelle ist derjenige, der den CREATE TABLE-Befehl erteilt hat.

Weitere Informationen dazu finden Sie in der Dokumentation zu CREATE TABLE.

Grammatikalische Syntax

--DATASOURCE TABLE
CREATE TABLE [ IF NOT EXISTS ] table_identifier
    [ ( col_name1 col_type1 [ COMMENT col_comment1 ], ... ) ]
    USING data_source
    [ OPTIONS ( key1=val1, key2=val2, ... ) ]
    [ PARTITIONED BY ( col_name1, col_name2, ... ) ]
    [ CLUSTERED BY ( col_name3, col_name4, ... ) 
        [ SORTED BY ( col_name [ ASC | DESC ], ... ) ] 
        INTO num_buckets BUCKETS ]
    [ LOCATION path ]
    [ COMMENT table_comment ]
    [ TBLPROPERTIES ( key1=val1, key2=val2, ... ) ]
    [ AS select_statement ]
    
--HIVE FORMAT TABLE
CREATE [ EXTERNAL ] TABLE [ IF NOT EXISTS ] table_identifier
    [ ( col_name1[:] col_type1 [ COMMENT col_comment1 ], ... ) ]
    [ COMMENT table_comment ]
    [ PARTITIONED BY ( col_name2[:] col_type2 [ COMMENT col_comment2 ], ... ) 
        | ( col_name1, col_name2, ... ) ]
    [ CLUSTERED BY ( col_name1, col_name2, ...) 
        [ SORTED BY ( col_name1 [ ASC | DESC ], col_name2 [ ASC | DESC ], ... ) ] 
        INTO num_buckets BUCKETS ]
    [ ROW FORMAT row_format ]
    [ STORED AS file_format ]
    [ LOCATION path ]
    [ TBLPROPERTIES ( key1=val1, key2=val2, ... ) ]
    [ AS select_statement ]
    
--LIKE TABLE
CREATE TABLE [IF NOT EXISTS] table_identifier LIKE source_table_identifier
    USING data_source
    [ ROW FORMAT row_format ]
    [ STORED AS file_format ]
    [ TBLPROPERTIES ( key1=val1, key2=val2, ... ) ]
    [ LOCATION path ]
Copy

IF NOT EXISTS

Beschreibung

Stellt sicher, dass die Tabelle nur erstellt wird, wenn sie noch nicht existiert, und verhindert so Duplizierungen und Fehler in Ihrem SQL-Skript.

Hinweis

Diese Syntax wird in Snowflake vollständig unterstützt.

Gilt für:

  • Hive

  • Spark

  • Databricks

Grammatikalische Syntax

IF NOT EXISTS
Copy

Beispielhafte Quellcode-Muster

Eingabecode:

CREATE TABLE IF NOT EXISTS table1 (
    col1 INTEGER
);
Copy

Ausgabecode:

CREATE TABLE IF NOT EXISTS table1 (
    col1 INTEGER
)
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "sybase",  "convertedOn": "03/19/2024" }}';
Copy

PARTITION BY

Beschreibung

Partitionen werden für die Tabelle auf der Grundlage der angegebenen Spalten erstellt.

Diese Syntax wird in Snowflake nicht benötigt.

Gilt für:

  • Hive

  • Spark

  • Databricks

Grammatikalische Syntax

PARTITIONED BY ( { partition_column [ column_type ] } [, ...] )
Copy

Beispielhafte Quellcode-Muster

Eingabecode:

CREATE TABLE orders (
    order_id INT,
    customer_id INT,
    order_date DATE,
    total_amount DECIMAL(10, 2),
    order_status STRING
)
PARTITIONED BY (order_status);
Copy

Ausgabecode:

CREATE TABLE orders (
    order_id INT,
    customer_id INT,
    order_date DATE,
    total_amount DECIMAL(10, 2),
    order_status STRING
);
Copy

CLUSTERED-BY

Beschreibung

Die für die Tabelle erstellten Partitionen werden anhand der für das Bucketing angegebenen Spalte in feste Buckets eingeteilt.

Diese Syntax wird teilweise unterstützt.

Gilt für:

  • Hive

  • Spark

  • Databricks

Grammatikalische Syntax

CLUSTERED BY (column_name1 [ASC|DESC], ...)
[SORTED BY (sort_column1 [ASC|DESC], ...)]
INTO num_buckets BUCKETS
Copy
  • Die CLUSTERED BY-Klausel, die zur Leistungsoptimierung verwendet wird, wird in Snowflake in CLUSTER BY umgewandelt. Die Leistung zwischen den beiden Architekturen kann variieren.

  • Die SORTED BY-Klausel kann während der Migration entfernt werden, da Snowflake die Sortierung der Daten innerhalb seiner Mikropartitionen automatisch vornimmt.

  • Die INTO BUCKETS-Klausel, eine SparkSQL/Databrick-spezifische Partitionierungseinstellung, sollte vollständig entfernt werden, da sie in Snowflake nicht anwendbar ist.

Beispielhafte Quellcode-Muster

Eingabecode:

CREATE TABLE table_name ( 
column1 data_type, column2 data_type, ... ) USING format CLUSTERED BY (bucketing_column1) SORTED BY (sorting_column1 DESC, sorting_column2 ASC) INTO 10 BUCKETS;
Copy

Ausgabecode:

CREATE TABLE table_name ( column1 data_type, column2 data_type, ... ) USING format
CLUSTER BY (bucketing_column1);
Copy

ROW-FORMAT

Beschreibung

Gibt das Zeilenformat für die Ein- und Ausgabe an.

Diese grammatikalische Syntax wird von Snowflake nicht unterstützt.

Gilt für:

  • Hive

  • Spark

  • Databricks

Grammatikalische Syntax

ROW FORMAT fow_format

row_format:
   { SERDE serde_class [ WITH SERDEPROPERTIES (serde_key = serde_val [, ...] ) ] |
     { DELIMITED [ FIELDS TERMINATED BY fields_terminated_char [ ESCAPED BY escaped_char ] ]
       [ COLLECTION ITEMS TERMINATED BY collection_items_terminated_char ]
       [ MAP KEYS TERMINATED BY map_key_terminated_char ]
       [ LINES TERMINATED BY row_terminated_char ]
       [ NULL DEFINED AS null_char ] } }
Copy

Beispielhafte Quellcode-Muster

Eingabecode:

CREATE TABLE parquet_table ( id INT, data STRING )  STORED AS TEXTFILE LOCATION '/mnt/delimited/target' ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' ESCAPED BY '\\' COLLECTION ITEMS TERMINATED BY ';' MAP KEYS TERMINATED BY ':' LINES TERMINATED BY '\n' NULL DEFINED AS 'NULL_VALUE';
Copy

Ausgabecode:

CREATE TABLE delimited_like_delta LIKE source_delta_table STORED AS TEXTFILE LOCATION '/mnt/delimited/target'
!!!RESOLVE EWI!!! /*** SSC-EWI-HV0002 - THE ROW FORMAT CLAUSE IS NOT SUPPORTED IN SNOWFLAKE ***/!!! ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' ESCAPED BY '\\' COLLECTION ITEMS TERMINATED BY ';' MAP KEYS TERMINATED BY ':' LINES TERMINATED BY '\n' NULL DEFINED AS 'NULL_VALUE';
Copy

STORED-AS

Beschreibung

Dateiformat für die Speicherung von Tabellen.

Diese grammatikalische Syntax wird von Snowflake nicht unterstützt.

Gilt für:

  • Hive

  • Spark

  • Databricks