- Newest
- Most votes
- Most comments
To initialize/update a table and create transformed files in the same PySpark Glue job without creating duplicate files, you have a few options:
Option 1: Using getSink with enableUpdateCatalog
This approach allows you to write data and update the catalog in one step:
sink = glueContext.getSink( connection_type="s3", path="s3://your-bucket/path/", enableUpdateCatalog=True, updateBehavior="UPDATE_IN_DATABASE", partitionKeys=["your_partition_key"] ) sink.setFormat("parquet") # or your preferred format sink.setCatalogInfo( catalogDatabase="your_database", catalogTableName="your_table" ) sink.writeFrame(your_dynamic_frame)
Option 2: Using job bookmarks to prevent duplicate processing
To prevent duplicate files when processing the same partition multiple times, properly configure job bookmarks:
# Initialize job with bookmarks job = Job(glueContext) job.init(args['JOB_NAME'], args) # Read with transformation context datasource = glueContext.create_dynamic_frame.from_catalog( database="your_database", table_name="your_table", transformation_ctx="unique_identifier" ) # Your transformations here transformed_frame = apply_transformations(datasource) # Write with the same transformation context glueContext.write_dynamic_frame.from_options( frame=transformed_frame, connection_type="s3", connection_options={ "path": "s3://your-bucket/output-path/", "partitionKeys": ["your_partition_key"] }, format="parquet", transformation_ctx="unique_identifier" ) # Commit the job to save bookmark state job.commit()
Important considerations:
- When using job bookmarks, make sure to:
- Use DynamicFrame API (not DataFrame or Spark SQL) for reading data
- Include a unique transformation_ctx parameter for each ETL operation
- Don't change the transformation_ctx between job runs
- Set the job's maximum concurrent runs to 1
-
For the getSink approach, you can specify updateBehavior as "UPDATE_IN_DATABASE" to update an existing table or create a new one if it doesn't exist.
-
If you're still seeing duplicate files, ensure you're properly configuring the job bookmark option in the Glue job properties.
By following these patterns, you should be able to both update your catalog and write transformed files in a single job without creating duplicates.
Sources
Updating the schema, and adding new partitions in the Data Catalog using AWS Glue ETL jobs - AWS Glue
Troubleshoot AWS Glue job bookmarks | AWS re:Post
Relevant content
asked 3 years ago
- AWS OFFICIALUpdated 2 years ago
