

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

# 使用 Python 將 EBCDIC 資料轉換為 AWS 上的 ASCII
<a name="convert-and-unpack-ebcdic-data-to-ascii-on-aws-by-using-python"></a>

*Luis Gustavo Dantas，Amazon Web Services*

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

由於大型主機通常會託管關鍵業務資料，因此將資料遷移至 Amazon Web Services (AWS) 雲端或其他美國資訊交換標準碼 (ASCII) 環境時，現代化資料是最重要的任務之一。在大型主機上，資料通常會以延伸二進位編碼的小數交換碼 (EBCDIC) 格式進行編碼。匯出資料庫、虛擬儲存存取方法 (VSAM) 或一般檔案通常會產生封裝的二進位 EBCDIC 檔案，這些檔案更複雜的遷移。最常用的資料庫遷移解決方案是變更資料擷取 (CDC)，在大多數情況下會自動轉換資料編碼。不過，CDC 機制可能無法用於這些資料庫、VSAM 或一般檔案。對於這些檔案，需要替代方法來現代化資料。

此模式說明如何透過將其轉換為 ASCII 格式來現代化 EBCDIC 資料。轉換後，您可以將資料載入分散式資料庫，或讓雲端中的應用程式直接處理資料。模式會使用[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>

**先決條件**
+ 作用中的 AWS 帳戶
+ EBCDIC 輸入檔案及其對應的常見商業導向語言 (COBOL) 複製本。範例 EBCDIC 檔案和 COBOL 複製手冊包含在[mainframe-data-utilities](https://github.com/aws-samples/mainframe-data-utilities) GitHub 儲存庫中。如需 COBOL 複本手冊的詳細資訊，請參閱 IBM 網站上的[適用於 z/OS 6.4 的企業 COBOL 程式設計指南](https://publibfp.dhe.ibm.com/epubs/pdf/igy6pg40.pdf)。

**限制**
+ 不支援 COBOL 程式中定義的檔案配置。它們必須單獨提供。

**產品版本**
+ Python 3.8 版或更新版本

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

**來源技術堆疊**
+ 大型主機上的 EBCDIC 資料
+ COBOL 複製手冊

**目標技術堆疊**
+ 虛擬私有雲端 (VPC) 中的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體
+ Amazon Elastic Block Store (Amazon EBS)
+ Python 及其所需的套件、JavaScript 物件標記法 (JSON)、sys 和日期時間
+ ASCII 平面檔案已準備好供現代應用程式讀取或載入關聯式資料庫資料表

**目標架構**

![使用 Python 指令碼和 COBOL 複製手冊，在 EC2 執行個體上將 EBCDIC 資料轉換為 ASCII](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/f5907bfe-7dff-4cd0-8523-57015ad48c4b/images/4f97b1dd-3f20-4966-a291-22180680ea99.png)


架構圖顯示將 EBCDIC 檔案轉換為 EC2 執行個體上 ASCII 檔案的程序：

1. 使用 **parse\_copybook\_to\_json.py** 指令碼，您可以將 COBOL 複製手冊轉換為 JSON 檔案。

1. 使用 JSON 檔案和 **extract\_ebcdic\_to\_ascii.py** 指令碼，您可以將 EBCDIC 資料轉換為 ASCII 檔案。

**自動化和擴展**

在第一個手動檔案轉換所需的資源就緒之後，您就可以自動化檔案轉換。此模式不包含自動化的指示。有多種方法可自動化轉換。以下是一種可能方法的概觀：

1. 將 AWS Command Line Interface (AWS CLI) 和 Python 指令碼命令封裝為 shell 指令碼。

1. 建立 AWS Lambda 函數，以非同步方式將 shell 指令碼任務提交至 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 Elastic Compute Cloud (Amazon EC2)](https://docs.aws.amazon.com/ec2/?id=docs_gateway) 在 AWS 雲端中提供可擴展的運算容量。您可以視需要啟動任意數量的虛擬伺服器，並快速向上或向下擴展。
+ [Amazon Elastic Block Store (Amazon EBS)](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AmazonEBS.html) 提供區塊層級儲存磁碟區，可與 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體搭配使用。
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 是一種開放原始碼工具，可協助您透過命令列 shell 中的命令與 AWS 服務互動。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 可透過控制已驗證並獲授權使用的人員，協助您安全地管理對 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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 啟動 EC2 執行個體。 | EC2 執行個體必須具有傳出網際網路存取。這可讓執行個體存取 GitHub 上可用的 Python 原始程式碼。若要建立執行個體：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/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_tw/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_tw/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_tw/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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 將 COBOL 複製手冊剖析到 JSON 配置檔案中。 | 在 `mainframe-data-utilities` 資料夾內，執行 **parse\_copybook\_to\_json.py **指令碼。此自動化模組會從 COBOL 複製手冊讀取檔案配置，並建立 JSON 檔案。JSON 檔案包含從來源檔案解譯和擷取資料所需的資訊。這會從 COBOL 複製手冊建立 JSON 中繼資料。<br />下列命令會將 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><br />指令碼會列印收到的引數。<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><br />如需引數的詳細資訊，請參閱 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_tw/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_tw/prescriptive-guidance/latest/patterns/convert-and-unpack-ebcdic-data-to-ascii-on-aws-by-using-python.html)<br />如需 JSON 配置檔案的詳細資訊，請參閱 GitHub 儲存庫中的[README 檔案](https://github.com/aws-samples/mainframe-data-utilities/blob/main/README.md)。 | 一般 AWS、JSON | 
| 建立 ASCII 檔案。 | 執行 **extract\_ebcdic\_to\_ascii.py** 指令碼，此指令碼包含在複製的 GitHub 儲存庫中。此指令碼會讀取 EBCDIC 檔案，並寫入已轉換且可讀取的 ASCII 檔案。<pre>python3 extract_ebcdic_to_ascii.py -local-json sample-data/cobpack2-list.json</pre><br />當指令碼處理 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><br />如需如何變更列印頻率的資訊，請參閱 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_tw/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><br />如果您使用範例 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><br />若要評估來源和目標檔案之間的等效性，需要 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) (Amazon EC2 文件）

**教學課程**
+ [使用 AWS Lambda 排程 SSH 任務](https://aws.amazon.com/blogs/compute/scheduling-ssh-jobs-using-aws-lambda/) (AWS 部落格文章）
+ [使用 Amazon S3 觸發來叫用 Lambda 函數](https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html) (AWS Lambda 文件）