

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

# 使用 Python 在 AWS 上将 EBCDIC 数据转换并解压为 ASCII
<a name="convert-and-unpack-ebcdic-data-to-ascii-on-aws-by-using-python"></a>

*Luis Gustavo Dantas，Amazon Web Services*

## Summary
<a name="convert-and-unpack-ebcdic-data-to-ascii-on-aws-by-using-python-summary"></a>

由于大型机通常托管关键业务数据，因此在将数据迁移置 Amazon Web Services (AWS) 云或其他美国信息交换标准规范 (ASCII) 环境时，实现数据现代化很重要。在大型机上，数据通常以扩展二进制编码十进制交换码 (EBCDIC) 格式编码。导出数据库、Virtual Storage Access Method (VSAM) 或平面文件通常会生成打包的二进制 EBCDIC 文件，这些文件迁移比较复杂。最常用的数据库迁移解决方案是变更数据捕获 (CDC)，在大多数情况下，其会自动转换数据编码。但是，CDC 机制可能不适用于此数据库、VSAM 或平面文件。对于此文件，需要另一种方法来实现数据现代化。

此模式介绍了如何通过将 EBCDIC 数据转换为 ASCII 格式来实现其现代化。转换后，您可将数据加载至分布式数据库中，也可以让云中的应用程序直接处理数据。该模式使用[mainframe-data-utilities](https://github.com/aws-samples/mainframe-data-utilities) GitHub 存储库中的转换脚本和示例文件。

## 先决条件和限制
<a name="convert-and-unpack-ebcdic-data-to-ascii-on-aws-by-using-python-prereqs"></a>

**先决条件**
+ 一个有效的 Amazon Web Services account。
+ EBCDIC 输入文件及其相应的通用面向业务的语言 (COBOL) 副本。存储库中包含一个 EBCDIC 文件和 COBOL 字帖样本。[mainframe-data-utilities](https://github.com/aws-samples/mainframe-data-utilities) GitHub 有关 COBOL 字帖的更多信息，请参阅 IBM 网站上的 [ z/OS 6.4 版 Enterprise COBOL 编程指南](https://publibfp.dhe.ibm.com/epubs/pdf/igy6pg40.pdf)。

**限制**
+ 不支持 COBOL 程序定义的文件布局。它们必须单独提供。

**产品版本**
+ Python 版本 3.8 或更高版本。

## 架构
<a name="convert-and-unpack-ebcdic-data-to-ascii-on-aws-by-using-python-architecture"></a>

**源技术堆栈**
+ 大型机的 EBCDIC 数据
+ COBOL 副本

**目标技术堆栈**
+ 虚拟私有云 (VPC EC2) 中的亚马逊弹性计算云 (Amazon) 实例
+ Amazon Elastic Block Store（Amazon EBS）
+ Python 及其必需的软件包、 JavaScript 对象表示法 (JSON)、系统和日期时间
+ ASCII 平面文件可供现代应用程序读取或加载至关系数据库表中

**目标架构**

![\[使用 Python 脚本和 COBOL 副本在 EC2 实例上将 EBCDIC 数据转换为 ASCII\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/f5907bfe-7dff-4cd0-8523-57015ad48c4b/images/4f97b1dd-3f20-4966-a291-22180680ea99.png)


架构图显示了在实例上将 EBCDIC 文件转换为 ASCII 文件的过程： EC2 

1. 使用 **parse\$1copybook\$1to\$1json.py** 脚本，您可将 COBOL 副本转换为 JSON 文件。

1. 使用 JSON 文件和 **extract\$1ebcdic\$1to\$1ascii.py** 脚本，您可将 EBCDIC 数据转换为 ASCII 文件。

**自动化和扩缩**

在首次手动文件转换所需资源到位后，您可自动进行文件转换。此示例不包括自动化说明。有多种方法可自动转换。下文概述了一种可能使用的方法：

1. 将 AWS 命令行界面（AWS CLI）和 Python 脚本命令封装至 shell 脚本中。

1. 创建一个 AWS Lambda 函数，该函数将外壳脚本任务异步提交到实例。 EC2 有关更多信息，请参阅[使用 AWS Lambda 安排 SSH 作业](https://aws.amazon.com/blogs/compute/scheduling-ssh-jobs-using-aws-lambda/)。

1. 创建 Amazon Simple Storage Service (Amazon S3) 触发器，该触发器在每次上传旧文件时调用 Lambda 函数。有关更多信息，请参见[使用 Amazon S3 触发器调用 Lambda函数](https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html)。

## 工具
<a name="convert-and-unpack-ebcdic-data-to-ascii-on-aws-by-using-python-tools"></a>

**AWS 服务**
+ [亚马逊弹性计算云 (Amazon EC2)](https://docs.aws.amazon.com/ec2/?id=docs_gateway) 在 AWS 云中提供可扩展的计算容量。您可以根据需要启动任意数量的虚拟服务器，并快速扩展或缩减它们。
+ [亚马逊弹性区块存储 (Amazon EBS) E](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AmazonEBS.html) lastic Block Store 提供块级存储卷，用于亚马逊弹性计算云 (Amazon) 实例。 EC2
+ [AWS 命令行界面（AWS CLI）](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html)是一种开源工具，它可帮助您通过命令行 Shell 中的命令与 Amazon Web Services 交互。
+ [AWS Identity and Access Management (AWS IAM) ](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html)通过控制验证和授权使用您 AWS 资源的用户，帮助您安全地管理对您 AWS 资源的访问。

**其他工具**
+ [GitHub](https://github.com/)是一项代码托管服务，提供协作工具和版本控制。
+ [Python](https://www.python.org/) 是高级编程语言。

**代码存储库**

此模式的代码可在[mainframe-data-utilities](https://github.com/aws-samples/mainframe-data-utilities) GitHub 存储库中找到。

## 操作说明
<a name="convert-and-unpack-ebcdic-data-to-ascii-on-aws-by-using-python-epics"></a>

### 准备实 EC2 例
<a name="prepare-the-ec2-instance"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 启动实 EC2 例。 | 该 EC2 实例必须具有出站互联网访问权限。这允许实例访问上可用的 Python 源代码 GitHub。创建实例：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/convert-and-unpack-ebcdic-data-to-ascii-on-aws-by-using-python.html) | 常规 AWS | 
| 安装 Git。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/convert-and-unpack-ebcdic-data-to-ascii-on-aws-by-using-python.html) | 常规 AWS、Linux | 
| 安装 Python。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/convert-and-unpack-ebcdic-data-to-ascii-on-aws-by-using-python.html) | 常规 AWS、Linux | 
| 克隆 GitHub 存储库。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/convert-and-unpack-ebcdic-data-to-ascii-on-aws-by-using-python.html) | 通用 AWS， GitHub | 

### 通过 EBCDIC 数据创建 ASCII 文件
<a name="create-the-ascii-file-from-the-ebcdic-data"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 将 COBOL 副本解析至 JSON 布局文件。 | 在 `mainframe-data-utilities` 文件夹中，运行 **parse\$1copybook\$1to\$1json.py** 脚本。此自动化模块从 COBOL 副本读取文件布局并创建 JSON 文件。JSON 文件包含解释和提取源文件中数据所需信息。这将通过 COBOL 副本创建 JSON 元数据。以下命令将 COBOL 副本转换为 JSON 文件。<pre>python3 parse_copybook_to_json.py \<br />-copybook LegacyReference/COBPACK2.cpy \<br />-output sample-data/cobpack2-list.json \<br />-dict sample-data/cobpack2-dict.json \<br />-ebcdic sample-data/COBPACK.OUTFILE.txt \<br />-ascii sample-data/COBPACK.ASCII.txt \<br />-print 10000</pre>该脚本打印所接收到的参数。<pre>-----------------------------------------------------------------------<br />Copybook file...............| LegacyReference/COBPACK2.cpy<br />Parsed copybook (JSON List).| sample-data/cobpack2-list.json<br />JSON Dict (documentation)...| sample-data/cobpack2-dict.json<br />ASCII file..................| sample-data/COBPACK.ASCII.txt<br />EBCDIC file.................| sample-data/COBPACK.OUTFILE.txt<br />Print each..................| 10000<br />-----------------------------------------------------------------------</pre>有关参数的更多信息，请参阅 GitHub 存储库中的 [README 文件](https://github.com/aws-samples/mainframe-data-utilities/blob/main/README.md)。 | 常规 AWS、Linux | 
| 检查 JSON 布局文件。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/convert-and-unpack-ebcdic-data-to-ascii-on-aws-by-using-python.html)<pre> "input": "extract-ebcdic-to-ascii/COBPACK.OUTFILE.txt",<br /> "output": "extract-ebcdic-to-ascii/COBPACK.ASCII.txt",<br /> "max": 0,<br /> "skip": 0,<br /> "print": 10000,<br /> "lrecl": 150,<br /> "rem-low-values": true,<br /> "separator": "|",<br /> "transf": [<br /> {<br /> "type": "ch",<br /> "bytes": 19,<br /> "name": "OUTFILE-TEXT"<br /> } </pre>JSON 布局文件最重要属性是：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/convert-and-unpack-ebcdic-data-to-ascii-on-aws-by-using-python.html)有关 JSON 布局文件的更多信息，请参阅 GitHub 存储库中的[自述文件](https://github.com/aws-samples/mainframe-data-utilities/blob/main/README.md)。 | 常规 AWS、JSON | 
| 创建 ASCII 文件。 | 运行 **extract\$1ebcdic\$1to\$1ascii.py** 脚本，该脚本包含在克隆的 GitHub 存储库中。此脚本读取 EBCDIC 文件，并写入转换后且可读的 ASCII 文件。<pre>python3 extract_ebcdic_to_ascii.py -local-json sample-data/cobpack2-list.json</pre>当脚本处理 EBCDIC 数据，它会为每批 10,000 条记录打印一条消息。请参阅以下示例。<pre>------------------------------------------------------------------<br />2023-05-15 21:21:46.322253 | Local Json file   | -local-json | sample-data/cobpack2-list.json<br />2023-05-15 21:21:47.034556 | Records processed | 10000<br />2023-05-15 21:21:47.736434 | Records processed | 20000<br />2023-05-15 21:21:48.441696 | Records processed | 30000<br />2023-05-15 21:21:49.173781 | Records processed | 40000<br />2023-05-15 21:21:49.874779 | Records processed | 50000<br />2023-05-15 21:21:50.705873 | Records processed | 60000<br />2023-05-15 21:21:51.609335 | Records processed | 70000<br />2023-05-15 21:21:52.292989 | Records processed | 80000<br />2023-05-15 21:21:52.938366 | Records processed | 89280<br />2023-05-15 21:21:52.938448 Seconds 6.616232</pre>有关如何更改打印频率的信息，请参阅 GitHub 存储库中的 [README 文件](https://github.com/aws-samples/mainframe-data-utilities/blob/main/README.md)。 | 常规 AWS | 
| 检查 ASCII 文件。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/convert-and-unpack-ebcdic-data-to-ascii-on-aws-by-using-python.html)如果您使用了提供的示例 EBCDIC 文件，以下是 ASCII 文件中的第一条记录。<pre>00000000: 2d30 3030 3030 3030 3030 3130 3030 3030  -000000000100000<br />00000010: 3030 307c 3030 3030 3030 3030 3031 3030  000|000000000100<br />00000020: 3030 3030 3030 7c2d 3030 3030 3030 3030  000000|-00000000<br />00000030: 3031 3030 3030 3030 3030 7c30 7c30 7c31  0100000000|0|0|1<br />00000040: 3030 3030 3030 3030 7c2d 3130 3030 3030  00000000|-100000<br />00000050: 3030 307c 3130 3030 3030 3030 307c 2d31  000|100000000|-1<br />00000060: 3030 3030 3030 3030 7c30 3030 3030 7c30  00000000|00000|0<br />00000070: 3030 3030 7c31 3030 3030 3030 3030 7c2d  0000|100000000|-<br />00000080: 3130 3030 3030 3030 307c 3030 3030 3030  100000000|000000<br />00000090: 3030 3030 3130 3030 3030 3030 307c 2d30  0000100000000|-0<br />000000a0: 3030 3030 3030 3030 3031 3030 3030 3030  0000000001000000<br />000000b0: 3030 7c41 7c41 7c0a                      00|A|A|.</pre> | 常规 AWS、Linux | 
| 评估 EBCDIC 文件。 | 在 Amazon EC2 控制台中，输入以下命令。这将会打开 EBCDIC 文件的第一条记录。<pre>head sample-data/COBPACK.OUTFILE.txt -c 150 | xxd</pre>如果您使用 EBCDIC 示例文件，则结果如下所示。<pre> 00000000: 60f0 f0f0 f0f0 f0f0 f0f0 f1f0 f0f0 f0f0 `...............<br /> 00000010: f0f0 f0f0 f0f0 f0f0 f0f0 f0f0 f1f0 f0f0 ................<br /> 00000020: f0f0 f0f0 f0f0 f0f0 f0f0 f0f0 f0f0 f1f0 ................<br /> 00000030: f0f0 f0f0 f0f0 d000 0000 0005 f5e1 00fa ................<br /> 00000040: 0a1f 0000 0000 0005 f5e1 00ff ffff fffa ................<br /> 00000050: 0a1f 0000 000f 0000 0c10 0000 000f 1000 ................<br /> 00000060: 0000 0d00 0000 0000 1000 0000 0f00 0000 ................<br /> 00000070: 0000 1000 0000 0dc1 c100 0000 0000 0000 ................<br /> 00000080: 0000 0000 0000 0000 0000 0000 0000 0000 ................<br /> 00000090: 0000 0000 0000 ......</pre>要评估源文件和目标文件之间的等效性，则需要全面了解 EBCDIC。例如，示例 EBCDIC 文件的第一个字符是连字符 (`-`)。在 EBCDIC 文件的十六进制表示法中，此字符由 `60` 表示，在 ASCII 文件的十六进制表示法中，该字符由 `2D` 表示。有关 EBCDIC-to-ASCII转换表，请参阅 IBM 网站上的 [EBCDIC 到 ASCII](https://www.ibm.com/docs/en/iis/11.3?topic=tables-ebcdic-ascii)。 | 常规 AWS、Linux、EBCDIC | 

## 相关资源
<a name="convert-and-unpack-ebcdic-data-to-ascii-on-aws-by-using-python-resources"></a>

**参考**
+ [EBCDIC 字符集](https://www.ibm.com/docs/en/zos-basic-skills?topic=mainframe-ebcdic-character-set)（IBM 文档）
+ [EBCDIC 到 ASCII](https://www.ibm.com/docs/en/iis/11.3?topic=tables-ebcdic-ascii)（IBM 文档）
+ [COBOL](https://www.ibm.com/docs/en/i/7.1?topic=languages-cobol)（IBM 文档）
+ [JCL 的基本概念](https://www.ibm.com/docs/en/zos-basic-skills?topic=collection-basic-jcl-concepts)（IBM 文档）
+ [连接到你的 Linux 实例](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstances.html)（亚马逊 EC2 文档）

**教程**
+ [使用 AWS Lambda 安排 SSH 多页](https://aws.amazon.com/blogs/compute/scheduling-ssh-jobs-using-aws-lambda/)（AWS Blog 文章）
+ [使用 Amazon S3 触发器调用 Lambda 函数](https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html)（AWS Lambda 文档）