

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

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

## 摘要
<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 記錄是否應該變成 null 字元或產生錯誤。 如需詳細資訊，請參閱 Athena 文件中的[讀取 JSON 資料的最佳實務](https://docs.aws.amazon.com/athena/latest/ug/parsing-JSON.html)。
+ 此模式只會考慮簡單和少量的 JSON 格式資料。如果您想要大規模使用這些概念，請考慮套用資料分割，並將您的資料合併成較大的檔案。

## Architecture
<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_tw/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>

**AWS 服務**
+ [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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 S3 儲存貯體。 | 若要建立儲存貯體以存放 JSON 檔案，請登入 AWS 管理主控台，開啟 [Amazon S3 主控台](https://console.aws.amazon.com/s3/)，然後選擇**建立儲存貯**體。如需詳細資訊，請參閱 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)。 Amazon S3  | 系統管理員 | 

### 分析 Athena 中的資料
<a name="analyze-data-in-ate"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立用於映射 JSON 資料的資料表。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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_tw/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_tw/prescriptive-guidance/latest/patterns/analyze-and-visualize-nested-json-data-with-amazon-athena-and-amazon-quicksight.html) | 開發人員 | 

### 在 Quick Sight 中視覺化資料
<a name="visualize-data-in-qsight"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 在 Quick Sight 中將 Athena 設定為資料來源。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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_tw/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 Quick Sight 教學課程](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/)（部落格文章）