[英]I am trying to access the data stored in a snowflake table using python sql. Below is the columns given below i want to access
Below is the data-sample and i want to access columns value,start.下面是数据样本,我想访问列值,开始。 This data i dumped in one column(DN) of a table (stg)
我将这些数据转储到表(stg)的一列(DN)中
{
"ok": true,
"metrics": [
{
"name": "t_in",
"data": [{"value": 0, "group": {"start": "00:00"}}]
},
{
"name": "t_out",
"data": [{"value": 0,"group": {"start": "00:00"}}]
}
]
}
##consider many lines stored in same column in different rows. ##考虑存储在不同行的同一列中的多行。
Below query only fetched data for name.下面的查询仅获取名称的数据。 I want to access other columns value also.
我也想访问其他列的值。 This query is a part of python script.
此查询是 python 脚本的一部分。
select
replace(DN : metrics[0].name , '"' , '')as metrics_name, #able to get
replace(DN : metrics[2].data , '"' , '')as metrics_data_value,##suggestion needed
replace(DN : metrics.data.start, '"','') as metrics_start, ##suggestion needed
replace(DN : metrics.data.group.finish, '"','') as metrics_finish, ##suggestion needed
from stg
Do i need to iterate over data and group?我需要迭代数据和组吗? If yes, please suggest the code.
如果是,请建议代码。
Here is an example of how to query that data.以下是如何查询该数据的示例。
Set up sample data:设置样本数据:
create or replace transient table test_db.public.stg (DN variant);
insert overwrite into test_db.public.stg (DN)
select parse_json('{
"ok": true,
"metrics": [
{
"name": "t_in",
"data": [
{"value": 0, "group": {"start": "00:00"}}
]
},
{
"name": "t_out",
"data": [
{"value": 0,"group": {"start": "00:00"}}
]
}
]
}');
Select statement example: Select 语句示例:
select
DN:metrics[0].name::STRING,
DN:metrics[1].data,
DN:metrics[1].data[0].group.start::TIME,
DN:metrics[1].data[0].group.finish::TIME
from test_db.public.stg;
Instead of querying individual indexes of the JSON arrays, I think you'll want to use the flatten function which is documented here .而不是查询 JSON arrays 的单个索引,我想你会想要使用 flatten function 记录在这里。
Here is how you do it with the flatten which is what I am guessing you want:这是我猜你想要的扁平化的方法:
select
mtr.value:name::string,
dta.value,
dta.value:group.start::string,
dta.value:group.finish::string
from test_db.public.stg stg,
lateral flatten(input => stg.DN:metrics) mtr,
lateral flatten(input => mtr.value:data) dta
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.