SnowConvert AI - Hive - CREATE EXTERNAL TABLE

Applies to
  • Hive SQL

  • Spark SQL

  • Databricks SQL

Description

Tables externes définit une nouvelle table à l’aide d’une source de données. (Référence de langage Spark SQL CREATE 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 ]

L’instruction CREATE EXTERNAL TABLE de Spark/Databricks sera transformée en instruction CREATE EXTERNAL TABLE de Snowflake. Cependant, cette transformation nécessite une intervention de l’utilisateur.

To complete the transformation performed by SnowConvert AI, it is necessary to define a Storage Integration, an External Stage, and (optionally) a Notification Integration that have access to the external source where files are located. Please refer to the following guides on how to set up the connection for each provider:

Considérations importantes pour les transformations présentées sur cette page :

  • L’espace réservé @EXTERNAL_STAGE doit être remplacé par la zone de préparation externe créée après avoir suivi le guide précédent.

  • Il est supposé que la zone de préparation externe pointera vers la racine du compartiment. Il est important d’en tenir compte, car la clause PATTERN générée pour chaque table spécifie les chemins d’accès aux fichiers/dossiers en partant de la base du compartiment. Définir une zone de préparation externe pointant vers un emplacement différent dans le compartiment peut entraîner un comportement indésirable.

  • La clause AUTO_REFRESH = FALSE est générée pour éviter les erreurs. Veuillez noter que l’actualisation automatique des métadonnées des tables externes n’est valable que si le fournisseur Cloud de votre compte Snowflake et le fournisseur du compartiment sont identiques et qu’une intégration de notification a été créée.

Modèles d’échantillons de sources

Créer une table externe avec une liste de colonnes explicite

When the column list is provided, SnowConvert AI will automatically generate the AS expression column options for each column to extract the file values.

Code d’entrée :

CREATE EXTERNAL TABLE IF NOT EXISTS external_table
(
  order_id int,
  date string,
  client_name string,
  total float
)
USING AVRO
LOCATION 'gs://sc_external_table_bucket/folder_with_avro/orders.avro';

Code de sortie :

CREATE EXTERNAL TABLE IF NOT EXISTS external_table
(
  order_id int AS CAST(GET_IGNORE_CASE($1, 'order_id') AS int),
  date string AS CAST(GET_IGNORE_CASE($1, 'date') AS string),
  client_name string AS CAST(GET_IGNORE_CASE($1, 'client_name') AS string),
  total float AS CAST(GET_IGNORE_CASE($1, 'total') AS float)
)
!!!RESOLVE EWI!!! /*** SSC-EWI-0032 - EXTERNAL TABLE REQUIRES AN EXTERNAL STAGE TO ACCESS gs:, DEFINE AND REPLACE THE EXTERNAL_STAGE PLACEHOLDER ***/!!!
LOCATION = @EXTERNAL_STAGE
AUTO_REFRESH = false
FILE_FORMAT = (TYPE = AVRO)
PATTERN = '/sc_external_table_bucket/folder_with_avro/orders.avro'
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "spark",  "convertedOn": "06/18/2025",  "domain": "no-domain-provided" }}';

CREATE EXTERNAL TABLE sans liste de colonnes explicite

When the column list is not provided, Spark automatically detects the schema of the columns from the file structure. To replicate this behavior, SnowConvert AI will generate a USING TEMPLATE clause that makes use of the INFER_SCHEMA function to generate the column definitions.

Étant donné que la fonction INFER_SCHEMA nécessite un format de fichier pour fonctionner, SnowConvert AI générera un format de fichier temporaire à cette fin. Ce format de fichier n’est requis que lors de l’exécution de l’instruction CREATE EXTERNAL TABLE et sera automatiquement détruit à la fin de la session.

Code d’entrée :

CREATE EXTERNAL TABLE IF NOT EXISTS external_table_No_Columns
using AVRO
LOCATION 'gs://sc_external_table_bucket/folder_with_avro/orders.avro';

Code de sortie :

CREATE OR REPLACE TEMPORARY FILE FORMAT SC_HIVE_FORMAT_ORDERS_NO_COLUMNS_FORMAT
TYPE = AVRO;
CREATE EXTERNAL TABLE IF NOT EXISTS hive_format_orders_No_Columns USING TEMPLATE (
SELECT
  ARRAY_AGG(OBJECT_CONSTRUCT('COLUMN_NAME', COLUMN_NAME, 'TYPE', TYPE, 'NULLABLE', NULLABLE, 'EXPRESSION', EXPRESSION))
FROM
  --** SSC-FDM-0035 - THE INFER_SCHEMA FUNCTION REQUIRES A FILE PATH WITHOUT WILDCARDS TO GENERATE THE TABLE TEMPLATE, REPLACE THE FILE_PATH PLACEHOLDER WITH IT **
  TABLE(INFER_SCHEMA(LOCATION => '@EXTERNAL_STAGE/FILE_PATH', FILE_FORMAT => 'SC_HIVE_FORMAT_ORDERS_NO_COLUMNS_FORMAT'))
)
!!!RESOLVE EWI!!! /*** SSC-EWI-0032 - EXTERNAL TABLE REQUIRES AN EXTERNAL STAGE TO ACCESS gs:, DEFINE AND REPLACE THE EXTERNAL_STAGE PLACEHOLDER ***/!!!
LOCATION = @EXTERNAL_STAGE
AUTO_REFRESH = false
FILE_FORMAT = (TYPE = AVRO)
PATTERN = '/sc_external_table_bucket/folder_with_avro/orders.avro'
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "spark",  "convertedOn": "06/18/2025",  "domain": "no-domain-provided" }}';

