Skip to content

Modified data through custom script is updated but when joining this data, old data is coming instead of updated data

0

I have written a custom script to transform a column, data transformed successfully and I can verify that in data preview in aws glue studio, acft_tail_num -> 8911(earlier this was N8911 ) when I joined the data with another table, I see N8911 instead of 8911. this is happening with other fields as well. attaching script for reference

`def MyTransform (glueContext, dfc) -> DynamicFrameCollection: import pyspark.sql.functions as F from awsglue.dynamicframe import DynamicFrame

# Step 1: Get first DynamicFrame
input_key = list(dfc.keys())[0]
dynamic_frame = dfc.select(input_key)
df = dynamic_frame.toDF()

# Step 2: Original transformations (unchanged sections)
df = df.withColumn(
    "FAULT_DESC_CLEAN",
    F.when(F.col("FAULT_DESC").startswith("<"), F.regexp_replace("FAULT_DESC", "(.*)br>", ""))
     .otherwise(F.col("FAULT_DESC"))
)

df = df.withColumn(
    "FAULT_NAME",
    F.when(
        F.col("FAULT_DESC_CLEAN").isNotNull() & (F.col("FAULT_DESC_CLEAN") != F.col("FAULT_NAME")),
        F.concat(F.col("FAULT_NAME"), F.lit(" "), F.col("FAULT_DESC_CLEAN"))
    ).otherwise(F.col("FAULT_NAME"))
)

df = df.withColumn(
    "FAULT_NAME",
    F.when(
        F.col("FAULT_NAME").substr(1, 7) == F.col("LOG_PAGE_NBR"),
        F.regexp_replace(F.concat(F.col("FAULT_NAME"), F.col("FAULT_DESC_CLEAN")), F.col("LOG_PAGE_NBR"), "")
    ).otherwise(F.concat(F.col("FAULT_NAME"), F.col("FAULT_DESC_CLEAN")))
)

df = df.withColumn(
    "FACT",
    F.when(F.col("CORR_ACTION_EXTSN").isNotNull(),
           F.concat(F.col("FACT"), F.lit(" "), F.col("CORR_ACTION_EXTSN")))
     .otherwise(F.col("FACT"))
)

conditions = [
    (F.col("WO_DESC").contains("30Y"), "30Y"),
    (F.col("WO_DESC").contains("28Y"), "28Y"),
    (F.col("WO_DESC").contains("27Y"), "27Y"),
    (F.col("WO_DESC").contains("26Y"), "26Y"),
    (F.col("WO_DESC").contains("24Y"), "24Y"),
    (F.col("WO_DESC").contains("22Y"), "22Y"),
    (F.col("WO_DESC").contains("21Y"), "21Y"),
    (F.col("WO_DESC").contains("20Y"), "20Y"),
    (F.col("WO_DESC").contains("18Y"), "18Y"),
    (F.col("WO_DESC").contains("16Y"), "16Y"),
    (F.col("WO_DESC").contains("15Y"), "15Y"),
    (F.col("WO_DESC").contains("14Y"), "14Y"),
    (F.col("WO_DESC").contains("12Y"), "12Y"),
    (F.col("WO_DESC").contains("10Y"), "10Y"),
    (F.col("WO_DESC").contains("9Y"), "9Y"),
    (F.col("WO_DESC").contains("8Y"), "8Y"),
    (F.col("WO_DESC").contains("6Y"), "6Y"),
    (F.col("WO_DESC").contains("HRON"), "HRON"),
    (F.col("WO_DESC").contains("IM") & F.col("WO_DESC").contains("HC"), "IM-HC"),
    (F.col("WO_DESC").contains("PAINT"), "PAINT"),
    (F.col("WO_DESC").contains("CABIN VISIT"), "CABIN VISIT"),
    (F.col("WO_DESC").contains("PSEW"), "PSEW"),
]

check_type_col = F.lit("")
for cond, result in conditions:
    check_type_col = F.when(cond, result).otherwise(check_type_col)

df = df.withColumn("CHECK_TYPE", check_type_col)

df = df.withColumn(
    "disc_type_log",
    F.when(
        (F.col("ROW_SOURCE_CD") == 'MTX') &
        ((F.col("FAULT_SOURCE_TYPE") == 'PILOT') | (F.col("FAULT_SOURCE_TYPE") == "LOGPAGE")),
        1
    )
    .when(
        (F.col("ROW_SOURCE_CD") == 'MTX') &
        (F.col("FAULT_SOURCE_TYPE") == 'MECH') &
        ((F.col("LOG_PAGE_NBR").substr(0, 1).between("0", "9")) |
        (F.col("FAULT_TYPE_CD").isin("MEL", "CDL")) |
        F.col("CHECK_TYPE").isNull()), 1
    ).otherwise(0)
)

df = df.withColumn(
    "disc_type_formula",
    F.when(F.col("disc_type_log") == 1, F.col("FAULT_SOURCE_TYPE"))
     .when((F.col("disc_type_log") == 0) & (F.col("FAULT_SOURCE_TYPE") == "MECH"), "NRC")
     .otherwise(F.col("FAULT_SOURCE_TYPE"))
)

df = df.withColumn("unq_id", F.col("FAULT_BARCODE_NUM"))

# Create discrepancy
df = df.withColumn(
    "discrepancy",
    F.concat(F.col("FAULT_NAME"), F.lit(" "), F.col("FAULT_DESC_CLEAN"))
)

# ------------------------------------------------------------------------
# ✅ ADDITIONAL OPERATIONS AFTER DISCREPANCY CREATION
# ------------------------------------------------------------------------

# 1) REGEX_Replace([acft_tail_num], "[^\\d]", "")
df = df.withColumn("acft_tail_num", F.regexp_replace("acft_tail_num", "[^\\d]", ""))

# 2) ATA transformation logic
df = df.withColumn(
    "ata",
    F.when(F.locate(" ", F.col("ata")) == 2,
           F.concat(F.substring(F.col("ata"), 1, 2), F.lit("00")))
     .when(F.locate(" ", F.col("ata")) == 5,
           F.concat(F.substring(F.col("ata"), 1, 2), F.substring(F.col("ata"), 3, 2)))
     .otherwise(F.lit("INV"))
)

# 3) Add a new column Source = "MTX"
df = df.withColumn("Source", F.lit("MTX"))

# 4) resolved_loc_cd = left([resolved_loc_cd],3)
df = df.withColumn("resolved_loc_cd", F.substring(F.col("resolved_loc_cd"), 1, 3))

# 5) REGEX_Replace([DISCREPANCY], "[^ -~]", "$1")
df = df.withColumn("discrepancy", F.regexp_replace("discrepancy", "[^ -~]", "$1"))

# 6) Regex_Replace([DISCREPANCY], "\r*\n*", "")
df = df.withColumn("discrepancy", F.regexp_replace("discrepancy", "\r*\n*", ""))

# 7) Regex_Replace([corr_action], "<br>", " ")
df = df.withColumn("FACT", F.regexp_replace("FACT", "<br>", " "))

# 8) Regex_Replace([FACT], "\r*\n*", "")
df = df.withColumn("FACT", F.regexp_replace("FACT", "\r*\n*", ""))

# 9) Regex_Replace([FACT], "[^ -~]", "$1")
df = df.withColumn("FACT", F.regexp_replace("FACT", "[^ -~]", "$1"))

# ------------------------------------------------------------------------

# Step 3: Convert back to DynamicFrame
result_dynamic_frame = DynamicFrame.fromDF(df, glueContext, "result_dynamic_frame")

# Step 4: Return the result
return DynamicFrameCollection({"CustomTransform0": result_dynamic_frame}, glueContext)
`

