

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 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*

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

此模式向您展示了如何在 AWS Glue 上使用将现有 Apache Cassandra 工作负载迁移到亚马逊密钥空间（适用于 Apache Cassandra）。 CQLReplicator 您可以 CQLReplicator 在 AWS Glue 上使用来最大限度地减少将工作负载迁移的复制延迟缩至几分钟。您还将学习如何使用 Amazon Simple Storage Service (Amazon S3)存 储桶来存储迁移所需数据，包括 [Apache Parquet](https://parquet.apache.org/) 文件、配置文件和脚本。此模式假设您的 Cassandra 工作负载托管在虚拟私有云 (VPC EC2) 中的亚马逊弹性计算云 (Amazon) 实例上。

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

**先决条件**
+ 带源表的 Cassandra 集群
+ Amazon Keyspaces 中的目标表，用于复制工作负载
+ 用于存储包含增量数据更改的中间 Parquet 文件的 S3 存储桶
+ 用于存储作业配置文件和脚本的 S3 存储桶

**限制**
+ CQLReplicator 在 AWS Glue 上，需要一些时间来为 Cassandra 工作负载配置数据处理单元 (DPUs)。Cassandra 集群与 Amazon Keyspaces 中的目标键空间和表之间的复制延迟可能只会持续几分钟。

## 架构
<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，AWS Glue 连接到节点 VPC。\]](http://docs.aws.amazon.com/zh_cn/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>

**Amazon Web Services 和工具**
+ [AWS 命令行界面（AWS CLI）](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html)是一种开源工具，它可帮助您通过命令行 Shell 中的命令与 Amazon Web Services 交互。
+ [AWS CloudShell](https://docs.aws.amazon.com/cloudshell/latest/userguide/welcome.html) 是一个基于浏览器的外壳，您可以使用 AWS 命令行界面 (AWS CLI) Line 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)是一项托管数据库服务，可帮助您在 Amazon Web Services Cloud 中迁移、运行和扩展 Cassandra 工作负载。

**代码**

此模式的代码可在 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 表中的行数。例如，使用 84 GB 的磁盘，每 0.25 DPU（2 vCPUs，4 GB 内存）有 250 K 行。
+ 在运行之前预热 Amazon Keyspaces 表。 CQLReplicator例如，八个 CQLReplicator 图块（AWS Glue 作业） WCUs 每秒最多可以写入 22 K，因此应将目标预热到每秒 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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建目标键空间和表。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/migrate-apache-cassandra-workloads-to-amazon-keyspaces-by-using-aws-glue.html) | 应用程序所有者，AWS 管理员，数据库管理员，应用程序开发人员 | 
| 将 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 Connector。有关更多信息，请参阅 [Cassandra](https://docs.datastax.com/en/developer/java-driver/4.17/manual/core/configuration/reference/) 的参考配置。 | 数据库管理员 | 
| 将 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 Connector。有关更多信息，请参阅 [Cassandra](https://docs.datastax.com/en/developer/java-driver/4.17/manual/core/configuration/reference/) 的参考配置。 | 数据库管理员 | 
| 为 AWS Glue 作业创建 IAM 角色。 | 用 AWS Glue 创建名为 `glue-cassandra-migration` 的新 AWS 服务角色作为受信任实体。`glue-cassandra-migration` 应提供对 S3 存储桶和 Amazon Keyspaces 的读写访问权限。S3 存储桶包含.jar 文件、Amazon Keyspaces 与 Cassandra 的配置文件，以及中间的 Parquet 文件。例如，其中包含 `AWSGlueServiceRole`、`AmazonS3FullAccess` 和 `AmazonKeyspacesFullAccess` 托管策略。 | AWS DevOps | 
|  CQLReplicator 在 AWS 中下载 CloudShell。 | 运行以下命令，将项目下载到您的主文件夹：<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> |  | 
| 修改参考配置文件。 | 将 `CassandraConnector.conf` 和 `KeyspacesConnector.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_cn/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_cn/prescriptive-guidance/latest/patterns/migrate-apache-cassandra-workloads-to-amazon-keyspaces-by-using-aws-glue.html) | AWS DevOps | 

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


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 开始迁移过程。 | 要 CQLReplicator 在 AWS Glue 上操作，您需要使用`--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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 在历史迁移阶段验证迁移的 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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 使用 `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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 删除已部署的资源。 | 以下命令将删除 AWS Glue 作业、连接器、S3 存储桶和键空间表 `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_cn/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 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)
+ [阿帕奇·卡桑德拉](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 源数据库完全正常运行。复制完成后，您可选择将应用程序割接到 Amazon Keyspaces，同时将 Cassandra 集群和 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`

 WCUs 每秒 69,444 是 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)
```