Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to use nvl in pyspark for variable?

i have a scenario like getting the maximum date from the table in hive and if the table is empty then max(column) will be null/none in that scenario replace that with "2022-01-01"

i am getting the max from

df1 = spark.table()
max_date_to=df1.agg(max(col("date"))).collect()[0].asDict()[‘max(date)’]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

but my table is empty so i what to get the value as "2022-01-01"

>Solution :

You could simply use if else in this scenario,

df1 = spark.table()
check_date = df1.agg(max(col("date"))).collect()[0].asDict()['max(date)']
# Check if the check_date variable is not null if yes then use as it is else use the other date.
max_date_to = check_date if check_date else "2022-01-01"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading