I am trying to execute a list of queries in Spark, but if the query does not run correctly, Spark throws me the following error: AnalysisException: "ALTER TABLE CHANGE COLUMN is not supported for changing ...
This is part of my code (i'm using python and Spark SQL on Databricks):
for index, row in df_tables.iterrows():
query = row["query"]
print ("Executing query: ")
try:
spark.sql(query)
print ("Query executed")
except (ValueError, RuntimeError, TypeError, NameError):
print("Unable to process your query dude!!")
else:
#do another thing
Is there any way to catch that exception? ValueError, RuntimeError, TypeError, NameError seems not working. There's no so much information about that in the Spark webpage.
Class AnalysisExceptionThrown when a query fails to analyze, usually because the query itself is invalid.
As of Spark 2.0, Spark SQL supports subqueries. A subquery (aka subquery expression) is a query that is nested inside of another query.
createorreplacetempview creates (or replaces if that view name already exists) a lazily evaluated "view" that you can then use like a hive table in Spark SQL. It does not persist to memory unless you cache the dataset that underpins the view. The data is cached fully only after the . count call.
I found AnalysisException defined in pyspark.sql.utils. https://spark.apache.org/docs/3.0.1/api/python/_modules/pyspark/sql/utils.html
import pyspark.sql.utils
try:
spark.sql(query)
print ("Query executed")
except pyspark.sql.utils.AnalysisException:
print("Unable to process your query dude!!")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With