Apache spark 如何在AWS Glue PySpark中运行并行线程?
我有一个spark工作,它只需要使用相同的转换从多个表中提取数据。基本上是一个for循环,它遍历一个表列表,查询目录表,添加时间戳,然后执行红移(下面的示例) 完成这项工作大约需要30分钟。有没有办法在相同的spark/glue环境下并行运行这些程序?如果可以避免的话,我不想创建单独的胶水作业Apache spark 如何在AWS Glue PySpark中运行并行线程?,apache-spark,pyspark,aws-glue,aws-glue-spark,Apache Spark,Pyspark,Aws Glue,Aws Glue Spark,我有一个spark工作,它只需要使用相同的转换从多个表中提取数据。基本上是一个for循环,它遍历一个表列表,查询目录表,添加时间戳,然后执行红移(下面的示例) 完成这项工作大约需要30分钟。有没有办法在相同的spark/glue环境下并行运行这些程序?如果可以避免的话,我不想创建单独的胶水作业 import datetime import os from awsglue.transforms import * from awsglue.utils import getResolvedOption
import datetime
import os
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from awsglue.dynamicframe import DynamicFrame
from awsglue.context import GlueContext
from awsglue.job import Job
from pyspark.context import SparkContext
from pyspark.sql.functions import *
# query the runtime arguments
args = getResolvedOptions(
sys.argv,
["JOB_NAME", "redshift_catalog_connection", "target_database", "target_schema"],
)
# build the job session and context
glueContext = GlueContext(SparkContext.getOrCreate())
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args["JOB_NAME"], args)
# set the job execution timestamp
job_execution_timestamp = datetime.datetime.utcnow()
tables = []
for table in tables:
catalog_table = glueContext.create_dynamic_frame.from_catalog(
database="test", table_name=table, transformation_ctx=table
)
data_set = catalog_table.toDF().withColumn(
"batchLoadTimestamp", lit(job_execution_timestamp)
)
# covert back to glue dynamic frame
export_frame = DynamicFrame.fromDF(data_set, glueContext, "export_frame")
# remove null rows from dynamic frame
non_null_records = DropNullFields.apply(
frame=export_frame, transformation_ctx="non_null_records"
)
temp_dir = os.path.join(args["TempDir"], redshift_table_name)
stores_redshiftSink = glueContext.write_dynamic_frame.from_jdbc_conf(
frame=non_null_records,
catalog_connection=args["redshift_catalog_connection"],
connection_options={
"dbtable": f"{args['target_schema']}.{redshift_table_name}",
"database": args["target_database"],
"preactions": f"truncate table {args['target_schema']}.{redshift_table_name};",
},
redshift_tmp_dir=temp_dir,
transformation_ctx="stores_redshiftSink",
) ```
您可以执行以下操作以加快此过程
因为spark redshift jdbc连接器首先将spark数据帧卸载到s3,然后准备一个复制命令到redshift表。在直接运行copy命令的同时,您消除了运行unload命令以及将数据读入spark df的开销。这两种方法对于我的用例都非常有效。非常感谢。