AWS Glue 建立關聯轉換之後,如何使用樞紐資料?

2 分的閱讀內容
0

我想使用 AWS Glue 關聯轉換來扁平化資料。我可以使用哪些欄位作為分割區,以在 Amazon Simple Storage Service (Amazon S3) 中儲存樞紐資料?

簡短描述

關聯轉換可讓您在關聯式資料庫中使用 NoSQL 資料結構,例如數組和結構。關聯轉換傳回 DynamicFrames(Python 中的 DynamicFrameCollection 和 Scala 中的數組)的集合。關聯轉換傳回的所有 DynamicFrames 可透過它們在 Python 中的個人名稱和 Scala 中的數組索引進行存取。

解決方法

關聯資料

本教學課程使用下列架構:

|-- family_name: string
|-- name: string
|-- gender: string
|-- image: string
|-- images: array
|    |-- element: struct
|    |    |-- url: string

在 Python 中使用以下關係語法:

# AWS Glue Data Catalog: database and table names
db_name = "us-legislators"
tempDir = "s3://awsexamplebucket/temp_dir/"

# Create dynamic frames from the source tables
persons = glueContext.create_dynamic_frame.from_catalog(database=db_name, table_name=tbl_persons)

# Relationalize transformation
dfc = persons.relationalize("root", tempDir)
dfc.select('root_images').printSchema()
dfc.select('root_images').show()

在 Scala 中使用以下關係語法:

// AWS Glue Data Catalog: database and table names
val dbName = "us-legislators"
val tblPersons = "persons_json"

// Output Amazon S3 temp directory
val tempDir = "s3://awsexamplebucket/temp_dir"

val persons: DynamicFrame = glueContext.getCatalogSource(database = dbName, tableName = tblPersons).getDynamicFrame()
val personRelationalize = persons.relationalize(rootTableName = "root", stagingPath = tempDir)
personRelationalize(2).printSchema()
personRelationalize(2).show()

解譯樞紐分析資料

這種關聯轉換會產生兩個架構:rootroot_images

root

|-- family_name: string
|-- name: string
|-- gender: string
|-- image: string
|-- images: long

root_images

|-- id: long
|-- index: int
|-- images.val.url: string
  • id:數組元素的順序(1、2 或 3)
  • index:數組中每個元素的索引位置
  • images.val.urlroot_images 中的 images.val.url

這些是唯一可用作分割區欄位的欄位,可儲存 Amazon S3 中的樞紐資料。由於 root_images 中不存在這些欄位,因此指定 root 資料表欄位不起作用,例如 name

加入關聯式資料以獲取標準化資料

root_images 中的 id 屬性是數組(1、2 或 3)在資料集中的順序。root 中的 images 屬性具有數組索引的值。這意味著您必須使用 imagesid 加入 rootroot_images。您可以執行 dynamicFrame.show() 以驗證數組順序和數組索引值。

若要加入 rootroot_images

Python:

joined_root_root_images = Join.apply(dfc.select('root'), dfc.select('root_images'), 'images', 'id')

Scala:

val joined_root_root_images = personRelationalize(0).join(keys1 = Seq("images"), keys2 = Seq("id"), frame2 = personRelationalize(1))

儲存樞紐資料

若要將樞紐資料與分割區一起儲存在 Amazon S3 中:

Python:

datasink4 = glueContext.write_dynamic_frame.from_options(frame = dfc.select('root_images'), connection_type = "s3", connection_options = {"path": outputHistoryDir,"partitionKeys":["id"]}, format = "csv",transformation_ctx = "datasink4")

Scala:

**注意:**在下列範例中,personRelationalize(2)root_images 樞紐資料表。

glueContext.getSinkWithFormat(connectionType = "s3",
  options = JsonOptions(Map("path" -> paths, "partitionKeys" -> List("id"))),
  format = "csv", transformationContext = "").writeDynamicFrame(personRelationalize(2))

若要在沒有分割區的情況下將樞紐資料儲存在 Amazon S3 中:

Python:

datasink5 = glueContext.write_dynamic_frame.from_options(frame = dfc.select('root_images'), connection_type = "s3", connection_options = {"path": outputHistoryDir}, format = "csv",transformation_ctx = "datasink5"

Scala:

**注意:**在下列範例中,personRelationalize(2)root_images 樞紐資料表。

glueContext.getSinkWithFormat(connectionType = "s3",
  options = JsonOptions(Map("path" -> paths)),
  format = "csv", transformationContext = "").writeDynamicFrame(personRelationalize(2))

將資料寫入 Amazon S3 之後,請查詢 Amazon Athena 中的資料,或使用 DynamicFrame 將資料寫入關聯式資料庫,例如 Amazon Redshift。


相關資訊

使用 AWS Glue 關聯轉換簡化查詢巢狀 JSON

程式碼範例: 加入和關聯資料

AWS 官方
AWS 官方已更新 3 年前