0
votes

I have a table in Amazon Arora Postgres. I need to move that table to S3 bucket in csv format. I have create the following pyspark code in AWS glue. Instead of storing as a csv file in S3 bucket. Multiple files are created in S3 bucket like run-XXX-part1. Is there a way to export a rds table into csv file in S3. Code: import sys from awsglue.transforms import * from awsglue.utils import getResolvedOptions from pyspark.context import SparkContext from awsglue.context import GlueContext from awsglue.job import Job

## @params: [JOB_NAME]
args = getResolvedOptions(sys.argv, ['JOB_NAME'])

sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)
## @type: DataSource
## @args: [database = "test1", table_name = "testdb_public_reports3", transformation_ctx = "datasource0"]
## @return: datasource0
## @inputs: []
## @type: ApplyMapping
## @args: [mapping = [("orderapprovedby", "string", "orderapprovedby", "string"), ("lname", "string", "lname", "string"), ("unitofmeasurement", "string", "unitofmeasurement", "string"), ("orderrequesteddtm", "timestamp", "orderrequesteddtm", "timestamp"), ("orderdeliverydtm", "timestamp", "orderdeliverydtm", "timestamp"), ("allowedqty", "decimal(10,2)", "allowedqty", "decimal(10,2)"), ("addressid", "int", "addressid", "int"), ("fname", "string", "fname", "string")], transformation_ctx = "applymapping1"]
## @return: applymapping1
## @inputs: [frame = datasource0]
applymapping1 = ApplyMapping.apply(frame = datasource0, mappings = [("mname", "string", "mname", "string"), ("lname", "string", "lname", "string"), ("designation", "string", "designation", "string"), ("joiningtime", "timestamp", "joiningtime", "timestamp"), ("leavingtime", "timestamp", "orderdeliverydtm", "leavingtime"),("fname", "string", "fname", "string")], transformation_ctx = "applymapping1")
## @type: DataSink
## @args: [connection_type = "s3", connection_options = {"path": "s3://deloitte-homefront-poc/PROCESSED"}, format = "csv", transformation_ctx = "datasink2"]
## @return: datasink2
## @inputs: [frame = applymapping1]
datasink2 = glueContext.write_dynamic_frame.from_options(frame = applymapping1, connection_type = "s3", connection_options = {"path": "s3://path"}, format = "csv", transformation_ctx = "datasink2")
job.commit()
1

1 Answers

1
votes

Using glue and pyspark just for exporting data is not a good option. You can follow the step by step guide provided by aws https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/postgresql-s3-export.html

Of you still want to use Glue and want single output file

#replace
datasink2 = glueContext.write_dynamic_frame.from_options(frame = applymapping1, connection_type = "s3", connection_options = {"path": "s3://path"}, format = "csv", transformation_ctx = "datasink2")

#with
df=applymapping1.toDF()
df.repartition(1).write.csv(path)