简体   繁体   English

我们可以将外部 JSON 文件复制到雪花中吗?

[英]Can we COPY External JSON File into Snowflake?

I am trying to load External JSON File from Azure Blob Storage to Snowflake.我正在尝试将外部 JSON 文件从 Azure Blob 存储加载到雪花。 I created the table LOCATION_DETAILS with all columns as Variant.我创建了 LOCATION_DETAILS 表,所有列都作为 Variant。 When I try to load into the table, I am getting the below error:当我尝试加载到表中时,出现以下错误:

从外部 JSON 文件错误详细信息复制到表中

Can anyone help me on this?谁可以帮我这个事?

You need to create a file format and mention the type of file and other specification like below: create or replace file format myjsonformat type = 'JSON' strip_outer_array = true;您需要创建文件格式并提及文件类型和其他规范,如下所示:创建或替换文件格式 myjsonformat type = 'JSON' strip_outer_array = true;

And then try to load the file it will work.然后尝试加载它会工作的文件。

When I use external data for Snowflake, I like to create stages that are linked to the BlobStorage (in this case), it's easy and you can do everything really easy and transparent, just as if it would be local data.当我为 Snowflake 使用外部数据时,我喜欢创建链接到 BlobStorage 的阶段(在这种情况下),这很容易,您可以做任何事情都非常简单和透明,就像它是本地数据一样。

Create the stage linked to the blobstorage like this:像这样创建链接到 blobstorage 的阶段:

CREATE OR REPLACE STAGE "<DATABASE>"."<SCHEMA>"."<STAGE_NAME>"
  URL='azure://demostorage178.blob.core.windows.net/democontainer'
  CREDENTIALS=(AZURE_SAS_TOKEN='***********************************************')
  FILE_FORMAT = (TYPE = JSON);

After that, you can list what is in the blobstorage fromo snowflake like this:之后,您可以像这样列出来自雪花的 blobstorage 中的内容:

list @"<DATABASE>"."<SCHEMA>"."<STAGE_NAME>";

Or like this:或者像这样:

use database "<DATABASE>";
use schema "<SCHEMA>";
SELECT *  FROM @"STAGE_NAME"/sales.json;

If you need to create the table, use this:如果您需要创建表,请使用以下命令:

create or replace table "<DATABASE>"."<SCHEMA>"."<TABLE>" (src VARIANT);

And you can COPY your data like this (for a single file):您可以像这样复制您的数据(对于单个文件):

copy into "<DATABASE>"."<SCHEMA>"."<TABLE>" from @"<STAGE_NAME>"/sales.json;

Finally, use this for all new data that you get in your stage.最后,将它用于您在阶段中获得的所有新数据。 Note: You don't need to erase previous data, it will ignore it and will load only the new one.注意:您不需要删除以前的数据,它会忽略它并仅加载新数据。

copy into "<DATABASE>"."<SCHEMA>"."<TABLE>" from @"STAGE_NAME";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM