

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

# 將 Oracle CLOB 值遷移至 AWS 上的 PostgreSQL 中的個別資料列
<a name="migrate-oracle-clob-values-to-individual-rows-in-postgresql-on-aws"></a>

*Sai Krishna Namburu 和 Sindhusha Paturu，Amazon Web Services*

## 總結
<a name="migrate-oracle-clob-values-to-individual-rows-in-postgresql-on-aws-summary"></a>

此模式說明如何將 Oracle 字元大型物件 (CLOB) 值分割為適用於 PostgreSQL 的 Amazon Aurora PostgreSQL 相容版本和 Amazon Relational Database Service (Amazon RDS) PostgreSQL 中的個別資料列。PostgreSQL 不支援 CLOB 資料類型。

具有間隔分割區的資料表會在來源 Oracle 資料庫中識別，而資料表名稱、分割區類型、分割區的間隔，以及其他中繼資料會擷取並載入目標資料庫。您可以使用 AWS Database Migration Service (AWS DMS) 將大小小於 1 GB 的 CLOB 資料以文字形式載入目標資料表，也可以匯出 CSV 格式的資料、將其載入 Amazon Simple Storage Service (Amazon S3) 儲存貯體，然後將其遷移至目標 PostgreSQL 資料庫。

遷移後，您可以使用此模式隨附的自訂 PostgreSQL 程式碼，根據新的行字元識別符 (`CHR(10)`) 將 CLOB 資料分割為個別資料列，並填入目標****資料表。 

## 先決條件和限制
<a name="migrate-oracle-clob-values-to-individual-rows-in-postgresql-on-aws-prereqs"></a>

**先決條件**
+ 具有間隔分割區和 CLOB 資料類型記錄的 Oracle 資料庫資料表。
+ Aurora PostgreSQL 相容或 Amazon RDS for PostgreSQL 資料庫，其資料表結構類似於來源資料表 （相同的資料欄和資料類型）。

**限制**
+ CLOB 值不能超過 1 GB。
+ 目標資料表中的每一列都必須有新的行字元識別符。

**產品版本**
+ Oracle 12c
+ Aurora Postgres 11.6

## Architecture
<a name="migrate-oracle-clob-values-to-individual-rows-in-postgresql-on-aws-architecture"></a>

下圖顯示具有 CLOB 資料的來源 Oracle 資料表，以及 Aurora PostgreSQL 相容 11.6 版中的同等 PostgreSQL 資料表。

![來源 CLOB 資料表和對等目標 PostgreSQL 資料表。](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/55806ee7-6a9f-4058-9a47-a07de68223ca/images/79b9d4b9-6f20-4db5-8ca8-2a599769a498.png)


## 工具
<a name="migrate-oracle-clob-values-to-individual-rows-in-postgresql-on-aws-tools"></a>

