【问题标题】:Create External Table pointing to S3创建指向 S3 的外部表
【发布时间】:2020-10-29 08:31:01
【问题描述】:

我们如何使用 Snowflake sql 创建一个指向 S3 中目录的外部表?以下是我迄今为止尝试过的代码,但没有奏效。非常感谢任何帮助。

create external table my_table
(
column1 varchar(4000),
column2 varchar(4000)
)
LOCATION 's3a://<externalbucket>'

注意:我在 S3 存储桶中的文件是一个 csv 文件(逗号分隔,双引号括起来并带有标题)。

【问题讨论】:

标签: sql amazon-s3 snowflake-cloud-data-platform external-tables snowsql


【解决方案1】:

您需要将您的位置更新为外部阶段,包括 file_format 参数,并包括列的正确表达式。
位置参数:

Specifies the external stage where the files containing data to be read are staged.  

此外,您还需要定义file_format

https://docs.snowflake.com/en/sql-reference/sql/create-external-table.html#required-parameters

所以你的陈述应该更像这样:

create external table my_table
(
column1 varchar as (value:c1::varchar),
column2 varchar as (value:c2::varchar)
)
location = @[namespace.]ext_stage_name[/path]
file_format = (type = CSV)

您可能需要在文件格式中定义其他参数以正确处理您的文件

【讨论】:

【解决方案2】:

最后我解决了这个问题。发布此答案是为了使答案易于理解,尤其是对于初学者。

假设我在 S3 位置有一个 csv 文件,格式如下。

第 1 步

创建一种文件格式,您可以在其中定义文件的类型、字段分隔符、用双引号括起来的数据、跳过文件头等。

create or replace file format schema_name.pipeformat
type = 'CSV' 
field_delimiter = '|' 
FIELD_OPTIONALLY_ENCLOSED_BY = '"'
skip_header = 1

https://docs.snowflake.com/en/sql-reference/sql/create-file-format.html

第 2 步

创建一个阶段以指定 S3 详细信息和文件格式。

create or replace stage schema_name.stage_name 
url='s3://<path where file is kept>'
credentials=(aws_key_id='****' aws_secret_key='****')
file_format = pipeformat

https://docs.snowflake.com/en/sql-reference/sql/create-stage.html#required-parameters

第 3 步

根据舞台名称和文件格式创建外部表。

create or replace external table schema_name.table_name 
(
    RollNumber INT as  (value:c1::int), 
    Name varchar(20) as ( value:c2::varchar), 
    Marks int as (value:c3::int)
)
with location = @stage_name
file_format = pipeformat

https://docs.snowflake.com/en/sql-reference/sql/create-external-table.html

第 4 步

现在您应该可以从外部表中查询了。

select * 
from schema_name.table_name

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多