CREATE EXTERNAL TABLE en utilisant le format Hive

La création de tables externes à l’aide du format Hive est également prise en charge. Un FDM sera ajouté afin d’informer l’utilisateur que l’insertion dans ces tables n’est pas prise en charge.

Code d’entrée :

CREATE EXTERNAL TABLE IF NOT EXISTS External_table_hive_format
(
  order_id int,
  date string,
  client_name string,
  total float
)
stored as AVRO
LOCATION 'gs://sc_external_table_bucket/folder_with_avro/orders.avro';

Code de sortie :

--** SSC-FDM-HV0001 - INSERTING VALUES INTO AN EXTERNAL TABLE IS NOT SUPPORTED IN SNOWFLAKE **
CREATE EXTERNAL TABLE IF NOT EXISTS hive_format_orders_Andres
(
  order_id int AS CAST(GET_IGNORE_CASE($1, 'order_id') AS int),
  date string AS CAST(GET_IGNORE_CASE($1, 'date') AS string),
  client_name string AS CAST(GET_IGNORE_CASE($1, 'client_name') AS string),
  total float AS CAST(GET_IGNORE_CASE($1, 'total') AS float)
)
!!!RESOLVE EWI!!! /*** SSC-EWI-0032 - EXTERNAL TABLE REQUIRES AN EXTERNAL STAGE TO ACCESS gs:, DEFINE AND REPLACE THE EXTERNAL_STAGE PLACEHOLDER ***/!!!
LOCATION = @EXTERNAL_STAGE
AUTO_REFRESH = false
FILE_FORMAT = (TYPE = AVRO)
PATTERN = '/sc_external_table_bucket/folder_with_avro/orders.avro'
COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "spark",  "convertedOn": "06/18/2025",  "domain": "no-domain-provided" }}';

Problèmes connus

1. Tables externes avec formats de fichier non pris en charge

Snowflake prend en charge les formats Spark suivants :

  • CSV

  • PARQUET

  • ORC

  • XML

  • JSON

  • AVRO

Les autres formats seront signalés comme non pris en charge.

2. Options de table non prises en charge

Certaines options de table ne sont pas prises en charge par SnowConvert AI et sont signalées par un EWI.

Code d’entrée :

CREATE EXTERNAL TABLE IF NOT EXISTS hive_format_orders_Andres
(
  order_id int,
  date string,
  client_name string,
  total float
)
using AVRO
LOCATION 'gs://sc_external_table_bucket/folder_with_avro/orders.avro'
Tblproperties (
    'unsupported_table_option' = 'value'
);

Code de sortie :

CREATE EXTERNAL TABLE IF NOT EXISTS hive_format_orders_Andres
(
  order_id int AS CAST(GET_IGNORE_CASE($1, 'order_id') AS int),
  date string AS CAST(GET_IGNORE_CASE($1, 'date') AS string),
  client_name string AS CAST(GET_IGNORE_CASE($1, 'client_name') AS string),
  total float AS CAST(GET_IGNORE_CASE($1, 'total') AS float)
)
    !!!RESOLVE EWI!!! /*** SSC-EWI-0032 - EXTERNAL TABLE REQUIRES AN EXTERNAL STAGE TO ACCESS gs:, DEFINE AND REPLACE THE EXTERNAL_STAGE PLACEHOLDER ***/!!!
    LOCATION = @EXTERNAL_STAGE
    AUTO_REFRESH = false
    PATTERN = '/sc_external_table_bucket/folder_with_avro/orders.avro'
    FILE_FORMAT = (TYPE = AVRO)
    !!!RESOLVE EWI!!! /*** SSC-EWI-0016 - SNOWFLAKE DOES NOT SUPPORT THE OPTIONS: 'UNSUPPORTED_TABLE_OPTION'. ***/!!!
    TBLPROPERTIES (
  'unsupported_table_option' = 'value'
    )
    COMMENT = '{ "origin": "sf_sc", "name": "snowconvert", "version": {  "major": 0,  "minor": 0,  "patch": "0" }, "attributes": {  "component": "spark",  "convertedOn": "06/19/2025",  "domain": "no-domain-provided" }}';