【问题标题】:Loading CSV data to Snowflake table将 CSV 数据加载到雪花表
【发布时间】:2020-11-25 15:20:56
【问题描述】:

自 CSV 文件以来,尝试将以下数据加载到 SnowFlake 表中时,列拆分为多列。

列数据:

{"Department":"Mens 
Wear","Departmentid":"10.1;20.1","customername":"john4","class":"tops wear","subclass":"sweat shirts","product":"North & Face 2 Bangle","style":"Sweat shirt hoodie - Large - Black"}

有没有其他方法可以将数据加载到单列中。

【问题讨论】:

  • 你的文件格式/你的复制到声明是什么?
  • 您可以创建一个带有 VARIANT 列的表,然后插入解析为 JSON 的数据。所以VARIANT 在这里,PARSE_JSON 在这里。
  • 正如@Marcel 所说,请分享整行,而不仅仅是一列数据 - 以及您的加载方式

标签: snowflake-cloud-data-platform


【解决方案1】:

最好的解决方案是在 CSV 文件中使用不同的分隔符而不是逗号。如果不可能,那么您可以使用不存在的分隔符来获取数据以将整行作为一列,然后对其进行解析。当然它不会像原生加载那样有效:

cat test.csv 
1,2020-10-12,Gokhan,{"Department":"Mens Wear","Departmentid":"10.1;20.1","customername":"john4","class":"tops wear","subclass":"sweat shirts","product":"North & Face 2 Bangle","style":"Sweat shirt hoodie - Large - Black"}

create file format csvfile type=csv FIELD_DELIMITER='NONEXISTENT';        

select $1 from @my_stage (file_format => csvfile );

create table testtable( id number, d1 date, name varchar, v variant );

copy into testtable from (
select 
split( split($1,',{')[0], ',' )[0], 
split( split($1,',{')[0], ',' )[1], 
split( split($1,',{')[0], ',' )[2], 
parse_json( '{' || split($1,',{')[1]  )  
from @my_stage (file_format => csvfile )
);

select * from testtable;


+----+------------+--------+-----------------------------------------------------------------+
| ID |     D1     |  NAME  |                                V                                |
+----+------------+--------+-----------------------------------------------------------------+
|  1 | 2020-10-12 | Gokhan | { "Department": "Mens Wear", "Departmentid": "10.1;20.1", ... } |
+----+------------+--------+-----------------------------------------------------------------+

【讨论】:

    猜你喜欢
    • 2020-11-13
    • 1970-01-01
    • 2020-04-21
    • 2020-10-07
    • 1970-01-01
    • 2020-09-27
    • 2021-07-18
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多