简体   繁体   中英

AWS Glue : Filter date field

I have a very basic question, I create a AWS Glue job and I need to create a filter while extracting data from a dynamodb table. I need extract data from previous day only using a field named "time".

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
import re

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 = "sampledb", table_name = "tablex", transformation_ctx = "DataSource0")

Transform1 = ApplyMapping.apply(frame = DataSource0, mappings = [("allresults.paymentCapabilityResult.paymentCapabilityCheckResult", "boolean", "allresults.paymentCapabilityResult.paymentCapabilityCheckResult", "boolean"), ("time", "string", "time", "timestamp")], transformation_ctx = "Transform1")

DataSink0 = glueContext.write_dynamic_frame.from_options(frame = Transform1, connection_type = "s3", format = "json", connection_options = {"path": "s3://xxxxx/output/", "partitionKeys": []}, transformation_ctx = "DataSink0")
job.commit()

A filter for yesterday can be easily done in PySpark, as discussed here . A script would look like this:

from pyspark.sql import functions as F

df = Transform1.toDF()
df = df.where(F.col("time") == F.date_sub(F.current_date(), 1))
Transform2 = DynamicFrame.fromDF(df, glue_ctx=glueContext, name="df")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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