简体   繁体   English

在AWS Glue pySpark脚本中使用SQL

[英]use SQL inside AWS Glue pySpark script

I want to use AWS Glue to convert some csv data to orc. 我想使用AWS Glue将一些csv数据转换为orc。
The ETL job I created generated the following PySpark script: 我创建的ETL作业生成了以下PySpark脚本:

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

args = getResolvedOptions(sys.argv, ['JOB_NAME'])

sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)

datasource0 = glueContext.create_dynamic_frame.from_catalog(database = "tests", table_name = "test_glue_csv", transformation_ctx = "datasource0")

applymapping1 = ApplyMapping.apply(frame = datasource0, mappings = [("id", "int", "id", "int"), ("val", "string", "val", "string")], transformation_ctx = "applymapping1")

resolvechoice2 = ResolveChoice.apply(frame = applymapping1, choice = "make_struct", transformation_ctx = "resolvechoice2")

dropnullfields3 = DropNullFields.apply(frame = resolvechoice2, transformation_ctx = "dropnullfields3")

datasink4 = glueContext.write_dynamic_frame.from_options(frame = dropnullfields3, connection_type = "s3", connection_options = {"path": "s3://glue/output"}, format = "orc", transformation_ctx = "datasink4")
job.commit()

It takes the csv data (from the location of which the Athena table tests.test_glue_csv points to) and outputs to s3://glue/output/ . 它需要csv数据(从Athena表的test.test_glue_csv指向的位置)和输出到s3://glue/output/

How can I insert in this script some SQL manipulations? 如何在此脚本中插入一些SQL操作?

Thanks 谢谢

You should first create a temp view/table from your dynamic frame 您应该首先从动态框架创建临时视图/表

dyf.toDF().createOrReplaceTempView("view_dyf")

Here, dyf is your dynamic frame. 在这里, dyf是你的动态框架。

Then, use your spark object to apply sql queries on it 然后,使用spark对象对其应用sql查询

sqlDF = spark.sql("select * from view_dyf")
sqlDF.show()

你可以使用toDF()

df = datasource0.toDF() df.printSchema()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM