1

Can someone let me know what the equivalent of the following CREATE VIEW in Databricks SQL is in PySpark?

CREATE OR REPLACE VIEW myview
as 
select last_day(add_months(current_date(),-1))

Can someone let me know the equivalent of the above is in PySpark? Basically, I need to create a dataframe from the above Databricks SQL code, but it needs to written in PySpark

Alex Ott
  • 80,552
  • 8
  • 87
  • 132
Patterson
  • 1,927
  • 1
  • 19
  • 56

1 Answers1

1

The syntax is df.createOrReplaceTempView for creating temp views (df is the dataframe object) but if you want to generate the view from the sql syntax. This is one way to do it:

sql("select last_day(add_months(current_date(),-1)) as last_date").createOrReplaceTempView("myview")

sql("select * from myview").show()

PS: pyspark has no direct methods for creating persistent views and you might have to use sql for this

sql("""CREATE OR REPLACE VIEW myview
as 
select last_day(add_months(current_date(),-1)) as column_name""")
sql("select * from myview").show()
ClumsyPuffin
  • 3,909
  • 1
  • 17
  • 17