cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Using WorkspaceClient -- run a saved query

lprevost
Contributor II

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"})

 

3 REPLIES 3

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.

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!

Sign Up Now