**AWS 服務**
+ [Amazon Aurora PostgreSQL 相容版本](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.AuroraPostgreSQL.html)是完全受管且符合 ACID 規範的關聯式資料庫引擎，可協助您設定、操作和擴展 PostgreSQL 部署。
+ [適用於 PostgreSQL 的 Amazon Relational Database Service (Amazon RDS)](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html) 可協助您在 AWS 雲端中設定、操作和擴展 PostgreSQL 關聯式資料庫。
+ [AWS Database Migration Service (AWS DMS)](https://docs.aws.amazon.com/dms/latest/userguide/Welcome.html) 可協助您將資料存放區遷移至 AWS 雲端，或在雲端和內部部署設定的組合之間遷移。
+ [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) 是一種雲端型物件儲存服務，可協助您儲存、保護和擷取任何數量的資料。

**其他工具**

您可以使用下列用戶端工具來連接、存取和管理 Aurora PostgreSQL 相容和 Amazon RDS for PostgreSQL 資料庫。（這些工具不會在此模式中使用。)
+ [pgAdmin](https://www.pgadmin.org/) 是 PostgreSQL 的開放原始碼管理工具。它提供圖形界面，可協助您建立、維護和使用資料庫物件。
+ [DBeaver](https://dbeaver.io/) 是開發人員和資料庫管理員的開放原始碼資料庫工具。您可以使用 工具來操作、監控、分析、管理和遷移資料。

## 最佳實務
<a name="migrate-oracle-clob-values-to-individual-rows-in-postgresql-on-aws-best-practices"></a>

如需將資料庫從 Oracle 遷移至 PostgreSQL 的最佳實務，請參閱 AWS 部落格文章[將 Oracle 資料庫遷移至 Amazon RDS PostgreSQL 或 Amazon Aurora PostgreSQL 的最佳實務：遷移程序和基礎設施考量](https://aws.amazon.com/blogs/database/best-practices-for-migrating-an-oracle-database-to-amazon-rds-postgresql-or-amazon-aurora-postgresql-migration-process-and-infrastructure-considerations/)。

如需設定 AWS DMS 任務以遷移大型二進位物件的最佳實務，請參閱 AWS DMS 文件中的[遷移大型二進位物件 (LOBs)](https://docs.aws.amazon.com/dms/latest/userguide/CHAP_BestPractices.html#CHAP_BestPractices.LOBS)。

## 史詩
<a name="migrate-oracle-clob-values-to-individual-rows-in-postgresql-on-aws-epics"></a>

### 識別 CLOB 資料
<a name="identify-the-clob-data"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 分析 CLOB 資料。 | 在來源 Oracle 資料庫中，分析 CLOB 資料，以查看它是否包含資料欄標頭，讓您可以判斷將資料載入目標資料表的方法。 <br />若要分析輸入資料，請使用下列查詢。<br />`SELECT * FROM clobdata_or;  ` | 開發人員 | 
| 將 CLOB 資料載入目標資料庫。 | 將具有 CLOB 資料的資料表遷移至 Aurora 或 Amazon RDS 目標資料庫中的臨時 （預備） 資料表。您可以使用 AWS DMS 或將資料作為 CSV 檔案上傳至 Amazon S3 儲存貯體。<br />如需針對此任務使用 AWS DMS 的詳細資訊，請參閱 AWS DMS 文件中的[使用 Oracle 資料庫做為來源](https://docs.aws.amazon.com/dms/latest/userguide/CHAP_Source.Oracle.html)和[使用 PostgreSQL 資料庫做為目標](https://docs.aws.amazon.com/dms/latest/userguide/CHAP_Target.PostgreSQL.html)。<br />如需針對此任務使用 Amazon S3 的詳細資訊，請參閱 AWS DMS 文件中的[使用 Amazon S3 做為目標](https://docs.aws.amazon.com/dms/latest/userguide/CHAP_Target.S3.html)。 | 遷移工程師，DBA | 
| 驗證目標 PostgreSQL 資料表。 | 使用目標資料庫中的下列查詢，針對來源資料驗證目標資料，包括標頭。<pre>SELECT * FROM clobdata_pg;<br />SELECT * FROM clobdatatarget;</pre><br />將結果與來源資料庫的查詢結果進行比較 （從第一個步驟）。 | 開發人員 | 
| 將 CLOB 資料分割成不同的資料列。 | 執行[其他資訊](#migrate-oracle-clob-values-to-individual-rows-in-postgresql-on-aws-additional)區段中提供的自訂 PostgreSQL 程式碼，以分割 CLOB 資料，並將其插入目標 PostgreSQL 資料表中的個別資料列。 | 開發人員 | 

### 驗證資料。
<a name="validate-the-data"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 驗證目標資料表中的資料。 | 使用以下查詢驗證插入目標資料表的資料。<pre>SELECT * FROM clobdata_pg;<br />SELECT * FROM clobdatatarget;</pre> | 開發人員 | 

## 相關資源
<a name="migrate-oracle-clob-values-to-individual-rows-in-postgresql-on-aws-resources"></a>
+ [CLOB 資料類型](https://docs.oracle.com/database/121/SQLRF/sql_elements001.htm#SQLRF0021) (Oracle 文件）
+ [資料類型 ](https://www.postgresql.org/docs/11/datatype.html)(PostgreSQL 文件）

## 其他資訊
<a name="migrate-oracle-clob-values-to-individual-rows-in-postgresql-on-aws-additional"></a>

**用於分割 CLOB 資料的 PostgreSQL 函數**

```
do
$$
declare
totalstr varchar;
str1 varchar;
str2 varchar;
pos1 integer := 1;
pos2 integer ;
len integer;


begin
        select rawdata||chr(10) into totalstr from clobdata_pg;
        len :=  length(totalstr) ;
        raise notice 'Total length : %',len;
        raise notice 'totalstr : %',totalstr;
        raise notice 'Before while loop';


        while pos1 < len  loop


                  select position (chr(10) in totalstr) into pos2;
                 raise notice '1st position of new line : %',pos2;


                str1 := substring (totalstr,pos1,pos2-1);
                raise notice 'str1 : %',str1;


                   insert into clobdatatarget(data) values (str1);
                   totalstr := substring(totalstr,pos2+1,len);
                   raise notice 'new totalstr :%',totalstr;
                len :=  length(totalstr) ;


        end loop;
end
$$
LANGUAGE 'plpgsql' ;
```

**輸入和輸出範例**

您可以使用下列範例，在遷移資料之前嘗試 PostgreSQL 程式碼。

建立具有三個輸入列的 Oracle 資料庫。

```
CREATE TABLE clobdata_or (
id INTEGER GENERATED ALWAYS AS IDENTITY,
rawdata clob  );


insert into clobdata_or(rawdata) values (to_clob('test line 1') || chr(10) || to_clob('test line 2') || chr(10) || to_clob('test line 3') || chr(10));
COMMIT;


SELECT * FROM clobdata_or;
```

這會顯示下列輸出。


|  |  | 
| --- |--- |
| id | 原始資料 | 
| 1 | 測試列 1 測試列 2 測試列 3 | 

將來源資料載入 PostgreSQL 預備資料表 (`clobdata_pg`) 進行處理。

```
SELECT * FROM clobdata_pg;

CREATE TEMP TABLE clobdatatarget (id1 SERIAL,data VARCHAR );

<Run the code in the additional information section.>

SELECT * FROM clobdatatarget;
```

這會顯示下列輸出。


|  |  | 
| --- |--- |
| id1 | data | 
| 1 | 測試列 1 | 
| 2 | 測試列 2 | 
| 3 | 測試列 3 | 