

 Amazon Redshift 將不再支援在 2026 年 6 月 30 日之後使用 Python UDFs。我們將開始分階段強制執行。如需 Python 生命週期結束和遷移選項的詳細資訊，請參閱 2025 年 6 月 30 日發佈的[部落格文章](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

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

# 將 Amazon Redshift 驅動程式中繼資料 API 用於應用程式和工具
<a name="discovering-metadata-driver-api"></a>

對於連接到 Amazon Redshift 的應用程式和工具，例如商業智慧工具或查詢編輯器，我們建議您使用 Amazon Redshift [JDBC 2.x](jdbc20-install.md)、[ODBC 2.x](odbc20-install.md) 或 [Python](python-redshift-driver.md) 驅動程式提供的驅動程式中繼資料 API，來探索有關資料倉儲物件的中繼資料，包括資料庫、結構描述、資料表、資料欄和資料類型。或者，您可以使用 Amazon Redshift `SHOW`命令。

使用驅動程式中繼資料 API 享有下列優點：
+ **規格合規**。JDBC 和 ODBC 驅動程式實作標準中繼資料界面 (`DatabaseMetaData`在 JDBC 中，`SQLTables`在 ODBC `SQLColumns`中）。由於 Python 的資料庫 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() 擷取資料表中繼資料
<a name="discovering-metadata-example-jdbc"></a>

```
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() 擷取資料欄中繼資料
<a name="discovering-metadata-example-python"></a>

```
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() 擷取主要金鑰中繼資料
<a name="discovering-metadata-example-odbc"></a>

```
// 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() 列出資料庫和結構描述
<a name="discovering-metadata-example-odbc-sqltables"></a>

ODBC API 不提供個別的函數來列出目錄或結構描述。反之，您可以使用 的特殊呼叫慣例`SQLTables()`來擷取此資訊。

**列出所有資料庫 （目錄）**

呼叫 `SQLTables()`，並將 `CatalogName`設定為 `SQL_ALL_CATALOGS`。將 `SchemaName`和 `TableName` 設定為空白字串。結果集只會傳回資料`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`。將 `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`因為它支援跨資料庫中繼資料探索，而且每個結構描述的範圍都限定在特定資料庫。