

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

# 在 Glue AWS 中使用 Avro 格式
<a name="aws-glue-programming-etl-format-avro-home"></a>

AWS Glue 從來源擷取資料，並將資料寫入以各種資料格式存放和傳輸的目標。如果您的資料是以 Avro 資料格式存放或傳輸，本文件會介紹在 Glue AWS 中使用資料的可用功能。

AWS Glue 支援使用 Avro 格式。此格式是效能導向、以資料列為基礎的資料格式。如需標準授權單位的格式簡介，請參閱 [Apache Avro 1.8.2 Documentation](https://avro.apache.org/docs/1.8.2/)。

您可以使用 AWS Glue 從 Amazon S3 和串流來源讀取 Avro 檔案，以及將 Avro 檔案寫入 Amazon S3。您可以讀取和寫入來自 S3 的包含 Avro 檔案的 `bzip2` 和 `gzip` 封存。此外，您可以寫入 `deflate`、`snappy` 和包含 Avro 檔案的 `xz` 封存。您可以在 [S3 連線參數](aws-glue-programming-etl-connect-s3-home.md#aws-glue-programming-etl-connect-s3) 上設定壓縮行為，而不是在本頁討論的組態中設定。

下表顯示哪些常見的 AWS Glue 操作支援 Avro 格式選項。


| 讀取 | 寫入 | 串流讀取 | 對小型檔案進行分組 | 任務書籤 | 
| --- | --- | --- | --- | --- | 
| 支援 | 支援 | 支援\$1 | 不支援 | 支援 | 

\$1 表示支援，但有所限制。如需詳細資訊，請參閱 [Avro 串流來源的注意事項與限制](add-job-streaming.md#streaming-avro-notes)。

## 範例：從 S3 讀取 Avro 檔案或資料夾
<a name="aws-glue-programming-etl-format-avro-read"></a>

**先決條件：**您需要指向希望讀取的 Avro 檔案或資料夾的 S3 路徑 (`s3path`)。

**組態：**在您的函數選項中，指定 `format="avro"`。在您的 `connection_options` 中，使用 `paths` 鍵來指定 `s3path`。您可以在 `connection_options` 中設定讀取器與 S3 的互動方式。如需詳細資訊，請參閱 Glue 中 ETL AWS 輸入和輸出的資料格式選項：[Amazon S3 連線選項參考](aws-glue-programming-etl-connect-s3-home.md#aws-glue-programming-etl-connect-s3)。您可以在 `format_options` 中設定讀取器解譯 Avro 的方式。如需詳細資訊，請參閱 [Avro 組態參考](#aws-glue-programming-etl-format-avro-reference)。

下列 AWS Glue ETL 指令碼顯示從 S3 讀取 Avro 檔案或資料夾的程序：

------
#### [ Python ]

在此範例中，使用 [create\$1dynamic\$1frame.from\$1options](aws-glue-api-crawler-pyspark-extensions-glue-context.md#aws-glue-api-crawler-pyspark-extensions-glue-context-create_dynamic_frame_from_options) 方法。

```
from pyspark.context import SparkContext
from awsglue.context import GlueContext

sc = SparkContext.getOrCreate()
glueContext = GlueContext(sc)

dynamicFrame = glueContext.create_dynamic_frame.from_options(
    connection_type="s3",
    connection_options={"paths": ["s3://s3path"]},
    format="avro"
)
```

------
#### [ Scala ]

在此範例中，使用 [getSourceWithFormat](glue-etl-scala-apis-glue-gluecontext.md#glue-etl-scala-apis-glue-gluecontext-defs-getSourceWithFormat) 操作。

```
import com.amazonaws.services.glue.util.JsonOptions
import com.amazonaws.services.glue.GlueContext
import org.apache.spark.sql.SparkContext

object GlueApp {
  def main(sysArgs: Array[String]): Unit = {
    val spark: SparkContext = new SparkContext()
    val glueContext: GlueContext = new GlueContext(spark)

    val dynamicFrame = glueContext.getSourceWithFormat(
      connectionType="s3",
      format="avro",
      options=JsonOptions("""{"paths": ["s3://s3path"]}""")
    ).getDynamicFrame()
  }
```

------

## 範例：將 Avro 檔案和資料夾寫入 S3
<a name="aws-glue-programming-etl-format-avro-write"></a>

**先決條件：**您需要初始化 DataFrame (`dataFrame`) 或 DynamicFrame (`dynamicFrame`)。您還需要預期的 S3 輸出路徑 `s3path`。

**組態：**在您的函數選項中，指定 `format="avro"`。在您的 `connection_options` 中，使用 `paths` 索引鍵指定 `s3path`。您可以在 `connection_options` 中進一步更改寫入器與 S3 的互動方式。如需詳細資訊，請參閱 Glue 中 ETL AWS 輸入和輸出的資料格式選項：[Amazon S3 連線選項參考](aws-glue-programming-etl-connect-s3-home.md#aws-glue-programming-etl-connect-s3)。您可以在 `format_options` 中更改寫入器解譯 Avro 檔案的方式。如需詳細資訊，請參閱 [Avro 組態參考](#aws-glue-programming-etl-format-avro-reference)。

下列 AWS Glue ETL 指令碼顯示將 Avro 檔案或資料夾寫入 S3 的程序。

------
#### [ Python ]

在此範例中，使用 [write\$1dynamic\$1frame.from\$1options](aws-glue-api-crawler-pyspark-extensions-glue-context.md#aws-glue-api-crawler-pyspark-extensions-glue-context-write_dynamic_frame_from_options) 方法。

```
from pyspark.context import SparkContext
from awsglue.context import GlueContext

sc = SparkContext.getOrCreate()
glueContext = GlueContext(sc)

glueContext.write_dynamic_frame.from_options(
    frame=dynamicFrame,
    connection_type="s3",
    format="avro",
    connection_options={
        "path": "s3://s3path"
    }
)
```

------
#### [ Scala ]

在此範例中，使用 [getSinkWithFormat](glue-etl-scala-apis-glue-gluecontext.md#glue-etl-scala-apis-glue-gluecontext-defs-getSinkWithFormat) 方法。

```
import com.amazonaws.services.glue.util.JsonOptions
import com.amazonaws.services.glue.{DynamicFrame, GlueContext}
import org.apache.spark.SparkContext

object GlueApp {
  def main(sysArgs: Array[String]): Unit = {
    val spark: SparkContext = new SparkContext()
    val glueContext: GlueContext = new GlueContext(spark)

    glueContext.getSinkWithFormat(
      connectionType="s3",
      options=JsonOptions("""{"path": "s3://s3path"}"""),
      format="avro"
    ).writeDynamicFrame(dynamicFrame)
  }
}
```

------

## Avro 組態參考
<a name="aws-glue-programming-etl-format-avro-reference"></a>

無論 Glue AWS 程式庫在何處指定 ，您都可以使用下列`format_options`值`format="avro"`：
+ `version` — 指定 Apache Avro Reader/Writer 格式支援的版本。預設值為 "1.7"。您可以指定 `format_options={"version": “1.8”}`，以啟用 Avro 邏輯類型的讀取和寫入。如需詳細資訊，請參閱 [Apache Avro 1.7.7 規格](https://avro.apache.org/docs/1.7.7/spec.html)和 [Apache Avro 1.8.2 規格](https://avro.apache.org/docs/1.8.2/spec.html)。

  Apache Avro 1.8 連接器支援以下邏輯類型的轉換：

對於讀取器：此表顯示適用於 Avro Reader 1.7 和 1.8 的 Avro 資料類型 (邏輯類型與 Avro 基本類型) 與 AWS Glue `DynamicFrame` 資料類型之間的轉換。


| Avro 資料類型：邏輯類型 | Avro 資料類型：Avro 基本類型 | GlueDynamicFrame 資料類型：Avro Reader 1.7 | GlueDynamicFrame 資料類型：Avro Reader 1.8 | 
| --- | --- | --- | --- | 
| Decimal (小數) | 位元組 | BINARY | Decimal (小數) | 
| Decimal (小數) | 固定 | BINARY | Decimal (小數) | 
| Date | int | INT | Date | 
| 時間 (毫秒) | int | INT | INT | 
| 時間 (微秒) | long | LONG | LONG | 
| 時間戳記 (毫秒) | long | LONG | 時間戳記 | 
| 時間戳記 (微秒) | long | LONG | LONG | 
| 持續時間 (不是邏輯類型) | 12 (固定) | BINARY | BINARY | 

對於寫入器：此表顯示適用於 Avro Writer 1.7 和 1.8 的 AWS Glue `DynamicFrame` 資料類型與 Avro 資料類型之間的轉換。


| AWS Glue `DynamicFrame` 資料類型 | Avro 資料類型：Avro Writer 1.7 | Avro 資料類型：Avro Writer 1.8 | 
| --- | --- | --- | 
| Decimal (小數) | String | decimal | 
| Date | String | date | 
| 時間戳記 | String | timestamp-micros | 

## Avro Spark DataFrame 支援
<a name="aws-glueprogramming-etl-format-avro-dataframe-support"></a>

為了從 Spark DataFrame API 使用Avro，您需要為相應的 Spark 版本安裝 Spark Avro 外掛程式。任務中可用的 Spark 版本取決於您的 Glue AWS 版本。如需 Spark 版本的詳細資訊，請參閱 [AWS Glue 版本](release-notes.md)。這個外掛程式是由 Apache 維護，我們不作出具體的支援保證。

在 AWS Glue 2.0 中 - 使用 Spark Avro 外掛程式的 2.4.3 版。您可以在 Maven Central 找到此 JAR，請參閱 [org.apache.spark:spark-avro\$12.12:2.4.3](https://search.maven.org/artifact/org.apache.spark/spark-avro_2.12/3.1.1/jar)。

在 AWS Glue 3.0 中 - 使用 Spark Avro 外掛程式的 3.1.1 版。您可以在 Maven Central 找到此 JAR，請參閱 [org.apache.spark:spark-avro\$12.12:3.1.1](https://search.maven.org/artifact/org.apache.spark/spark-avro_2.12/3.1.1/jar)。

若要在 Glue ETL AWS 任務中包含額外的 JARs，請使用 `--extra-jars`任務參數。如需有關任務參數的詳細資訊，請參閱 [在 Glue AWS 任務中使用任務參數](aws-glue-programming-etl-glue-arguments.md)。您也可以在 AWS 管理主控台中設定此參數。