

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

# 將資料新增至來源 RDS 資料庫並進行查詢
<a name="zero-etl.querying"></a>

若要完成建立 零 ETL 整合，將資料從 Amazon RDS 複製到 Amazon Redshift，您必須在目標目的地中建立資料庫。

對於與 Amazon Redshift 的連線，連線到您的 Amazon Redshift 叢集或工作群組，並建立參考整合識別符的資料庫。然後，您可以將資料新增到來源 RDS 資料庫，並看到該資料在 Amazon Redshift 或 Amazon SageMaker 中複寫。

**Topics**
+ [建立目標資料庫](#zero-etl.create-db)
+ [將資料新增至來源資料庫](#zero-etl.add-data-rds)
+ [在 Amazon Redshift 中查詢您的 Amazon RDS 資料。](#zero-etl.query-data-redshift)
+ [RDS 與 Amazon Redshift 資料庫之間的資料類型差異](#zero-etl.data-type-mapping)
+ [RDS for PostgreSQL 的 DDL 操作](#zero-etl.ddl-postgres)

## 建立目標資料庫
<a name="zero-etl.create-db"></a>

在建立整合之後，您必須在目標資料倉儲中建立資料庫，然後才能開始將資料複寫到 Amazon Redshift。此資料庫必須包含整合識別符的參考。您可以使用 Amazon Redshift 主控台或查詢編輯器第 2 版來建立資料庫。

如需建立目的地資料庫的指示，請參閱[在 Amazon Redshift 中建立目的地資料庫](https://docs.aws.amazon.com/redshift/latest/mgmt/zero-etl-using.creating-db.html#zero-etl-using.create-db)。

## 將資料新增至來源資料庫
<a name="zero-etl.add-data-rds"></a>

設定整合之後，您可以將要複寫至資料倉儲的資料填入來源 RDS 資料庫。

**注意**  
Amazon RDS 與目標分析倉儲中的資料類型之間存在差異。如需資料類型映射的資料表，請參閱 [RDS 與 Amazon Redshift 資料庫之間的資料類型差異](#zero-etl.data-type-mapping)。

首先，使用您選擇的 MySQL 用戶端連線到來源資料庫。如需說明，請參閱[連線至您的 MySQL 資料庫執行個體](USER_ConnectToInstance.md)。

然後，建立資料表並插入一列範例資料。

**重要**  
請確定資料表具有主索引鍵。否則，無法將其複寫到目標資料倉儲。

**RDS for MySQL**

下列範例使用 [MySQL Workbench 公用程式](https://dev.mysql.com/downloads/workbench/)。

```
CREATE DATABASE my_db;

USE my_db;

CREATE TABLE books_table (ID int NOT NULL, Title VARCHAR(50) NOT NULL, Author VARCHAR(50) NOT NULL,
Copyright INT NOT NULL, Genre VARCHAR(50) NOT NULL, PRIMARY KEY (ID));

INSERT INTO books_table VALUES (1, 'The Shining', 'Stephen King', 1977, 'Supernatural fiction');
```



**RDS for PostgreSQL**

下列範例使用 `[psql](https://www.postgresql.org/docs/current/app-psql.html)` PostgreSQL 互動式終端機。連線至資料庫時，請包含您要複寫的資料庫名稱。

```
psql -h mydatabase.123456789012.us-east-2.rds.amazonaws.com -p 5432 -U username -d named_db;

named_db=> CREATE TABLE books_table (ID int NOT NULL, Title VARCHAR(50) NOT NULL, Author VARCHAR(50) NOT NULL,
Copyright INT NOT NULL, Genre VARCHAR(50) NOT NULL, PRIMARY KEY (ID));

named_db=> INSERT INTO books_table VALUES (1, 'The Shining', 'Stephen King', 1977, 'Supernatural fiction');
```

**RDS for Oracle**

下列範例使用 SQL\$1Plus 連線至 RDS for Oracle 資料庫。

```
sqlplus 'user_name@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=dns_name)(PORT=port))(CONNECT_DATA=(SID=database_name)))'

SQL> CREATE TABLE books_table (ID int NOT NULL, Title VARCHAR(50) NOT NULL, Author VARCHAR(50) NOT NULL,
Copyright INT NOT NULL, Genre VARCHAR(50) NOT NULL, PRIMARY KEY (ID));

SQL> INSERT INTO books_table VALUES (1, 'The Shining', 'Stephen King', 1977, 'Supernatural fiction');
```

## 在 Amazon Redshift 中查詢您的 Amazon RDS 資料。
<a name="zero-etl.query-data-redshift"></a>

將資料新增至 RDS 資料庫後，資料會複寫至目的地資料庫，並準備好進行查詢。

**查詢複製的資料**

1. 導覽至 Amazon Redshift 主控台，然後從左側導覽窗格中選擇**查詢編輯器第 2 版**。

1. 連線到您的叢集或工作群組，然後從下拉式功能表中選擇您已從整合中建立的目的地資料庫 (在此範例中為 **destination\$1database**)。如需建立目的地資料庫的指示，請參閱[在 Amazon Redshift 中建立目的地資料庫](https://docs.aws.amazon.com/redshift/latest/mgmt/zero-etl-using.creating-db.html#zero-etl-using.create-db)。

1. 使用 SELECT 陳述式查詢您的資料。在此範例中，您可以執行下列命令，從您在來源 RDS 資料庫中建立的資料表中選取所有資料：

   ```
   SELECT * from my_db."books_table";
   ```  
![\[在查詢編輯器中執行 SELECT 陳述式。結果是新增至 Amazon RDS 資料庫的單一範例資料列。\]](http://docs.aws.amazon.com/zh_tw/AmazonRDS/latest/UserGuide/images/zero-etl-redshift-editor.png)
   + `my_db` 是 RDS 資料庫結構描述名稱。
   + `books_table` 是 RDS 資料表名稱。

您也可以使用命令列用戶端查詢資料。例如：

```
destination_database=# select * from my_db."books_table";

 ID |       Title |        Author |   Copyright |                  Genre |  txn_seq |  txn_id
----+–------------+---------------+-------------+------------------------+----------+--------+
  1 | The Shining |  Stephen King |        1977 |   Supernatural fiction |        2 |   12192
```

**注意**  
如需區分大小寫，請針對結構描述、資料表和資料欄名稱使用雙引號 (" ")。如需詳細資訊，請參閱 [enable\$1case\$1sensitive\$1identifier](https://docs.aws.amazon.com/redshift/latest/dg/r_enable_case_sensitive_identifier.html)。

## RDS 與 Amazon Redshift 資料庫之間的資料類型差異
<a name="zero-etl.data-type-mapping"></a>

下表顯示 RDS for MySQL、RDS for PostgreSQL 和 RDS for Oracle 的映射資料類型與對應目的地資料類型。*Amazon RDS 目前僅支援這些資料類型進行零 ETL 整合。*

如果來源資料庫中的資料表包含不受支援的資料類型，則資料表不同步，而且目的地目標無法取用該資料表。從來源到目標的串流會繼續進行，但是無法使用其中資料類型不受支援的資料表。若要修正資料表並使其可在目標目的地中使用，您必須手動還原重大變更，然後執行 `[ALTER DATABASE...INTEGRATION REFRESH](https://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_DATABASE.html)` 以重新整理整合。

**注意**  
您無法重新整理與 Amazon SageMaker 資料湖倉的零 ETL 整合。相反地，請刪除並嘗試再次建立整合。

**Topics**
+ [RDS for MySQL](#zero-etl.data-type-mapping-mysql)
+ [RDS for PostgreSQL](#zero-etl.data-type-mapping-postgres)
+ [RDS for Oracle](#zero-etl.data-type-mapping-oracle)

### RDS for MySQL
<a name="zero-etl.data-type-mapping-mysql"></a>

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/AmazonRDS/latest/UserGuide/zero-etl.querying.html)

### RDS for PostgreSQL
<a name="zero-etl.data-type-mapping-postgres"></a>

RDS for PostgreSQL 的零 ETL 整合不支援自訂資料類型或擴充功能建立的資料類型。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/AmazonRDS/latest/UserGuide/zero-etl.querying.html)

### RDS for Oracle
<a name="zero-etl.data-type-mapping-oracle"></a>

**不受支援的資料類型**

Amazon Redshift 不支援下列 RDS for Oracle 資料類型：
+ `ANYDATA`
+ `BFILE`
+ `REF`
+ `ROWID`
+ `UROWID`
+ `VARRAY`
+ `SDO_GEOMETRY`
+ 使用者定義的資料類型

**資料類型差異**

下表顯示當 RDS for Oracle 是來源且 Amazon Redshift 是目標時，影響零 ETL 整合的資料類型差異。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/AmazonRDS/latest/UserGuide/zero-etl.querying.html)

## RDS for PostgreSQL 的 DDL 操作
<a name="zero-etl.ddl-postgres"></a>

Amazon Redshift 衍生自 PostgreSQL，因此由於其常見的 PostgreSQL 架構，它與 RDS for PostgreSQL 共用多項功能。零 ETL 整合利用這些相似性，簡化從 RDS for PostgreSQL 到 Amazon Redshift 的資料複寫，依名稱映射資料庫，並使用共用資料庫、結構描述和資料表結構。

管理 RDS for PostgreSQL 零 ETL 整合時，請考慮下列要點：
+ 隔離是在資料庫層級管理。
+ 複寫會在資料庫層級發生。
+ RDS for PostgreSQL 資料庫會依名稱映射至 Amazon Redshift 資料庫，如果原始資料庫重新命名，資料會流向對應的重新命名 Redshift 資料庫。

雖然兩者相似，但 Amazon Redshift 和 RDS for PostgreSQL 仍有重要的差異。下列各節概述常見 DDL 操作的 Amazon Redshift 系統回應。

**Topics**
+ [資料庫操作](#zero-etl.ddl-postgres-database)
+ [結構描述操作](#zero-etl.ddl-postgres-schema)
+ [資料表操作](#zero-etl.ddl-postgres-table)

### 資料庫操作
<a name="zero-etl.ddl-postgres-database"></a>

下表顯示資料庫 DDL 操作的系統回應。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/AmazonRDS/latest/UserGuide/zero-etl.querying.html)

### 結構描述操作
<a name="zero-etl.ddl-postgres-schema"></a>

下表顯示結構描述 DDL 操作的系統回應。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/AmazonRDS/latest/UserGuide/zero-etl.querying.html)

### 資料表操作
<a name="zero-etl.ddl-postgres-table"></a>

下表顯示資料表 DDL 操作的系統回應。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/AmazonRDS/latest/UserGuide/zero-etl.querying.html)