asked 9 months ago108 views

2 Answers
0

This issue is likely related to how data is being cached or processed during the join operation in AWS Glue. When you join data after making transformations, the join might be using a cached version of the data rather than the updated version.

There are a few potential causes and solutions:

  1. Caching issue: Spark might be caching the original data frame before your transformations. When you join, it's using the cached version instead of the transformed one.

  2. Join order: The join operation might be occurring before your transformation is fully applied, especially if you're using multiple operations in sequence.

  3. Data validation: The transformation is working correctly in the preview, but the join might be using a different reference to the data.

To resolve this issue, try these approaches:

  1. Force cache invalidation: After your transformation, add a .cache() call on your transformed DataFrame and then call .count() to materialize it before joining.

  2. Explicitly persist the transformation: Use .persist() on your transformed DataFrame before joining.

  3. Check join keys: Ensure that the join keys are properly specified and that they match between the two tables.

  4. Verify transformation timing: Make sure your transformation (especially the acft_tail_num regex replacement) is completed before the join operation.

  5. Check data types: Ensure that the data types of the join columns match between the two tables.

In your specific case, the acft_tail_num column is being transformed from "N8911" to "8911" using regex replacement, but when joining, the original value "N8911" appears. This suggests that either the transformation isn't being applied before the join or the join is using a different reference to the data than what you're seeing in the preview.
Sources
CDK Version Upgrade Causing DynamoDB Table Replacement and Unreferenced Historical Data | AWS re:Post
Optimizing data lakes with Amazon S3 Tables and Apache Spark on Amazon EKS | Containers
AWS Glue Scala DynamicFrame class - AWS Glue

answered 9 months ago

0

In order to verify the transformations are taking effect as intended, print a sample of the transformed data before and after performing the join. Add the following line inside your script : df.show()

First, add .show() immediately after you complete all your transformations on the DataFrame/DynamicFrame that will be then joined with the another table. This will display the transformed records directly in your AWS Glue job output logs, allowing you to verify that columns like "acft_tail_num" and any other modified fields are being updated as expected. This is your first checkpoint to confirm the transformation logic is functioning properly.

Second, add another .show() call on the resulting DataFrame/DynamicFrame after you perform your join operation. This will help you verify that the join is producing the intended results and that data from both DataFrame/DynamicFrame is being combined correctly.

This validation approach helps ensure transformations are reflected properly before and after joining, making it easier to identify if issues lie in the transformation logic or data flow between nodes.

In case you are unable to identify the root cause or you encounter this issue only with Glue Visual ETL, I would request you to reach out to AWS Support team since they will be able to deep dive into the logs and help you troubleshoot the root cause of the issue you are experiencing.

References: https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrame.show.html

AWS

answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.