I have following Spark sql and I want to pass variable to it. How to do that? I tried following way.
sqlContext.sql("SELECT count from mytable WHERE id=$id")
On
You could use a concatenation, with this the engine understands the query, I leave an example:
First:
In a variable inserts the value to pass in the query (in this case is a date)
date= spark.range(1).withColumn('date',regexp_replace(date_add(current_date(),-4),"-","")).toPandas().to_string().split()[4]
Result = '20220206'
Second:
query = '''
SELECT
*
FROM
table
WHERE
country = '''+' '+date+'''
'''
df= spark.sql(query)
You are almost there just missed
s:)