本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
DynamicFrameWriter 類別
方法
__init__
__init__(glue_context)
glue_context– 所要使用的 GlueContext 類別。
from_options
from_options(frame, connection_type, connection_options={},
format=None, format_options={}, transformation_ctx="")
使用指定的連線和格式來撰寫 DynamicFrame。
frame– 所要撰寫的DynamicFrame。connection_type– 連線類型。有效值包括s3、mysql、postgresql、redshift、sqlserver及oracle。connection_options– 連線選項,例如路徑和資料庫資料表 (選用)。如果是connection_type的s3,會定義 Amazon S3 路徑。connection_options = {"path": "s3://aws-glue-target/temp"}如果是 JDBC 連線,必須定義幾項屬性。請注意,資料庫名稱必須是 URL 的一部分。它可以選擇性包含在連線選項中。
警告
不建議在指令碼中存放密碼。考慮使用 從 AWS Secrets Manager 或 Glue Data Catalog AWS
boto3擷取它們。connection_options = {"url": "jdbc-url/database", "user": "username", "password":passwordVariable,"dbtable": "table-name", "redshiftTmpDir": "s3-tempdir-path"}dbtable屬性為 JDBC 資料表的名稱。若是支援資料庫內結構描述的 JDBC 資料存放區,請指定schema.table-name。如果未提供結構描述,則會使用預設的 "public" 結構描述。如需詳細資訊,請參閱AWS Glue for Spark 中 ETL 的連線類型和選項。
format– 格式化規格 (選用)。這用於 Amazon Simple Storage Service (Amazon S3) 或支援多種格式的 AWS Glue 連線。請參閱 AWS Glue for Spark 中的輸入與輸出的資料格式選項 以了解受支援的格式。format_options– 指定格式的格式選項。請參閱 AWS Glue for Spark 中的輸入與輸出的資料格式選項 以了解受支援的格式。transformation_ctx– 所要使用的轉換細節 (選用)。
from_catalog
from_catalog(frame, name_space, table_name, redshift_tmp_dir="", transformation_ctx="")
使用指定的目錄資料庫和資料表名稱,來撰寫 DynamicFrame。
frame– 所要撰寫的DynamicFrame。name_space– 所要使用的資料庫。table_name– 所要使用的table_name。redshift_tmp_dir– 所要使用的 Amazon Redshift 暫時目錄 (選用)。transformation_ctx– 所要使用的轉換細節 (選用)。-
additional_options– 提供給 AWS Glue 的額外選項。若要寫入受 Lake Formation 管控的資料表,您可以使用下列其他選項:
-
transactionId– (字串) 要寫入受管控資料表的交易 ID。此交易不能已遞交或中止,否則寫入將失敗。 -
callDeleteObjectsOnCancel– (布林值,選用) 如果設定為true(預設值),則 AWS Glue 會在物件寫入至 Amazon S3 之後自動呼叫DeleteObjectsOnCancelAPI。如需詳細資訊,請參閱《AWS Lake Formation 開發人員指南》中的 DeleteObjectsOnCancel。
範例:寫入 Lake Formation 中的受管控資料表
txId = glueContext.start_transaction(read_only=False) glueContext.write_dynamic_frame.from_catalog( frame=dyf, database = db, table_name = tbl, transformation_ctx = "datasource0", additional_options={"transactionId":txId}) ... glueContext.commit_transaction(txId) -
from_jdbc_conf
from_jdbc_conf(frame, catalog_connection, connection_options={}, redshift_tmp_dir = "", transformation_ctx="")
使用指定的 JDBC 連線資訊來撰寫 DynamicFrame。
frame– 所要撰寫的DynamicFrame。catalog_connection– 所要使用的目錄連線。connection_options– 連線選項,例如路徑和資料庫資料表 (選用)。redshift_tmp_dir– 所要使用的 Amazon Redshift 暫時目錄 (選用)。transformation_ctx– 所要使用的轉換細節 (選用)。
write_dynamic_frame 的範例
這個範例使用 S3 的 connection_type 和 connection_options 中的 POSIX 路徑參數在本機寫入輸出,這允許寫入本機儲存。
glueContext.write_dynamic_frame.from_options(\ frame = dyf_splitFields,\ connection_options = {'path': '/home/glue/GlueLocalOutput/'},\ connection_type = 's3',\ format = 'json')