您可以使用具有 VARIANT 类型的单列的表来加载 json 文件。
这是一个例子:
/* Create a JSON file format that strips the outer array. */
create or replace file format json_format
type = 'JSON'
strip_outer_array = true;
/* Create an internal stage that references the JSON file format. */
create or replace stage mystage
file_format = json_format;
/* Stage the JSON file. */
put file:///tmp/sales.json @mystage auto_compress=true;
/* Create a target table for the JSON data. */
create or replace table house_sales (src variant);
/* Copy the JSON data into the target table. */
copy into house_sales
from @mystage/sales.json.gz;
select * from house_sales;
+---------------------------+
| SRC |
|---------------------------|
| { |
| "location": { |
| "city": "Lexington", |
| "zip": "40503" |
| }, |
| "price": "75836", |
| "sale_date": "4-25-16", |
| "sq__ft": "1000", |
| "type": "Residential" |
| } |
| { |
| "location": { |
| "city": "Belmont", |
| "zip": "02478" |
| }, |
| "price": "92567", |
| "sale_date": "6-18-16", |
| "sq__ft": "1103", |
| "type": "Residential" |
| } |
| { |
| "location": { |
| "city": "Winchester", |
| "zip": "01890" |
| }, |
| "price": "89921", |
| "sale_date": "1-31-16", |
| "sq__ft": "1122", |
| "type": "Condo" |
| } |
+---------------------------+
有关更多信息,请查看此处
您也可以直接查询暂存的 JSON 文件,请参见以下示例:
create or replace file format my_json_format type = 'json';
select * from @~/example_2.json.gz
(
file_format => my_json_format
);
我得到:
{
"quiz": {
"maths": {
"q1": {
"answer": "12",
"options": [
"10",
"11",
"12",
"13"
],
"question": "5 + 7 = ?"
},
"q2": {
"answer": "4",
"options": [
"1",
"2",
"3",
"4"
],
"question": "12 - 8 = ?"
}
},
"sport": {
"q1": {
"answer": "Huston Rocket",
"options": [
"New York Bulls",
"Los Angeles Kings",
"Golden State Warriros",
"Huston Rocket"
],
"question": "Which one is correct team name in NBA?"
}
}
}
}
我也可以这样做:
select parse_json($1):quiz.maths from @~/example_2.json.gz
(
file_format => my_json_format
);
我得到:
{
"q1": {
"answer": "12",
"options": [
"10",
"11",
"12",
"13"
],
"question": "5 + 7 = ?"
},
"q2": {
"answer": "4",
"options": [
"1",
"2",
"3",
"4"
],
"question": "12 - 8 = ?"
}
}