

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

# 步骤 3：将实体分析输出格式化为 Amazon Kendra 元数据
<a name="tutorial-search-metadata-format-output"></a>

要将 Amazon Comprehend 提取的实体转换为 Amazon Kendra 索引所需的元数据格式，您需要运行 Python 3 脚本。转换结果存储在 Amazon S3 存储桶中的 `metadata` 文件夹中。

有关 Amazon Kendra 元数据格式和结构的更多信息，请参阅 [S3 文档元数据](https://docs.aws.amazon.com/kendra/latest/dg/s3-metadata.html)。

**Topics**
+ [下载和提取 Amazon Comprehend 的输出](#tutorial-search-metadata-format-output-download-extract)
+ [将输出上传到 S3 存储桶](#tutorial-search-metadata-format-output-upload)
+ [将输出转换为 Amazon Kendra 元数据格式](#tutorial-search-metadata-format-output-script)
+ [清理 Amazon S3 存储桶](#tutorial-search-metadata-format-output-cleanup)

## 下载和提取 Amazon Comprehend 的输出
<a name="tutorial-search-metadata-format-output-download-extract"></a>

要格式化 Amazon Comprehend 实体分析输出，您必须先下载 Amazon Comprehend 实体分析 `output.tar.gz` 档案并提取实体分析文件。

### 下载和解压缩输出文件（控制台）
<a name="tutorial-search-metadata-download-extract-console"></a>

1. 在 Amazon Comprehend 控制台导航窗格中，导航至**分析作业**。

1. 选择您的实体分析作业 `data-entities-analysis`。

1. 在**输出**下，选择**输出数据位置**旁边显示的链接。这会将您重定向到 S3 存储桶中的 `output.tar.gz` 存档。

1. 在**概述**选项卡中，选择**下载**。
**提示**  
所有 Amazon Comprehend 分析作业的输出具有相同的名称。重命名存档将帮助您更轻松地对其进行跟踪。

1. 将下载的 Amazon Comprehend 文件解压并提取到您的设备上。

### 下载并解压缩文件（AWS CLI）。
<a name="tutorial-search-metadata-download-extract-cli"></a>

1. 要访问您的 S3 存储桶中包含实体分析任务结果的 Amazon Comprehend 自动生成文件夹的名称，请使用以下命令：[describe-entities-detection-job](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/comprehend/describe-entities-detection-job.html)

------
#### [ Linux ]

   ```
   aws comprehend describe-entities-detection-job \
             --job-id entities-job-id \
             --region aws-region
   ```

   其中：
   + *entities-job-id*你是`comprehend-job-id`从中救出来的[步骤 2：在 Amazon Comprehend 上运行实体分析任务 Amazon Comprehend](tutorial-search-metadata-entities-analysis.md)，
   + *aws-region*是你所在 AWS 的地区。

------
#### [ macOS ]

   ```
   aws comprehend describe-entities-detection-job \
             --job-id entities-job-id \
             --region aws-region
   ```

   其中：
   + *entities-job-id*你是`comprehend-job-id`从哪里救出来的[步骤 2：在 Amazon Comprehend 上运行实体分析任务 Amazon Comprehend](tutorial-search-metadata-entities-analysis.md)，
   + *aws-region*是你所在 AWS 的地区。

------
#### [ Windows ]

   ```
   aws comprehend describe-entities-detection-job ^
             --job-id entities-job-id ^
             --region aws-region
   ```

   其中：
   + *entities-job-id*你是`comprehend-job-id`从哪里救出来的[步骤 2：在 Amazon Comprehend 上运行实体分析任务 Amazon Comprehend](tutorial-search-metadata-entities-analysis.md)，
   + *aws-region*是你所在 AWS 的地区。

------

1. 在实体作业描述中的 `OutputDataConfig` 对象中，复制 `S3Uri` 值并将其保存为文本编辑器中的 `comprehend-S3uri`。
**注意**  
该`S3Uri`值的格式类似于*s3://amzn-s3-demo-bucket/.../output/output.tar.gz*。

1. 要下载实体输出存档，请使用 [copy](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/cp.html) 命令：

------
#### [ Linux ]

   ```
   aws s3 cp s3://amzn-s3-demo-bucket/.../output/output.tar.gz path/output.tar.gz
   ```

   其中：
   + *s3://amzn-s3-demo-bucket/.../output/output.tar.gz*是你保存的`S3Uri`值`comprehend-S3uri`，
   + *path/*是您要保存输出的本地目录。

------
#### [ macOS ]

   ```
   aws s3 cp s3://amzn-s3-demo-bucket/.../output/output.tar.gz path/output.tar.gz
   ```

   其中：
   + *s3://amzn-s3-demo-bucket/.../output/output.tar.gz*是你保存的`S3Uri`值`comprehend-S3uri`，
   + *path/*是您要保存输出的本地目录。

------
#### [ Windows ]

   ```
   aws s3 cp s3://amzn-s3-demo-bucket/.../output/output.tar.gz path/output.tar.gz
   ```

   其中：
   + *s3://amzn-s3-demo-bucket/.../output/output.tar.gz*是你保存的`S3Uri`值`comprehend-S3uri`，
   + *path/*是您要保存输出的本地目录。

------

1. 要提取实体输出，请在终端窗口中运行以下命令：

------
#### [ Linux ]

   ```
   tar -xf path/output.tar.gz -C path/
   ```

   其中：
   + *path/*是本地设备上已下载`output.tar.gz`档案的文件路径。

------
#### [ macOS ]

   ```
   tar -xf path/output.tar.gz -C path/
   ```

   其中：
   + *path/*是本地设备上已下载`output.tar.gz`档案的文件路径。

------
#### [ Windows ]

   ```
   tar -xf path/output.tar.gz -C path/
   ```

   其中：
   + *path/*是本地设备上已下载`output.tar.gz`档案的文件路径。

------

完成此步骤后，您的设备上应该有一个名为 `output` 的文件，其中包含已识别的 Amazon Comprehend 实体的列表。

## 将输出上传到 S3 存储桶
<a name="tutorial-search-metadata-format-output-upload"></a>

下载并解压 Amazon Comprehend 实体分析文件后，您可以将提取的 `output` 文件上传到您的 Amazon S3 存储桶。

### 上传提取的 Amazon Comprehend 输出文件（控制台）
<a name="tutorial-search-metadata-upload-output-console"></a>

1. 打开 Amazon S3 控制台，网址为 [https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/)。

1. 在**存储桶**中，单击存储桶的名称，然后选择**上传**。

1. 在**文件和文件夹**中，选择**添加文件**。

1. 在对话框中，导航到设备中提取的 `output` 文件，将其选中，然后选择**打开**。

1. 保留**目标**、**权限**和**属性**的默认设置。

1. 选择**上传**。

### 上传提取的 Amazon Comprehend 输出文件（AWS CLI）
<a name="tutorial-search-metadata-upload-output-cli"></a>

1. 要将提取的 `output` 文件上传到您的存储桶，请使用 [copy](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/cp.html) 命令：

------
#### [ Linux ]

   ```
   aws s3 cp path/output s3://amzn-s3-demo-bucket/output
   ```

   其中：
   + *path/*是您提取的文件的本地`output`文件路径，
   + amzn-s3-demo-bucket 是您的 S3 存储桶名称。

------
#### [ macOS ]

   ```
   aws s3 cp path/output s3://amzn-s3-demo-bucket/output
   ```

   其中：
   + *path/*是您提取的文件的本地`output`文件路径，
   + amzn-s3-demo-bucket 是您的 S3 存储桶名称。

------
#### [ Windows ]

   ```
   aws s3 cp path/output s3://amzn-s3-demo-bucket/output
   ```

   其中：
   + *path/*是您提取的文件的本地`output`文件路径，
   + amzn-s3-demo-bucket 是您的 S3 存储桶名称。

------

1. 为确保 `output` 文件已成功上传到您的 S3 存储桶，请使用 [list](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/ls.html) 命令检查其内容：

------
#### [ Linux ]

   ```
   aws s3 ls s3://amzn-s3-demo-bucket/
   ```

   其中：
   + amzn-s3-demo-bucket 是您的 S3 存储桶名称。

------
#### [ macOS ]

   ```
   aws s3 ls s3://amzn-s3-demo-bucket/
   ```

   其中：
   + amzn-s3-demo-bucket 是您的 S3 存储桶名称。

------
#### [ Windows ]

   ```
   aws s3 ls s3://amzn-s3-demo-bucket/
   ```

   其中：
   + amzn-s3-demo-bucket 是您的 S3 存储桶名称。

------

## 将输出转换为 Amazon Kendra 元数据格式
<a name="tutorial-search-metadata-format-output-script"></a>

要将 Amazon Comprehend 输出转换为 Amazon Kendra 元数据，您需要运行 Python 3 脚本。如果您使用的是控制台，则使用 AWS CloudShell 此步骤。

### 运行 Python 3 脚本（控制台）
<a name="tutorial-search-metadata-format-output-console"></a>

1. 将 [converter.py.zip](https://docs.aws.amazon.com/kendra/latest/dg/samples/converter.py.zip) 下载到您的设备上。

1. 提取 Python 3 文件 `converter.py`。

1. 登录[AWS 管理控制台](https://aws.amazon.com/console/)，确保将您的 AWS 区域设置为与 S3 存储桶和 Amazon Comprehend 分析任务相同的区域。

1. 选择**AWS CloudShell 图标**或**AWS CloudShell**在顶部导航栏的**搜索**框中键入以启动环境。
**注意**  
首次在新的浏览器窗口中 AWS CloudShell 启动时，将显示一个欢迎面板并列出主要功能。关闭此面板后，表示 Shell 已经准备就绪，可以进行交互。

1. 终端准备就绪后，从导航窗格中选择**操作**，然后从菜单中选择**上传文件**。

1. 在打开的对话框中点击**选择文件**，然后从您的设备中选择下载的 Python 3 文件 `converter.py`。选择**上传**。

1. 在 AWS CloudShell 环境中，输入以下命令：

   ```
   python3 converter.py
   ```

1. 当 Shell 界面提示您**输入 S3 存储桶的名称**时，输入您的 S3 存储桶的名称并按 Enter。

1. 当 Shell 界面提示您**输入 Comprehend 输出文件的完整文件路径**时，输入 **output** 并按 Enter。

1. 当 Shell 界面提示您**输入元数据文件夹的完整文件路径**时，输入 **metadata/** 并按 Enter。

**重要**  
要使元数据格式正确，步骤 8-10 中的输入值必须精确。

### 运行 Python 3 脚本（AWS CLI）
<a name="tutorial-search-metadata-format-output-cli"></a>

1. 要下载 Python 3 文件 `converter.py`，请在终端窗口中运行以下命令：

------
#### [ Linux ]

   ```
   curl -o path/converter.py.zip https://docs.aws.amazon.com/kendra/latest/dg/samples/converter.py.zip
   ```

   其中：
   + *path/*是您要将压缩文件保存到的位置的文件路径。

------
#### [ macOS ]

   ```
   curl -o path/converter.py.zip https://docs.aws.amazon.com/kendra/latest/dg/samples/converter.py.zip
   ```

   其中：
   + *path/*是您要将压缩文件保存到的位置的文件路径。

------
#### [ Windows ]

   ```
   curl -o path/converter.py.zip https://docs.aws.amazon.com/kendra/latest/dg/samples/converter.py.zip
   ```

   其中：
   + *path/*是您要将压缩文件保存到的位置的文件路径。

------

1. 要提取 Python 3 文件，请在终端窗口中运行以下命令：

------
#### [ Linux ]

   ```
   unzip path/converter.py.zip -d path/
   ```

   其中：
   + *path/*是您保存`converter.py.zip`的文件路径。

------
#### [ macOS ]

   ```
   unzip path/converter.py.zip -d path/
   ```

   其中：
   + *path/*是您保存`converter.py.zip`的文件路径。

------
#### [ Windows ]

   ```
   tar -xf path/converter.py.zip -C path/
   ```

   其中：
   + *path/*是您保存`converter.py.zip`的文件路径。

------

1. 通过运行以下命令确保已将 Boto3 安装在您的设备上。

------
#### [ Linux ]

   ```
   pip3 show boto3
   ```

------
#### [ macOS ]

   ```
   pip3 show boto3
   ```

------
#### [ Windows ]

   ```
   pip3 show boto3
   ```

------
**注意**  
如果尚未安装 Boto3，请运行 `pip3 install boto3` 进行安装。

1. 要运行 Python 3 脚本以转换 `output` 文件，请运行以下命令。

------
#### [ Linux ]

   ```
   python path/converter.py
   ```

   其中：
   + *path/*是您保存`converter.py.zip`的文件路径。

------
#### [ macOS ]

   ```
   python path/converter.py
   ```

   其中：
   + *path/*是您保存`converter.py.zip`的文件路径。

------
#### [ Windows ]

   ```
   python path/converter.py
   ```

   其中：
   + *path/*是您保存`converter.py.zip`的文件路径。

------

1. 当 AWS CLI 提示您输入时`Enter the name of your S3 bucket`，输入您的 S3 存储桶的名称，然后按 Enter。

1. 当 AWS CLI 提示您这样做时`Enter the full filepath to your Comprehend output file`，输入**output**并按 Enter。

1. 当 AWS CLI 提示您这样做时`Enter the full filepath to your metadata folder`，输入**metadata/**并按 Enter。

**重要**  
要使元数据格式正确，步骤 5-7 中的输入值必须精确。

在此步骤结束时，格式化的元数据将存放在您的 S3 存储桶的 `metadata` 文件夹中。

## 清理 Amazon S3 存储桶
<a name="tutorial-search-metadata-format-output-cleanup"></a>

由于 Amazon Kendra 索引会同步存储在存储桶中的所有文件，因此我们建议您清理 Amazon S3 存储桶，以防止出现冗余的搜索结果。

### 清理 Amazon S3 存储桶（控制台）。
<a name="tutorial-search-metadata-cleanup-bucket-console"></a>

1. 打开 Amazon S3 控制台，网址为 [https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/)。

1. 在**存储桶**中，选择您的存储桶，然后选择 Amazon Comprehend 实体分析输出文件夹、Amazon Comprehend 实体分析 `.temp` 文件和提取的 Amazon Comprehend `output` 文件。

1. 从**概览**选项卡中选择**删除**。

1. 在**删除对象**中，选择**永久删除对象？**，然后在文本输入字段中输入 **permanently delete**。

1. 选择**删除对象**。

### 清理 Amazon S3 存储桶（AWS CLI）
<a name="tutorial-search-metadata-cleanup-bucket-cli"></a>

1. 要删除 S3 存储桶中除 `data` 和 `metadata`文件夹之外的所有文件，请在 AWS CLI中使用 [remove](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/rm.html) 命令：

------
#### [ Linux ]

   ```
   aws s3 rm s3://amzn-s3-demo-bucket/ --recursive --exclude "data/*" --exclude "metadata/*"
   ```

   其中：
   + amzn-s3-demo-bucket 是您的 S3 存储桶名称。

------
#### [ macOS ]

   ```
   aws s3 rm s3://amzn-s3-demo-bucket/ --recursive --exclude "data/*" --exclude "metadata/*"
   ```

   其中：
   + amzn-s3-demo-bucket 是您的 S3 存储桶名称。

------
#### [ Windows ]

   ```
   aws s3 rm s3://amzn-s3-demo-bucket/ --recursive --exclude "data/*" --exclude "metadata/*"
   ```

   其中：
   + amzn-s3-demo-bucket 是您的 S3 存储桶名称。

------

1. 为确保已成功从您的 S3 存储桶删除对象，请使用 [list](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/ls.html) 命令检查其内容：

------
#### [ Linux ]

   ```
   aws s3 ls s3://amzn-s3-demo-bucket/
   ```

   其中：
   + amzn-s3-demo-bucket 是您的 S3 存储桶名称。

------
#### [ macOS ]

   ```
   aws s3 ls s3://amzn-s3-demo-bucket/
   ```

   其中：
   + amzn-s3-demo-bucket 是您的 S3 存储桶名称。

------
#### [ Windows ]

   ```
   aws s3 ls s3://amzn-s3-demo-bucket/
   ```

   其中：
   + amzn-s3-demo-bucket 是您的 S3 存储桶名称。

------

在本步骤结束时，您已将 Amazon Comprehend 实体分析输出转换为 Amazon Kendra 元数据。现在，您可以创建 Amazon Kendra 索引。