将 JSON 数据加载到 Snow flake table
Load JSON Data into Snow flake table
我的数据如下:
[ {
"InvestorID": "10014-49",
"InvestorName": "Blackstone",
"LastUpdated": "11/23/2021"
},
{
"InvestorID": "15713-74",
"InvestorName": "Bay Grove Capital",
"LastUpdated": "11/19/2021"
}]
到目前为止尝试过:
CREATE OR REPLACE TABLE STG_PB_INVESTOR (
Investor_ID string, Investor_Name string,Last_Updated DATETIME
); Created table
create or replace file format investorformat
type = 'JSON'
strip_outer_array = true;
created file format
create or replace stage investor_stage
file_format = investorformat;
created stage
copy into STG_PB_INVESTOR from @investor_stage
我遇到一个错误:
SQL 编译错误:JSON 文件格式只能产生一列类型变体或对象或数组。如果要加载多列,请使用 CSV 文件格式。
您应该将 JSON 数据加载到 table 中,其中只有一个列是 VARIANT。进入 Snowflake 后,您可以通过视图或随后的 table 加载来展平数据。您也可以在 COPY 语句中使用 SELECT 的方式将其展平,但这往往会慢一些。
尝试这样的事情:
CREATE OR REPLACE TABLE STG_PB_INVESTOR_JSON (
var variant
);
create or replace file format investorformat
type = 'JSON'
strip_outer_array = true;
create or replace stage investor_stage
file_format = investorformat;
copy into STG_PB_INVESTOR_JSON from @investor_stage;
create or replace table STG_PB_INVESTOR as
SELECT
var:InvestorID::string as Investor_id,
var:InvestorName::string as Investor_Name,
TO_DATE(var:LastUpdated::string,'MM/DD/YYYY') as last_updated
FROM STG_PB_INVESTOR_JSON;
我的数据如下:
[ {
"InvestorID": "10014-49",
"InvestorName": "Blackstone",
"LastUpdated": "11/23/2021"
},
{
"InvestorID": "15713-74",
"InvestorName": "Bay Grove Capital",
"LastUpdated": "11/19/2021"
}]
到目前为止尝试过:
CREATE OR REPLACE TABLE STG_PB_INVESTOR (
Investor_ID string, Investor_Name string,Last_Updated DATETIME
); Created table
create or replace file format investorformat
type = 'JSON'
strip_outer_array = true;
created file format
create or replace stage investor_stage
file_format = investorformat;
created stage
copy into STG_PB_INVESTOR from @investor_stage
我遇到一个错误:
SQL 编译错误:JSON 文件格式只能产生一列类型变体或对象或数组。如果要加载多列,请使用 CSV 文件格式。
您应该将 JSON 数据加载到 table 中,其中只有一个列是 VARIANT。进入 Snowflake 后,您可以通过视图或随后的 table 加载来展平数据。您也可以在 COPY 语句中使用 SELECT 的方式将其展平,但这往往会慢一些。
尝试这样的事情:
CREATE OR REPLACE TABLE STG_PB_INVESTOR_JSON (
var variant
);
create or replace file format investorformat
type = 'JSON'
strip_outer_array = true;
create or replace stage investor_stage
file_format = investorformat;
copy into STG_PB_INVESTOR_JSON from @investor_stage;
create or replace table STG_PB_INVESTOR as
SELECT
var:InvestorID::string as Investor_id,
var:InvestorName::string as Investor_Name,
TO_DATE(var:LastUpdated::string,'MM/DD/YYYY') as last_updated
FROM STG_PB_INVESTOR_JSON;