1

I'm trying to estimate proportion of maths marks in pyspark 3.0.1 on data bricks. Some of the cases my total_marks are 0.So I wrote following code

 df_data.registerTempTable('myTable')
 df_oversees2=SQLContext.sql("select A.*,case when total_marks=0 then 0 else (maths_marks/total_marks) end as prop_maths from myTable A ")

But I'm getting error message

TypeError: sql() missing 1 required positional argument: 'sqlQuery'

Can you please help me to resolve the issue? I also verified that my total_marks & maths_marks are present in my table using below code

df1 = sqlContext.sql("select maths_marks,total_marks from myTable")
df1.show()

I'm seeing the output for both the fields

1 Answer 1

5

SQLContext is different from sqlContext - the latter is an SQLContext attached to a SparkContext, while the former is a class not attached to any SparkContext. Therefore, you need to do:

sqlContext = SQLContext(sc)
df_oversees2 = sqlContext.sql("your query")

In any case, it is deprecated and the better way would be to call

spark.sql("your query")

where spark is a sparkSession object.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm getting parse exception with spark.sql
I have resolved the issue. By mistake there was a "#" given. Removed that & now it's running

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.