

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

# 使用 Amazon Athena 和 Amazon Quick Sight 分析和可视化嵌套的 JSON 数据
<a name="analyze-and-visualize-nested-json-data-with-amazon-athena-and-amazon-quicksight"></a>

*Anoop Singh，Amazon Web Services*

## Summary
<a name="analyze-and-visualize-nested-json-data-with-amazon-athena-and-amazon-quicksight-summary"></a>

此模式说明了如何使用 Amazon Athena 将嵌套的、JSON 格式的数据结构转换为表格视图，然后在 Amazon Quick Sight 中可视化数据。

您可以将 JSON 格式的数据用于来自操作系统的 API 驱动的数据源，以创建数据产品。这些数据还可以帮助您更好地了解客户及其与产品的互动，便于您量身定制用户体验并预测结果。

## 先决条件和限制
<a name="analyze-and-visualize-nested-json-data-with-amazon-athena-and-amazon-quicksight-prereqs"></a>

**先决条件**
+ 活跃的 AWS 账户
+ 表示嵌套数据结构的 JSON 文件（此模式提供了示例文件）

**限制:**
+ JSON 功能与 Athena 中现有的面向 SQL 的函数很好地集成。但是，它们与 ANSI SQL 不兼容，并且 JSON 文件应将每条记录放在单独的行中。您可能需要使用 Athena 中的 `ignore.malformed.json` 属性来指示是否应将格式错误的 JSON 记录转换为空字符或生成错误。有关更多信息，请参阅 Athena 文档中的[读取 JSON 数据的最佳实践](https://docs.aws.amazon.com/athena/latest/ug/parsing-JSON.html)。
+ 此模式仅考虑简单、少量的 JSON 格式数据。如果要大规模使用这些概念，可以考虑应用数据分区并将数据整合到更大的文件中。

## 架构
<a name="analyze-and-visualize-nested-json-data-with-amazon-athena-and-amazon-quicksight-architecture"></a>

下图显示了此模式的架构和工作流。嵌套的数据结构以 JSON 格式存储在 Amazon Simple Storage Service（Amazon S3）中。在 Athena 中，JSON 数据映射到 Athena 数据结构。然后，您可以创建一个视图来分析数据，并在 Quick Sight 中可视化数据结构。

![\[在 AWS 上分析和可视化嵌套的 JSON 数据。\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/e9ad39a1-e0a4-4429-bdc0-594b68707761/images/474e8747-626f-468c-9c27-c007af79bf2d.png)


## 工具
<a name="analyze-and-visualize-nested-json-data-with-amazon-athena-and-amazon-quicksight-tools"></a>

**Amazon Web Services**
+ [Amazon Simple Storage Service（Amazon S3）](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html)是一项基于云的对象存储服务，可帮助您存储、保护和检索任意数量的数据。此模式使用 Amazon S3 来存储 JSON 文件。
+ [Amazon Athena](https://docs.aws.amazon.com/athena/latest/ug/what-is.html) 是一种交互式查询服务，可帮助您使用标准 SQL 直接在 Amazon S3 中分析数据。此模式使用 Athena 来查询和转换 JSON 数据。只需在中执行一些操作 AWS 管理控制台，您就可以将 Athena 指向您在 Amazon S3 中的数据，然后使用标准 SQL 来运行一次性查询。Athena 没有服务器，因此您无需设置或管理任何基础设施，且只需为您运行的查询付费。Athena 可自动扩展且并行执行查询，因此，即使在数据集很大、查询很复杂的情况下也能很快获得结果。    
+ [Amazon Quick Sight](https://docs.aws.amazon.com/quicksuite/latest/userguide/quick-bi.html) 是一项云规模的商业智能 (BI) 服务，可帮助您在单个控制面板上可视化、分析和报告数据。Quick Sight 可让您轻松创建和发布包含机器学习 (ML) 见解的交互式仪表板。您可以从任何设备访问这些控制面板，并将其嵌入到您的应用程序、门户和网站中。

**示例代码**

下面的 JSON 文件提供了可以在此模式中使用的嵌套数据结构。

```
{
  "symbol": "AAPL",
  "financials": [
    {
      "reportDate": "2017-03-31",
      "grossProfit": 20591000000,
      "costOfRevenue": 32305000000,
      "operatingRevenue": 52896000000,
      "totalRevenue": 52896000000,
      "operatingIncome": 14097000000,
      "netIncome": 11029000000,
      "researchAndDevelopment": 2776000000,
      "operatingExpense": 6494000000,
      "currentAssets": 101990000000,
      "totalAssets": 334532000000,
      "totalLiabilities": 200450000000,
      "currentCash": 15157000000,
      "currentDebt": 13991000000,
      "totalCash": 67101000000,
      "totalDebt": 98522000000,
      "shareholderEquity": 134082000000,
      "cashChange": -1214000000,
      "cashFlow": 12523000000,
      "operatingGainsLosses": null
    }
  ]
}
```

## 操作说明
<a name="analyze-and-visualize-nested-json-data-with-amazon-athena-and-amazon-quicksight-epics"></a>

### 设置 S3 存储桶
<a name="set-up-an-s3-bucket"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建 S3 存储桶。 | 要创建用于存储 JSON 文件的存储桶，请登录并打开 [Amazon S3 控制台](https://console.aws.amazon.com/s3/)，然后选择**创建存储桶**。 AWS 管理控制台有关更多信息，请参阅 Amazon S3 文档中的[创建存储桶](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html)。  | 系统管理员 | 
| 添加嵌套 JSON 数据。 | 将您的 JSON 文件上传到 S3 存储桶。有关示例 JSON 文件，请参阅上一部分的内容。有关说明，请参阅 Amazon S3 文档中的[上传对象](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html)。 | 系统管理员 | 

### 在 Athena 中分析数据
<a name="analyze-data-in-ate"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建用于映射 JSON 数据的表。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/analyze-and-visualize-nested-json-data-with-amazon-athena-and-amazon-quicksight.html)有关创建表的更多信息，请参阅 [Athena 文档](https://docs.aws.amazon.com/athena/latest/ug/creating-tables.html)。 | 开发者版 | 
| 创建数据分析的视图。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/analyze-and-visualize-nested-json-data-with-amazon-athena-and-amazon-quicksight.html)有关创建视图的更多信息，请参阅 [Athena 文档](https://docs.aws.amazon.com/athena/latest/ug/create-view.html)。 | 开发者版 | 
| 分析和验证数据。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/analyze-and-visualize-nested-json-data-with-amazon-athena-and-amazon-quicksight.html) | 开发者版 | 

### 在快速视图中可视化数据
<a name="visualize-data-in-qsight"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 在 Quick Sight 中将 Athena 设置为数据源。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/analyze-and-visualize-nested-json-data-with-amazon-athena-and-amazon-quicksight.html) | 系统管理员 | 
| 使用 Quick Sight 可视化数据 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/analyze-and-visualize-nested-json-data-with-amazon-athena-and-amazon-quicksight.html) | 数据分析人员 | 

## 相关资源
<a name="analyze-and-visualize-nested-json-data-with-amazon-athena-and-amazon-quicksight-resources"></a>
+ [Amazon Athena 文档](https://docs.aws.amazon.com/athena/latest/ug/getting-started.html)
+ [Amazon 快速瞄准教程](https://docs.aws.amazon.com/quicksuite/latest/userguide/example-analysis.html)
+ [使用嵌套 JSON](https://aws.amazon.com/blogs/big-data/create-tables-in-amazon-athena-from-nested-json-and-mappings-using-jsonserde/)（博客文章）