

# AWS Glue で ORC 形式を使用する
<a name="aws-glue-programming-etl-format-orc-home"></a>

AWS Glue はソースからデータを取得し、さまざまなデータ形式で保存および転送されたターゲットにデータを書き込みます。このドキュメントでは、データが ORC データ形式で保存または転送される場合に、AWS Glue でデータを使用する際に利用できる機能について説明します。

AWS Glue は、ORC 形式の使用をサポートしています。この形式は、パフォーマンス指向の列ベースのデータ形式です。標準局による形式の概要については、「[Apache Orc](https://orc.apache.org/docs/)」を参照してください。

AWS Glue を使用して、Amazon S3 およびストリーミングソースから ORC ファイルを読み取り、Amazon S3 に ORC ファイルを書き込むことができます。S3 から、ORC ファイルを含む `bzip` および `gzip` アーカイブを読み書きすることができます。このページで説明する設定ではなく、[S3 接続パラメータ](aws-glue-programming-etl-connect-s3-home.md#aws-glue-programming-etl-connect-s3) 上で圧縮動作を設定します。

次の表は、ORC 形式のオプションをサポートする一般的な AWS Glue の機能を示しています。


| 読み込み | 書き込み | ストリーミングの読み取り | 小さなファイルのグループ化 | ジョブのブックマーク | 
| --- | --- | --- | --- | --- | 
| サポート | サポート対象 | サポート | サポートされていません | サポート対象\$1 | 

\$1AWS Glue バージョン 1.0\$1 でサポート

## 例: S3 から ORC ファイルまたはフォルダを読み取る
<a name="aws-glue-programming-etl-format-orc-read"></a>

**前提条件:** 読み取る ORC ファイルまたはフォルダへの S3 パス (`s3path`) が必要です。

**設定:** 関数オプションで `format="orc"` を指定します。`connection_options` で、`paths` キーを使用して `s3path` を指定します。リーダーが S3 とやり取りする方法は、`connection_options` で設定できます。詳細については、AWS Glue: [Amazon S3 接続のオプションのリファレンス](aws-glue-programming-etl-connect-s3-home.md#aws-glue-programming-etl-connect-s3) の「ETL の接続タイプとオプション」を参照してください。

 次の AWS Glue ETL スクリプトは、S3 から ORC ファイルまたはフォルダを読み取るプロセスを示しています。

------
#### [ 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="orc"
)
```

スクリプト (`pyspark.sql.DataFrame`) では DataFrame を使用することもできます。

```
dataFrame = spark.read\
    .orc("s3://s3path")
```

------
#### [ 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="orc",
      options=JsonOptions("""{"paths": ["s3://s3path"]}""")
    ).getDynamicFrame()
  }
}
```

スクリプト (`pyspark.sql.DataFrame`) では DataFrame を使用することもできます。

```
val dataFrame = spark.read
    .orc("s3://s3path")
```

------

## 例: ORC ファイルおよびフォルダを S3 に書き込む
<a name="aws-glue-programming-etl-format-orc-write"></a>

**前提条件:** 初期化された DataFrame (`dataFrame`) または DynamicFrame (`dynamicFrame`) が必要です。また、予想される S3 出力パスである `s3path` も必要になります。

**設定:** 関数オプションで `format="orc"` を指定します。接続オプションでは、`s3path` を指定するための `paths` キーを使用します。ライターが S3 と対話する方法を、`connection_options` でさらに詳しく変更することができます。詳細については、AWS Glue: [Amazon S3 接続のオプションのリファレンス](aws-glue-programming-etl-connect-s3-home.md#aws-glue-programming-etl-connect-s3) の「ETL の入力および出力のデータ形式オプション」を参照してください。次のコード例は、プロセスを示しています。

------
#### [ 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="orc",
    connection_options={
        "path": "s3://s3path"
    }
)
```

スクリプト (`pyspark.sql.DataFrame`) では DataFrame を使用することもできます。

```
df.write.orc("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="orc"
    ).writeDynamicFrame(dynamicFrame)
  }
}
```

スクリプト (`pyspark.sql.DataFrame`) では DataFrame を使用することもできます。

```
df.write.orc("s3://s3path/")
```

------

## ORC 設定リファレンス
<a name="aws-glue-programming-etl-format-orc-reference"></a>

`format="orc"` の `format_options` 値はありません。ただし、基になる SparkSQL コードで受け入れられるオプションは、`connection_options` マップパラメータを介して渡すことができます。