【问题标题】:How to retrieve all the catalog names , schema names and the table names in a database like snowflake or any such database?如何检索雪花或任何此类数据库等数据库中的所有目录名称、模式名称和表名称?
【发布时间】:2020-11-24 06:37:27
【问题描述】:

我需要删除一些列并将雪花表中的数据大写。 为此,我需要遍历所有目录/数据库、其各自的模式,然后是表。 我需要在 python 中列出目录模式,然后是表,之后我将执行 SQL 查询来进行操作。

如何进行?

1.列出所有目录名称

2.列出所有架构名称

3.列出所有表名

我已经使用 python 雪花连接器建立了连接

【问题讨论】:

标签: python sql database snowflake-cloud-data-platform snowflake-schema


【解决方案1】:

您可能不需要 result_scan。最近,我创建了一个 python 程序来列出 Snowflake 中所有表的所有列。我的要求是验证每一列并计算这些列的一些数值统计信息。我只能使用“显示列”来做到这一点。我已经开源了一些常见的雪花操作,可以在这里找到

https://github.com/Infosys/Snowflake-Python-Development-Framework

您可以克隆此代码,然后使用此框架创建您的 python 程序以列出如下列,然后您可以对列详细信息做任何您想做的事情

##
from utilities.sf_operations import Snowflakeconnection

connection = Snowflakeconnection(profilename ='snowflake_host')
sfconnectionresults = connection.get_snowflake_connection()

sfconnection = sfconnectionresults.get('connection')
statuscode = sfconnectionresults.get('statuscode')
statusmessage = sfconnectionresults.get('statusmessage')

print(sfconnection,statuscode,statusmessage)

snow_sql = 'SHOW COLUMNS;'
queryresult = connection.execute_snowquery(sfconnection,snow_sql);
print(queryresult['result'])
print('column_name|table_name|column_attribute')
print('---------------------------------------------')
for rows in queryresult['result']:
    table_name = rows[0]
    schema_name = rows[1]
    column_name = rows[2]
    column_attribute = rows[3]
    is_Null = rows[4]
    default_Value = rows[5]
    kind = rows[6]
    expression = rows[7]
    comment = rows[8]
    database_name = rows[9]
    autoincrement = rows[10]

    print(column_name+'|'+table_name+'|'+column_attribute)

【讨论】:

    【解决方案2】:

    最简单的方法是按照以下流程进行

    show databases;
    select "name" from table(result_scan(last_query_id()));
    

    这将为您提供数据库列表。把它们放在一个列表中。遍历此列表并在每个项目上执行以下操作:

    use <DBNAME>;
    show schemas;
    select "name" from table(result_scan(last_query_id()));
    

    获取架构列表

    use schema <SchemaName>;
    show tables;
    select "name" from table(result_scan(last_query_id()));
    

    获取表列表,然后运行查询。

    【讨论】:

      【解决方案3】:

      此信息的最佳来源是 Snowflake 提供的 SNOWFLAKE.ACCOUNT_USAGE 分享。您需要向用于连接 Python 的任何角色授予权限。不过,从那里,有以下视图:DATABASESSCHEMATATABLES 等等。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-23
        • 2022-11-24
        • 2023-01-19
        • 1970-01-01
        相关资源
        最近更新 更多