从补丁 198 开始,Amazon Redshift 将不再支持创建新的 Python UDF。现有的 Python UDF 将继续正常运行至 2026 年 6 月 30 日。有关更多信息,请参阅博客文章
将 Amazon Redshift 驱动程序元数据 API 用于应用程序和工具
对于连接到 Amazon Redshift 的应用程序和工具,例如商业智能工具或查询编辑器,我们建议您使用 Amazon Redshift JDBC 2.x、ODBC 2.x 或 Python 驱动程序提供的驱动程序元数据 API 来发现有关数据仓库对象的元数据,包括数据库、架构、表、列和数据类型。作为替代方案,您可以使用 Amazon Redshift SHOW 命令。
使用驱动程序元数据 API 可获得以下优势:
-
符合规范。JDBC 和 ODBC 驱动程序实施了标准的元数据接口(在 JDBC 为
DatabaseMetaData,在 ODBC 中为SQLTables和SQLColumns)。由于 Python 的 DB-API(PEP 249)未定义元数据 API 规范,因此 Amazon Redshift Python 驱动程序遵循 JDBC DatabaseMetaData 规范,提供了等效的方法,例如get_tables()、get_columns()和get_schemas()。这些 API 遵循明确定义的规范,因此您的集成代码是可移植的。随着 Amazon Redshift 不断发展其内部系统表,您的应用程序不需要更改。 -
性能优化。驱动程序元数据 API 经过优化,可高效地返回元数据。AWS 继续投资改进驱动程序元数据 API 性能。
-
向前兼容。Amazon Redshift 遵循 JDBC、ODBC 和 Python 连接器规范。当您根据这些标准 API 进行编码时,应用程序受到保护,而不受底层系统目录结构更改所影响。
示例:使用 JDBC DatabaseMetaData.getTables() 检索表元数据
DatabaseMetaData dbmd = connection.getMetaData(); // getTables(catalog, schemaPattern, tableNamePattern, types) // catalog: "test" — filters to the database named "test" // schemaPattern: "test_pattern" — filters schemas matching this pattern (supports SQL wildcards % and _) // tableNamePattern: null — no filter, returns all table names // types: {"TABLE", "EXTERNAL TABLE"} — only return regular tables and external tables ResultSet rs = dbmd.getTables("test", "test_pattern", null, new String[] {"TABLE", "EXTERNAL TABLE"});
示例:使用 Python cursor.get_columns() 检索列元数据
cursor: redshift_connector.Cursor = conn.cursor() # get_columns(catalog, schema_pattern, table_name_pattern, column_name_pattern) # catalog: 'test' — filters to the database named "test" # schema_pattern: 'test_pattern' — filters schemas matching this pattern (supports SQL wildcards % and _) # table_name_pattern: 'testabc' — filters to the table named "testabc" # column_name_pattern: '%' — wildcard, returns all columns in the matching table result: tuple = cursor.get_columns('test', 'test_pattern', 'testabc', '%')
示例:使用 ODBC SQLPrimaryKeys() 检索主键元数据
// SQLPrimaryKeys(hstmt, catalog, catalog_len, schema, schema_len, table, table_len) // catalog: "test" — filters to the database named "test" // schema: "test_schema" — filters to the schema named "test_schema" // table: "test_table" — retrieves primary key columns for this table // Note: Unlike getTables/getColumns, SQLPrimaryKeys does NOT support wildcard patterns. retcode = SQLPrimaryKeys(hstmt, (SQLCHAR *)"test", SQL_NTS, (SQLCHAR *)"test_schema", SQL_NTS, (SQLCHAR *)"test_table", SQL_NTS); while (SQL_SUCCEEDED(retcode = SQLFetch(hstmt))) { for (i = 1; i <= columns; i++) { retcode = SQLGetData(hstmt, i, SQL_C_CHAR, buf, sizeof(buf), &indicator); } }
示例:使用 ODBC SQLTables() 列出数据库和架构
ODBC API 未提供用于列出目录或架构的单独函数。相反,您可以使用 SQLTables() 的特殊调用约定来检索此信息。
列出所有数据库(目录)
在 CatalogName 设置为 SQL_ALL_CATALOGS 的情况下调用 SQLTables()。将 SchemaName 和 TableName 设置为空字符串。结果集仅在 TABLE_CAT 列中返回有效值。所有其它列都包含 NULL。
// List all catalogs (databases) available on the data source. retcode = SQLTables(hstmt, (SQLCHAR *)SQL_ALL_CATALOGS, SQL_NTS, // CatalogName = "%" (SQL_ALL_CATALOGS) (SQLCHAR *)"", 0, // SchemaName = "" (empty string) (SQLCHAR *)"", 0, // TableName = "" (empty string) NULL, 0); // TableType = NULL (not filtered)
列出所有架构
在 SchemaName 设置为 SQL_ALL_SCHEMAS 的情况下调用 SQLTables()。将 CatalogName 和 TableName 设置为空字符串。
// List all schemas available on the data source. retcode = SQLTables(hstmt, (SQLCHAR *)"", 0, // CatalogName = "" (empty string) (SQLCHAR *)SQL_ALL_SCHEMAS, SQL_NTS, // SchemaName = "%" (SQL_ALL_SCHEMAS) (SQLCHAR *)"", 0, // TableName = "" (empty string) NULL, 0); // TableType = NULL (not filtered)
注意
ODBC 规范仅将 TABLE_SCHEM 定义为对架构枚举有效。Amazon Redshift 还会填充 TABLE_CAT,因为它支持跨数据库元数据发现,而且每个架构的范围都限于特定的数据库。