Issue with filter_by in Databricks SQL Query History API (/api/2.0/sql/history/queries)

lukamu
New Contributor II

Hi everyone,

I'm trying to use the filter_by parameter in a GET request to /api/2.0/sql/history/queries, but I keep getting a 400 Bad Request error. When I use max_results, it works fine, but adding filter_by causes the request to fail.

Example value for filter_by in the request:

 

{
   "statuses":["FINISHED","CANCELED"],
   "query_start_time_range":{
       "start_time_ms" : 1640995200000, 
       "end_time_ms" : 1641081599000
   }
}

 

Response

 

{
    "error_code": "MALFORMED_REQUEST",
    "message": "Could not parse request object: Expected 'START_OBJECT' not 'VALUE_STRING'\n at [Source: (ByteArrayInputStream); line: 1, column: 15]\n at [Source: java.io.ByteArrayInputStream@28488ac8; line: 1, column: 15]"
}

 

I've tried several variations, but I keep getting the same error. Unfortunately, the documentation does not include an example of how to format filter_by correctly in the query string.

Has anyone successfully used filter_by in this API? If so, could you share a working example?

Thanks!

Alberto_Umana
Databricks Employee
Databricks Employee

Here's how you can do it.

 

import requests

import json

api_endpoint = "https://test-1.cloud.databricks.com/api/2.0/sql/history/queries"

 

headers = {

    "Authorization": "Bearer XXXXXX”

}

 

params = json.dumps({

    "filter_by": {

        "user_id": "5342291138747455",  # Example filter: queries executed by a specific user

        "query_start_time_range": {

            "start_time_ms": 1640995200000,  # Example start time in milliseconds

            "end_time_ms": 1641081599000    # Example end time in milliseconds

        }

    }

})

response = requests.get(api_endpoint, headers=headers, params=params)

 

# Parse the response

data = response.json()

print(json.dumps(data, indent=4))

View solution in original post

Hi Alberto,

I just wanted to thank you for your suggestion. Thanks to your input, I was able to successfully use the API with filters. I really appreciate your help!

Alberto_Umana
Databricks Employee
Databricks Employee

Hi @lukamu, glad that it worked for you!