当授予Monitor Monitor帐户特权时,是否可以使用python查询计费使用情况?

时间:2019-10-21 10:16:20

标签: python sql snowflake-data-warehouse

我想查询雪花帐单信息并将其汇总在Jupyter Notebook中。

我目前正在使用Snowflake-SQLAlchemy和python-snowflake-connector连接到Snowflake和查询数据库。我想查询该帐户上不同用户/仓库的信用使用情况。我已经获得了“监视帐户”特权,因此可以在UI中看到此信息,但是,看不到如何直接查询此信息。

我尝试了以下查询,但是由于我没有AccountAdmin,因此无法直接查询此架构。

from sqlalchemy import create_engine
from snowflake.sqlalchemy import URL
import pandas as pd
import numpy as np
import snowflake.connector

query = """
select start_time::date as usage_date,
       warehouse_name,
       sum(credits_used) as total_credits_used
from warehouse_metering_history
where start_time >= date_trunc(month, current_date)
group by 1,2
order by 2,1;"""

result_df = pd.read_sql_query(query, engine)

预期结果是一个数据帧,其中包含与在帐户,账单和使用情况下看到的数据相似的数据。该表列出了一段时间内的用户和信用使用情况。目前,我无法返回任何此类信息。

1 个答案:

答案 0 :(得分:2)

您需要确保以正确的角色(已授予监视帐户privs的角色)登录。如果您没有像在python脚本中那样指定要登录的角色,则Snowflake将使用您的default_role,而该角色可能没有正确的特权。

此外,您可以尝试完全限定warehouse_metering_history表,使其包含表的数据库和架构,如下所示:

select start_time::date as usage_date,
       warehouse_name,
       sum(credits_used) as total_credits_used
from snowflake.account_usage.warehouse_metering_history -- Here fully qualify the table 
where start_time >= date_trunc(month, current_date)
group by 1,2
order by 2,1;

尝试通过Web界面运行相同的查询,看看会得到什么。