View a markdown version of this page

將 Amazon Redshift 驅動程式中繼資料 API 用於應用程式和工具 - Amazon Redshift

Amazon Redshift 將不再支援從修補程式 198 開始建立新的 Python UDFs。現有 Python UDF 將繼續正常運作至 2026 年 6 月 30 日。如需詳細資訊,請參閱部落格文章

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

將 Amazon Redshift 驅動程式中繼資料 API 用於應用程式和工具

對於連接到 Amazon Redshift 的應用程式和工具,例如商業智慧工具或查詢編輯器,我們建議您使用 Amazon Redshift JDBC 2.xODBC 2.xPython 驅動程式提供的驅動程式中繼資料 API,來探索有關資料倉儲物件的中繼資料,包括資料庫、結構描述、資料表、資料欄和資料類型。或者,您可以使用 Amazon Redshift SHOW命令。

使用驅動程式中繼資料 API 享有下列優點:

  • 規格合規。JDBC 和 ODBC 驅動程式實作標準中繼資料界面 (DatabaseMetaData在 JDBC 中,SQLTables在 ODBC SQLColumns中)。由於 Python 的 DB-API (PEP 249) 未定義中繼資料 API 規格,因此 Amazon Redshift Python 驅動程式遵循 JDBC DatabaseMetaData 規格,提供同等方法,例如 get_tables()get_columns()get_schemas()。這些 APIs遵循明確定義的規格,因此您的整合程式碼是可攜式的。隨著 Amazon Redshift 發展其內部系統資料表,您的應用程式不需要變更。

  • 效能最佳化。驅動程式中繼資料 API 經過最佳化,可有效率地傳回中繼資料。 AWS 會繼續投資驅動程式中繼資料 API 效能。

  • 向前相容。Amazon Redshift 遵循 JDBC、ODBC 和 Python 連接器規格。當您針對這些標準 APIs編寫程式碼時,您的應用程式會受到保護,免於基礎系統目錄結構的變更。

範例:使用 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()來擷取此資訊。

列出所有資料庫 (目錄)

呼叫 SQLTables(),並將 CatalogName設定為 SQL_ALL_CATALOGS。將 SchemaNameTableName 設定為空白字串。結果集只會傳回TABLE_CAT欄中的有效值。所有其他資料欄都包含 NULLs。

// 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)

列出所有結構描述

呼叫 SQLTables(),並將 SchemaName設定為 SQL_ALL_SCHEMAS。將 CatalogNameTableName 設定為空白字串。

// 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因為它支援跨資料庫中繼資料探索,而且每個結構描述的範圍都限定在特定資料庫。