

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

# 使用 AWS Glue 將 Apache Cassandra 工作負載遷移至 Amazon Keyspaces
<a name="migrate-apache-cassandra-workloads-to-amazon-keyspaces-by-using-aws-glue"></a>

*Nikolai Kolesnikov、Karthiga Priya Chandran 和 Samir Patel，Amazon Web Services*

## 總結
<a name="migrate-apache-cassandra-workloads-to-amazon-keyspaces-by-using-aws-glue-summary"></a>

此模式說明如何在 AWS Glue 上使用 CQLReplicator，將現有的 Apache Cassandra 工作負載遷移至 Amazon Keyspaces （適用於 Apache Cassandra)。您可以使用 AWS Glue 上的 CQLReplicator，將遷移工作負載的複寫延遲降到最低到幾分鐘。您也會了解如何使用 Amazon Simple Storage Service (Amazon S3) 儲存貯體來存放遷移所需的資料，包括 [Apache Parquet](https://parquet.apache.org/) 檔案、組態檔案和指令碼。此模式假設您的 Cassandra 工作負載託管在虛擬私有雲端 (VPC) 中的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體上。

## 先決條件和限制
<a name="migrate-apache-cassandra-workloads-to-amazon-keyspaces-by-using-aws-glue-prereqs"></a>

**先決條件**
+ 具有來源資料表的 Cassandra 叢集
+ Amazon Keyspaces 中複寫工作負載的目標資料表
+ S3 儲存貯體可存放包含增量資料變更的中繼 Parquet 檔案
+ 存放任務組態檔案和指令碼的 S3 儲存貯體

**限制**
+ AWS Glue 上的 CQLReplicator 需要一些時間來佈建 Cassandra 工作負載的資料處理單位 (DPUs)。 AWS Glue Cassandra 叢集與 Amazon Keyspaces 中的目標金鑰空間和資料表之間的複寫延遲可能只會持續幾分鐘。

## Architecture
<a name="migrate-apache-cassandra-workloads-to-amazon-keyspaces-by-using-aws-glue-architecture"></a>

**來源技術堆疊**
+ Apache Cassandra
+ DataStax 伺服器
+ ScyllaDB

**目標技術堆疊**
+ Amazon Keyspaces

**遷移架構**

下圖顯示範例架構，其中 Cassandra 叢集託管在 EC2 執行個體上，並分散在三個可用區域。Cassandra 節點託管在私有子網路中。

![\[自訂服務角色、Amazon Keyspaces 和 Amazon S3，搭配連接到節點 VPC 的 AWS Glue。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/e08048da-8996-4f2c-b8ed-da49fe9e693b/images/76256ab3-a1e6-4c9e-9c40-dc78f51edf0f.png)


該圖顯示以下工作流程：

1. 自訂服務角色可讓您存取 Amazon Keyspaces 和 S3 儲存貯體。

1. AWS Glue 任務會讀取 S3 儲存貯體中的任務組態和指令碼。

1. AWS Glue 任務會透過連接埠 9042 連接，以從 Cassandra 叢集讀取資料。

1. AWS Glue 任務透過連接埠 9142 連接，將資料寫入 Amazon Keyspaces。

## 工具
<a name="migrate-apache-cassandra-workloads-to-amazon-keyspaces-by-using-aws-glue-tools"></a>

**AWS 服務和工具**
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 是一種開放原始碼工具，可協助您透過命令列 shell 中的命令與 AWS 服務互動。
+ [AWS CloudShell](https://docs.aws.amazon.com/cloudshell/latest/userguide/welcome.html) 是一種瀏覽器型 shell，您可以使用 AWS Command Line Interface (AWS CLI) 和一系列預先安裝的開發工具來管理 AWS 服務。
+ [AWS Glue](https://docs.aws.amazon.com/glue/latest/dg/what-is-glue.html) 是一項全受管 ETL 服務，可協助您可靠地分類、清理、擴充和移動資料存放區和資料串流之間的資料。
+ [Amazon Keyspaces （適用於 Apache Cassandra)](https://docs.aws.amazon.com/keyspaces/latest/devguide/what-is-keyspaces.html) 是一種受管資料庫服務，可協助您在 AWS 雲端中遷移、執行和擴展 Cassandra 工作負載。

**Code**

此模式的程式碼可在 GitHub [CQLReplicator](https://github.com/aws-samples/cql-replicator/tree/main/glue) 儲存庫中使用。

## 最佳實務
<a name="migrate-apache-cassandra-workloads-to-amazon-keyspaces-by-using-aws-glue-best-practices"></a>
+ 若要判斷遷移所需的 AWS Glue 資源，請估計來源 Cassandra 資料表中的資料列數。例如，每 0.25 個 DPU 250 K 資料列 (2 個 vCPUs，4 GB 記憶體） 與 84 GB 磁碟。
+ 在執行 CQLReplicator 之前預熱 Amazon Keyspaces 資料表。例如，八個 CQLReplicator 圖磚 (AWS Glue 任務） 每秒最多可寫入 22 K WCUs，因此目標應預先暖機至每秒最多 25-30 K WCUs。
+ 若要啟用 AWS Glue 元件之間的通訊，請針對安全群組中的所有 TCP 連接埠使用自我參考傳入規則。
+ 使用增量流量策略，隨時間分配遷移工作負載。

## 史詩
<a name="migrate-apache-cassandra-workloads-to-amazon-keyspaces-by-using-aws-glue-epics"></a>

### 部署 CQLReplicator
<a name="deploy-cqlreplicator"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立目標金鑰空間和資料表。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/migrate-apache-cassandra-workloads-to-amazon-keyspaces-by-using-aws-glue.html) | 應用程式擁有者、AWS 管理員、DBA、應用程式開發人員 | 
| 設定 Cassandra 驅動程式以連線至 Cassandra。 | 使用下列組態指令碼：<pre>Datastax-java-driver {<br />  basic.request.consistency = "LOCAL_QUORUM"<br />  basic.contact-points = ["127.0.0.1:9042"]<br />   advanced.reconnect-on-init = true<br />   basic.load-balancing-policy {<br />        local-datacenter = "datacenter1"<br />}<br />advanced.auth-provider = {<br />       class = PlainTextAuthProvider<br />       username = "user-at-sample"<br />       password = "S@MPLE=PASSWORD="<br />}<br />}</pre>上述指令碼使用 Spark Cassandra 連接器。如需詳細資訊，請參閱[ Cassandra](https://docs.datastax.com/en/developer/java-driver/4.17/manual/core/configuration/reference/) 的參考組態。 | DBA | 
| 設定 Cassandra 驅動程式以連線至 Amazon Keyspaces。 | 使用下列組態指令碼：<pre>datastax-java-driver {<br />basic {<br />  load-balancing-policy {<br />    local-datacenter = us-west-2<br />        }<br />  contact-points = [<br />            "cassandra.us-west-2.amazonaws.com:9142"<br />        ]<br />  request {<br />  page-size = 2500<br />  timeout = 360 seconds<br />  consistency = LOCAL_QUORUM<br />        }<br />    }<br />advanced {<br /> control-connection {<br />  timeout = 360 seconds<br />        }<br /> session-leak.threshold = 6<br /> connection {<br /> connect-timeout = 360 seconds<br /> init-query-timeout = 360 seconds<br /> warn-on-init-error = false<br />        }<br /> auth-provider = {<br />  class = software.aws.mcs.auth.SigV4AuthProvider<br />  aws-region = us-west-2<br /> }<br /><br /> ssl-engine-factory {<br />  class = DefaultSslEngineFactory<br />        }<br />    }<br />}</pre>上述指令碼使用 Spark Cassandra 連接器。如需詳細資訊，請參閱[ Cassandra](https://docs.datastax.com/en/developer/java-driver/4.17/manual/core/configuration/reference/) 的參考組態。 | DBA | 
| 為 AWS Glue 任務建立 IAM 角色。 | 建立名為 的新 AWS 服務角色`glue-cassandra-migration`，以 AWS Glue 做為信任的實體。`glue-cassandra-migration` 應該提供對 S3 儲存貯體和 Amazon Keyspaces 的讀取和寫入存取權。S3 儲存貯體包含 .jar 檔案、Amazon Keyspaces 和 Cassandra 的組態檔案，以及中繼 Parquet 檔案。例如，它包含 `AWSGlueServiceRole`、 `AmazonS3FullAccess`和 `AmazonKeyspacesFullAccess`受管政策。 | AWS DevOps | 
| 在 AWS CloudShell 中下載 CQLReplicator。 | 執行下列命令，將專案下載到您的主資料夾：<pre>git clone https://github.com/aws-samples/cql-replicator.git<br />cd cql-replicator/glue<br /># Only for AWS CloudShell, the bc package includes bc and dc. Bc is an arbitrary precision numeric processing arithmetic language<br />sudo yum install bc -y</pre> |  | 
| 修改參考組態檔案。 | 將 `KeyspacesConnector.conf` `CassandraConnector.conf`和 複製到專案資料夾中的 `../glue/conf`目錄。 | AWS DevOps | 
| 啟動遷移程序。 | 下列命令會初始化 CQLReplicator 環境。初始涉及複製 .jar 成品，以及建立 AWS Glue 連接器、S3 儲存貯體、AWS Glue 任務、 `migration` 金鑰空間和 `ledger`資料表：<pre>cd cql-replicator/glue/bin<br />./cqlreplicator --state init --sg '"sg-1","sg-2"' \ <br />                --subnet "subnet-XXXXXXXXXXXX" \ <br />                --az us-west-2a --region us-west-2 \ <br />                --glue-iam-role glue-cassandra-migration \ <br />                --landing-zone s3://cql-replicator-1234567890-us-west-2<br /></pre>該指令碼包括下列參數：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/migrate-apache-cassandra-workloads-to-amazon-keyspaces-by-using-aws-glue.html) | AWS DevOps | 
| 驗證部署。 | 在您執行先前的命令後，AWS 帳戶應包含下列項目：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/migrate-apache-cassandra-workloads-to-amazon-keyspaces-by-using-aws-glue.html) | AWS DevOps | 

### 執行 CQLReplicator
<a name="run-cqlreplicator"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 啟動遷移程序。 | 若要在 AWS Glue 上操作 CQLReplicator，您需要使用 `--state run`命令，後面接著一系列參數。這些參數的精確組態主要取決於您唯一的遷移需求。例如，如果您選擇複寫存留時間 (TTL) 值和更新，或將超過 1 MB 的物件卸載至 Amazon S3，這些設定可能會有所不同。若要將工作負載從 Cassandra 叢集複寫到 Amazon Keyspaces，請執行下列命令：<pre>./cqlreplicator --state run --tiles 8  \<br />                --landing-zone s3://cql-replicator-1234567890-us-west-2 \ <br />                --region us-west-2 \                              <br />                --src-keyspace source_keyspace \ <br />                --src-table source_table \  <br />                --trg-keyspace taget_keyspace \<br />                --writetime-column column_name \<br />                --trg-table target_table --inc-traffic</pre>您的來源金鑰空間和資料表位於 Cassandra 叢集`source_keyspace.source_table`中。您的目標金鑰空間和資料表位於 Amazon Keyspaces `target_keyspace.target_table`中。參數`--inc-traffic`有助於防止增量流量因大量請求而使 Cassandra 叢集和 Amazon Keyspaces 超載。若要複寫更新，請將 `--writetime-column regular_column_name`新增至命令列。一般資料欄將用作寫入時間戳記的來源。 | AWS DevOps | 

### 監控遷移程序
<a name="monitor-the-migration-process"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 在歷史遷移階段驗證已遷移的 Cassandra 資料列。 | 若要取得回填階段期間複寫的資料列數，請執行下列命令：<pre>./cqlreplicator --state stats \<br />                --landing-zone s3://cql-replicator-1234567890-us-west-2 \  <br />                --src-keyspace source_keyspace --src-table source_table --region us-west-2</pre> | AWS DevOps | 

### 停止遷移程序
<a name="stop-the-migration-process"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 使用 `cqlreplicator`命令或 AWS Glue 主控台。 | 若要正常停止遷移程序，請執行下列命令：<pre>./cqlreplicator --state request-stop --tiles 8 \                         <br />                --landing-zone s3://cql-replicator-1234567890-us-west-2 \     <br />                --region us-west-2 \                     <br />                --src-keyspace source_keyspace --src-table source_table</pre>若要立即停止遷移程序，請使用 AWS Glue 主控台。 | AWS DevOps | 

### 清除
<a name="clean-up"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 刪除已部署的資源。 | 下列命令會刪除 AWS Glue 任務、連接器、S3 儲存貯體和 Keyspaces 資料表 `ledger`：<pre>./cqlreplicator --state cleanup --landing-zone s3://cql-replicator-1234567890-us-west-2</pre> | AWS DevOps | 

## 疑難排解
<a name="migrate-apache-cassandra-workloads-to-amazon-keyspaces-by-using-aws-glue-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| AWS Glue 任務失敗，並傳回記憶體不足 (OOM) 錯誤。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/migrate-apache-cassandra-workloads-to-amazon-keyspaces-by-using-aws-glue.html) | 

## 相關資源
<a name="migrate-apache-cassandra-workloads-to-amazon-keyspaces-by-using-aws-glue-resources"></a>
+ [CQLReplicator 搭配 AWS Glue https：//README.MD](https://github.com/aws-samples/cql-replicator/blob/main/glue/README.MD)
+ [AWS Glue 文件](https://docs.aws.amazon.com/glue/latest/dg/what-is-glue.html)
+ [Amazon Keyspaces 文件](https://docs.aws.amazon.com/keyspaces/latest/devguide/what-is-keyspaces.html)
+ [Apache Cassandra](https://cassandra.apache.org/_/index.html)

## 其他資訊
<a name="migrate-apache-cassandra-workloads-to-amazon-keyspaces-by-using-aws-glue-additional"></a>

**遷移考量**

您可以使用 AWS Glue 將 Cassandra 工作負載遷移至 Amazon Keyspaces，同時在遷移過程中保持 Cassandra 來源資料庫完全正常運作。複寫完成後，您可以選擇在 Cassandra 叢集和 Amazon Keyspaces 之間，以最小的複寫延遲 （不到 分鐘） 將應用程式切換到 Amazon Keyspaces。若要維持資料一致性，您也可以使用類似的管道，將資料從 Amazon Keyspaces 複寫回 Cassandra 叢集。

**寫入單位計算**

例如，假設您打算在一小時內寫入 500，000，000 且資料列大小為 1 KiB。您需要的 Amazon Keyspaces 寫入單位 WCUs) 總數是根據此計算：

`(number of rows/60 mins 60s) 1 WCU per row = (500,000,000/(60*60s) * 1 WCU) = 69,444 WCUs required`

每秒 69，444 WCUs 是 1 小時的速率，但您可以為額外負荷新增一些緩衝。 例如， `69,444 * 1.10 = 76,388 WCUs`有 10% 的額外負荷。

**使用 CQL 建立金鑰空間**

若要使用 CQL 建立金鑰空間，請執行下列命令：

```
CREATE KEYSPACE target_keyspace WITH replication = {'class': 'SingleRegionStrategy'}
CREATE TABLE target_keyspace.target_table ( userid uuid, level text, gameid int, description text, nickname text, zip text, email text, updatetime text, PRIMARY KEY (userid, level, gameid) ) WITH default_time_to_live = 0 AND CUSTOM_PROPERTIES = {'capacity_mode':{ 'throughput_mode':'PROVISIONED', 'write_capacity_units':76388, 'read_capacity_units':3612 }} AND CLUSTERING ORDER BY (level ASC, gameid ASC)
```