

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

# 在 Glue AWS 中使用內容感知與 Amazon Q Data Integration
<a name="q-context-awareness"></a>

 您現在可以在 Amazon Q Data Integration 中使用查詢型內容感知和 PySpark DataFrame 程式碼產生，從而更有效地建立資料處理任務。例如，您可以使用此提示來產生 PySpark 程式碼：「建立任務以使用連線‘erp\_conn’從 Redshift 資料表‘analytics.salesorder’載入銷售資料、篩選低於 50 美元的 order\_amount，並以 parquet 格式儲存至 Amazon S3。」 

 Amazon Q 會根據您的提示產生指令碼，並使用問題中提供的詳細資訊來設定資料整合工作流程，例如連線組態、結構描述詳細資訊、資料庫/資料表名稱，以及用於轉換的資料欄規格。會繼續修訂諸如連線選項密碼等敏感資訊。

 如果提示中未提供必要資訊，Amazon Q 會放置預留位置，您必須在執行程式碼之前使用適當的值更新產生的程式碼。

 以下是如何使用內容感知的範例。

## 範例：互動
<a name="q-context-awareness-examples"></a>

### 讀取 Amazon S3 檔案並寫入到 Amazon S3 儲存貯體
<a name="q-context-awareness-examples-generated-script-1"></a>

 提示：`Create an AWS Glue spark job that reads a file s3://amzn-s3-demo-bucket-input-bucket-name/input-filename.csv and saves it into the parquet file under directory s3://amzn-s3-demo-bucket-output-bucket-name/output-s3-prefix/`

```
import sys
from awsglue.transforms import *
from pyspark.context import SparkContext
from pyspark.sql import SparkSession

sc = SparkContext.getOrCreate()
spark = SparkSession.builder.getOrCreate()

# Script generated for node S3DataSource
S3DataSource_dsource1 = spark.read.format("csv").load(
    "s3://amzn-s3-demo-bucket-input-bucket-name/input-filename.csv"
)

# Script generated for node S3DataSink
S3DataSource_dsource1.write.format("parquet").mode("append").save(
    "s3://amzn-s3-demo-bucket-output-bucket-name/output-s3-prefix/"
)
```

![向 Glue 中的 Amazon Q AWS 資料整合要求產生 ETL 指令碼的範例。](http://docs.aws.amazon.com/zh_tw/glue/latest/dg/images/context-awareness-lakehouse-example.gif)


### 從 Lakehouse 取得資料並寫入到資料庫
<a name="q-context-awareness-examples-generated-script-2"></a>

 提示：`write an ETL script to read from a Lakehouse table my-table in database my-database and write it to a RDS MySQL table my-target-table`

 對於未提供資訊的欄位 (例如，MySQL 資料接收需要 connectionName，在產生的程式碼中預設具有預留位置 <connection-name>)，會保留一個預留位置，以便您在執行指令碼之前填寫所需資訊。

 產生的指令碼：

```
import sys
from awsglue.transforms import *
from pyspark.context import SparkContext
from pyspark.sql import SparkSession
from connectivity.adapter import CatalogConnectionHelper

sc = SparkContext.getOrCreate()
spark = SparkSession.builder.getOrCreate()

# Script generated for node S3DataSource
S3DataSource_dsource1 = spark.read.format("parquet").load(
    "s3://amzn-lakehouse-demo-bucket/my-database/my-table"
)

# Script generated for node ConnectionV2DataSink
ConnectionV2DataSink_dsink1_additional_options = {"dbtable": "my-target-table"}
CatalogConnectionHelper(spark).write(
    S3DataSource_dsource1,
    "mysql",
    "<connection-name>",
    ConnectionV2DataSink_dsink1_additional_options,
)
```

![向 Glue 中的 Amazon Q AWS 資料整合要求產生 ETL 指令碼的範例。](http://docs.aws.amazon.com/zh_tw/glue/latest/dg/images/context-awareness-example-interactions.gif)


### 範例：完整的 ETL 工作流程
<a name="q-context-awareness-complex-example"></a>

 下列範例示範如何透過下列提示，要求 AWS Glue 建立 AWS Glue 指令碼以完成完整的 ETL 工作流程：`Create a AWS Glue ETL Script read from two AWS Glue Data Catalog tables venue and event in my database glue_db_4fthqih3vvk1if, join the results on the field venueid, filter on venue state with condition as venuestate=='DC' after joining the results and write output to an Amazon S3 S3 location s3://amz-s3-demo-bucket/output/ in CSV format`。

 工作流程包含來自不同資料來源 （兩個 AWS Glue Data Catalog 資料表） 的讀取，以及讀取之後的幾個轉換，方法是聯結來自兩個讀取的結果，根據某些條件進行篩選，並以 CSV 格式將轉換的輸出寫入 Amazon S3 目的地。

 產生的任務會填入資料來源、轉換和接收操作的詳細資訊，其中包含從使用者問題中擷取的對應資訊，如下所示。

```
import sys
from awsglue.transforms import *
from pyspark.context import SparkContext
from pyspark.sql import SparkSession

sc = SparkContext.getOrCreate()
spark = SparkSession.builder.getOrCreate()

# Script generated for node CatalogDataSource
CatalogDataSource_dsource1 = spark.sql("select * from `glue_db_4fthqih3vvk1if`.`venue`")

# Script generated for node CatalogDataSource
CatalogDataSource_dsource2 = spark.sql("select * from `glue_db_4fthqih3vvk1if`.`event`")

# Script generated for node JoinTransform
JoinTransform_transform1 = CatalogDataSource_dsource1.join(
    CatalogDataSource_dsource2,
    (CatalogDataSource_dsource1["venueid"] == CatalogDataSource_dsource2["venueid"]),
    "inner",
)

# Script generated for node FilterTransform
FilterTransform_transform2 = JoinTransform_transform1.filter("venuestate=='DC'")

# Script generated for node S3DataSink
FilterTransform_transform2.write.format("csv").mode("append").save(
    "s3://amz-s3-demo-bucket/output//output/"
)
```

![向 Glue 中的 Amazon Q AWS 資料整合要求產生 ETL 指令碼的範例。](http://docs.aws.amazon.com/zh_tw/glue/latest/dg/images/context-awareness-complex-example.gif)


## 限制
<a name="q-context-awareness-limitations"></a>
+  內容傳遞：
  +  內容感知功能只會傳遞相同對話中先前使用者查詢的內容。其不會保留上一個查詢以外的內容。
+  支援節點組態：
  +  目前，內容感知僅支援各種節點所需的組態子集。
  +  未來版本中會規劃支援選用欄位。
+  可用性：
  +  Q Chat 和 SageMaker Unified Studio 筆記本支援內容感知和 DataFrame。不過，這些功能尚無法在 Glue Studio AWS 筆記本中使用。