

如需與 Amazon Timestream for LiveAnalytics 類似的功能，請考慮使用 Amazon Timestream for InfluxDB。它提供簡化的資料擷取和單一位數毫秒查詢回應時間，以進行即時分析。[在這裡](https://docs.aws.amazon.com//timestream/latest/developerguide/timestream-for-influxdb.html)進一步了解。

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

# 從 Timestream for LiveAnalytics 的 UNLOAD 範例使用案例
<a name="export-unload-example-use-case"></a>

假設您正在監控電子商務網站的使用者工作階段指標、流量來源和產品購買。您正在使用 Timestream for LiveAnalytics 來衍生使用者行為、產品銷售的即時洞見，並在流量管道 （組織搜尋、社交媒體、直接流量、付費行銷活動等） 上執行行銷分析，以協助客戶前往網站。

**Topics**
+ [匯出不含任何分割區的資料](#export-unload-example-sample-1)
+ [依頻道分割資料](#export-unload-example-sample-2)
+ [依事件分割資料](#export-unload-example-sample-3)
+ [依頻道和事件分割資料](#export-unload-example-sample-4)
+ [資訊清單和中繼資料檔案](#export-unload-example-manifest-metadata)
+ [使用 Glue 爬蟲程式建置 Glue Data Catalog](#export-unload-example-using-glue-crawlers)

## 匯出不含任何分割區的資料
<a name="export-unload-example-sample-1"></a>

您想要以 CSV 格式匯出資料的最後兩天。

```
UNLOAD(SELECT user_id, ip_address, event, session_id, measure_name, time, 
query, quantity, product_id, channel 
FROM sample_clickstream.sample_shopping 
WHERE time BETWEEN ago(2d) AND now())  
TO 's3://<bucket_name>/withoutpartition' 
WITH (  format='CSV',   
compression='GZIP')
```

## 依頻道分割資料
<a name="export-unload-example-sample-2"></a>

您想要以 CSV 格式匯出最後兩天的資料，但想要將來自每個流量通道的資料放在個別的資料夾中。若要這樣做，您需要使用 `channel`欄分割資料，如下所示。

```
UNLOAD(SELECT user_id, ip_address, event, session_id, measure_name, time, 
query, quantity, product_id, channel 
FROM sample_clickstream.sample_shopping 
WHERE time BETWEEN ago(2d) AND now())  
TO 's3://<bucket_name>/partitionbychannel/' 
WITH (  
partitioned_by = ARRAY ['channel'], 
format='CSV',   
compression='GZIP')
```

## 依事件分割資料
<a name="export-unload-example-sample-3"></a>

您想要以 CSV 格式匯出最後兩天的資料，但想要將每個事件的資料放在個別的資料夾中。若要這樣做，您需要使用 `event`資料欄分割資料，如下所示。

```
UNLOAD(SELECT user_id, ip_address, channel, session_id, measure_name, time, 
query, quantity, product_id, event 
FROM sample_clickstream.sample_shopping 
WHERE time BETWEEN ago(2d) AND now())  
TO 's3://<bucket_name>/partitionbyevent/' 
WITH (  
partitioned_by = ARRAY ['event'], 
format='CSV',   
compression='GZIP')
```

## 依頻道和事件分割資料
<a name="export-unload-example-sample-4"></a>

您想要以 CSV 格式匯出最後兩天的資料，但想要將每個頻道和頻道內的資料存放在個別資料夾中。若要這樣做，您需要使用 `channel`和 資料`event`欄來分割資料，如下所示。

```
UNLOAD(SELECT user_id, ip_address, session_id, measure_name, time, 
query, quantity, product_id, channel,event 
FROM sample_clickstream.sample_shopping 
WHERE time BETWEEN ago(2d) AND now())  
TO 's3://<bucket_name>/partitionbychannelevent/' 
WITH (  
partitioned_by = ARRAY ['channel','event'], 
format='CSV',   
compression='GZIP')
```

## 資訊清單和中繼資料檔案
<a name="export-unload-example-manifest-metadata"></a>

### 清單檔案
<a name="export-unload-common-questions-what-information-manifest"></a>

資訊清單檔案提供使用 UNLOAD 執行匯出的檔案清單資訊。清單檔案可在提供的 S3 儲存貯體中使用，檔案名稱為：`S3://bucket_name/<queryid>_<UUID>_manifest.json`。資訊清單檔案將包含結果資料夾中檔案的 URL、個別檔案的記錄數和大小，以及查詢中繼資料 （這是匯出至 S3 的查詢總位元組數和總列數）。

```
{
  "result_files": [
    {
        "url":"s3://my_timestream_unloads/ec2_metrics/AEDAGANLHLBH4OLISD3CVOZZRWPX5GV2XCXRBKCVD554N6GWPWWXBP7LSG74V2Q_1448466917_szCL4YgVYzGXj2lS.gz", 
        "file_metadata": 
            { 
                "content_length_in_bytes": 32295, 
                "row_count": 10 
            }
    },
    {
        "url":"s3://my_timestream_unloads/ec2_metrics/AEDAGANLHLBH4OLISD3CVOZZRWPX5GV2XCXRBKCVD554N6GWPWWXBP7LSG74V2Q_1448466917_szCL4YgVYzGXj2lS.gz", 
        "file_metadata": 
            { 
                "content_length_in_bytes": 62295, 
                "row_count": 20 
            }
    },
  ],
  "query_metadata": 
    {
      "content_length_in_bytes": 94590, 
      "total_row_count": 30,
      "result_format": "CSV",
      "result_version": "Amazon Timestream version 1.0.0"  
    },
  "author": {
        "name": "Amazon Timestream", 
        "manifest_file_version": "1.0" 
  }
}
```

### 中繼資料
<a name="export-unload-common-questions-what-information-metadata"></a>

中繼資料檔案提供有關資料集的其他資訊，例如資料欄名稱、資料欄類型和結構描述。中繼資料檔案可在提供的 S3 儲存貯體中使用，檔案名稱為：S3：//bucket\$1name/<queryid>\$1<UUID>\$1metadata.json 

以下是中繼資料檔案的範例。

```
{
    "ColumnInfo": [
        {
            "Name": "hostname",
            "Type": {
                "ScalarType": "VARCHAR"
            }
        },
        {
            "Name": "region",
            "Type": {
                "ScalarType": "VARCHAR"
            }
        },
        {
            "Name": "measure_name",
            "Type": {
                "ScalarType": "VARCHAR"
            }
        },
        {
            "Name": "cpu_utilization",
            "Type": {
                "TimeSeriesMeasureValueColumnInfo": {
                    "Type": {
                        "ScalarType": "DOUBLE"
                    }
                }
            }
        }
  ],
  "Author": {
        "Name": "Amazon Timestream", 
        "MetadataFileVersion": "1.0" 
  }
}
```

在中繼資料檔案中共用的資料欄資訊與`SELECT`查詢的查詢 API 回應中`ColumnInfo`傳送的結構相同。

## 使用 Glue 爬蟲程式建置 Glue Data Catalog
<a name="export-unload-example-using-glue-crawlers"></a>

1. 使用 Admin 登入資料登入您的帳戶，以進行下列驗證。

1. 使用[此處](https://docs.aws.amazon.com/glue/latest/ug/tutorial-add-crawler.html)提供的準則建立 Glue Database 的爬蟲程式。請注意，資料來源中提供的 S3 資料夾應該是`UNLOAD`結果資料夾，例如 `s3://my_timestream_unloads/results`。

1. 依照[此處](https://docs.aws.amazon.com/glue/latest/ug/tutorial-add-crawler.html#tutorial-add-crawler-step2)的指導方針執行爬蟲程式。

1. 檢視 Glue 資料表。
   + 前往 **AWS Glue** → **資料表**。
   + 您會看到在建立爬蟲程式時，使用提供的資料表字首建立新的資料表。
   + 您可以按一下資料表詳細資訊檢視來查看結構描述和分割區資訊。

以下是使用 AWS Glue Data Catalog 的其他 AWS 服務和開放原始碼專案。
+ **Amazon Athena** – 如需詳細資訊，請參閱《Amazon Athena 使用者指南》中的[了解資料表、資料庫和資料目錄](https://docs.aws.amazon.com/athena/latest/ug/understanding-tables-databases-and-the-data-catalog.html)。
+ **Amazon Redshift Spectrum** – 如需詳細資訊，請參閱《[Amazon Redshift 資料庫開發人員指南》中的使用 Amazon Redshift Spectrum 查詢外部資料](https://docs.aws.amazon.com/redshift/latest/dg/c-using-spectrum.html)。
+ **Amazon EMR** – 如需詳細資訊，請參閱《[Amazon EMR 管理指南》中的使用資源型政策來存取 AWS Glue Data Catalog](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-iam-roles-glue.html)。
+ **AWS 適用於 Apache Hive 中繼存放區的 Glue Data Catalog 用戶端** – 如需此 GitHub 專案的詳細資訊，請參閱[AWS 適用於 Apache Hive 中繼存放區的 Glue Data Catalog 用戶端](https://github.com/awslabs/aws-glue-data-catalog-client-for-apache-hive-metastore)。