Skip to content

Instantly share code, notes, and snippets.

@pietheinstrengholt
Created December 29, 2021 15:40
Show Gist options
  • Select an option

  • Save pietheinstrengholt/bee6a0091365ef634a71ee19d7957467 to your computer and use it in GitHub Desktop.

Select an option

Save pietheinstrengholt/bee6a0091365ef634a71ee19d7957467 to your computer and use it in GitHub Desktop.
%%pyspark
from datetime import date
current_date = datetime.today().date()
try:
# Read original data - this is your scd type 2 table holding all data
dataOriginal = spark.read.load(dfDataOriginalPath + "/" + cw_database + "/" + cw_table, format='delta')
except:
# Use first load when no data exists yet
newOriginalData = dataChanged.withColumn('current', lit(True)).withColumn('effectiveDate', lit(current_date)).withColumn('endDate', lit(date(9999, 12, 31)))
newOriginalData.write.format("delta").mode("overwrite").save(dfDataOriginalPath + "/" + cw_database + "/" + cw_table)
newOriginalData.show()
newOriginalData.printSchema()
exit("Done loading data! Newly loaded data will be used to generate original data.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment