Using WorkspaceClient -- run a saved query

lprevost
Contributor III

I've saved a query on my sql warehouse which has a parameter called :list_parameter.   

I've found my query id as follows:

 

 

from databricks.sdk import WorkspaceClient
w = WorkspaceClient()
for query in w.queries.list():
    print(f"query: {query.display_name}, paramaters: {query.parameters}")

 

 

What i now can't seem to find out is how to run and wait for the query response on my client.

Something like w.queries.run(id = "xyz", parameters = {'list_parameter": "my, excellent, list"})

 

koji_kawamura
Databricks Employee
Databricks Employee

Hi @lprevost 

The WorkspaceClient provides APIs to manage Query objects. But it doesn't provide the API to run it. If you need to run the query from a notebook, you can pass the query text into `spark.sql`. It returns SparkDataFrame. I hope this helps!

 

query = w.queries.get("query-uuid")
df = spark.sql(query.query_text, {"param_name": "param_value"})
display(df)

Was hoping to run a remote query from a WorkspaceClient and get a result.   

Thanks for describing the detail. Now I understand your motivation better.
In that case, the execute_statement API can be used. Please take a look and let me know if it suits your need.