

 Amazon Redshift 將不再支援從修補程式 198 開始建立新的 Python UDFs。現有 Python UDF 將繼續正常運作至 2026 年 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 中參考 Iceberg 資料表
<a name="referencing-iceberg-tables"></a>

Amazon Redshift 提供多種方法來參考存放在資料湖中的 Apache Iceberg 資料表。您可以使用外部結構描述來建立包含 Iceberg 資料表的資料目錄資料庫參考，或使用三段式表示法直接存取自動掛載的目錄。

## 使用外部結構描述參考 Iceberg 資料表
<a name="referencing-iceberg-external-schemas"></a>

外部結構描述可讓您從 Amazon Redshift 內參考 Data Catalog 中的資料表。當您建立外部結構描述時，您可以在 Amazon Redshift 資料庫與包含 Iceberg 資料表的特定 Data Catalog 資料庫之間建立連線。

若要建立 Iceberg 資料表的外部結構描述：

```
CREATE EXTERNAL SCHEMA schema_name
FROM DATA CATALOG
DATABASE 'glue_database_name'
IAM_ROLE 'arn:aws:iam::account-id:role/role-name';
```

建立外部結構描述後，您可以使用兩段式表示法查詢 Iceberg 資料表：

```
SELECT * FROM schema_name.iceberg_table_name;
```

您也可以將 Iceberg 資料表與本機 Amazon Redshift 資料表聯結：

```
SELECT r.customer_id, i.order_date, r.customer_name
FROM local_customers r
JOIN schema_name.iceberg_orders i 
ON r.customer_id = i.customer_id;
```

## 搭配自動掛載目錄使用三部分標記法
<a name="referencing-iceberg-three-part-notation"></a>

三部分表示法可讓您直接參考自動掛載目錄中的資料表，而無需建立外部結構描述。此方法在使用與 聯合的 Amazon S3 資料表儲存貯體時特別有用 AWS Lake Formation。如需有關設定自動掛載 Data Catalog 的資訊，請參閱[使用自動掛載簡化 Amazon Redshift 中的外部物件存取 AWS Glue Data Catalog](https://aws.amazon.com/blogs/big-data/simplify-external-object-access-in-amazon-redshift-using-automatic-mounting-of-the-aws-glue-data-catalog/)。

三部分表示法的語法為：

```
"catalog_name".database_name.table_name
```

例如，若要在自動掛載的 Amazon S3 資料表目錄中查詢 Iceberg 資料表：

```
SELECT * FROM "my_table_bucket@s3tablescatalog".my_database.my_iceberg_table;
```

如需將 Amazon S3 資料表儲存貯體與 Amazon Redshift 整合的詳細資訊，請參閱《[Amazon S3 使用者指南》中的將 S3 資料表與 Amazon Redshift 整合](https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-tables-integrating-redshift.html)。 *Amazon S3 *

您也可以使用 `USE`陳述式來設定預設目錄和資料庫：

```
USE "my_table_bucket@s3tablescatalog".my_database;
SELECT * FROM my_iceberg_table;
```

若要設定結構描述解析的搜尋路徑：

```
USE "my_table_bucket@s3tablescatalog";
SET search_path TO my_database;
SELECT * FROM my_iceberg_table;
```

## 參考 Iceberg 資料表的最佳實務
<a name="referencing-iceberg-best-practices"></a>

在 Amazon Redshift 中參考 Iceberg 資料表時，請考慮下列最佳實務：
+ **使用描述性結構描述名稱** – 建立外部結構描述時，請使用可清楚指出資料來源和用途的名稱，例如 `sales_data_lake`或 `customer_analytics`。
+ **利用資料表統計資料** – 確保使用 為您的 Iceberg 資料表產生資料欄統計資料 AWS Glue ，以最佳化查詢效能。Amazon Redshift 使用這些統計資料進行查詢規劃和最佳化。
+ **考量資料新鮮度** – 在您查詢時，其他服務可能會更新 Iceberg 資料表。Amazon Redshift 提供交易一致性，確保您在查詢執行期間看到一致的資料快照。
+ **使用適當的 IAM 許可** – 確保您的 Amazon Redshift 叢集或工作群組具有必要的 IAM 許可，可存取存放 Iceberg 資料表的 Amazon S3 位置，以及資料目錄中繼資料。
+ **監控查詢效能** – 使用 Amazon Redshift 查詢監控功能來追蹤針對 Iceberg 資料表的查詢效能，並視需要最佳化。

## 常見參考模式
<a name="referencing-iceberg-examples"></a>

下列範例示範參考 Iceberg 資料表的常見模式：

**跨多個 Iceberg 資料表彙總資料：**

```
SELECT 
    region,
    SUM(sales_amount) as total_sales,
    COUNT(*) as transaction_count
FROM data_lake.sales_transactions
WHERE transaction_date >= '2024-01-01'
GROUP BY region
ORDER BY total_sales DESC;
```

**將 Iceberg 資料表與本機 Amazon Redshift 資料表聯結：**

```
SELECT 
    c.customer_name,
    c.customer_tier,
    SUM(o.order_amount) as total_orders
FROM customers c
JOIN data_lake.order_history o ON c.customer_id = o.customer_id
WHERE o.order_date >= CURRENT_DATE - INTERVAL '30 days'
GROUP BY c.customer_name, c.customer_tier;
```

**搭配複雜查詢使用三部分表示法：**

```
WITH recent_orders AS (
    SELECT customer_id, order_date, order_amount
    FROM "analytics_bucket@s3tablescatalog".ecommerce.orders
    WHERE order_date >= CURRENT_DATE - INTERVAL '7 days'
)
SELECT 
    customer_id,
    COUNT(*) as order_count,
    AVG(order_amount) as avg_order_value
FROM recent_orders
GROUP BY customer_id
HAVING COUNT(*) > 1;
```