cancel
Showing results for 
Search instead for 
Did you mean: 
Get Started Discussions
Start your journey with Databricks by joining discussions on getting started guides, tutorials, and introductory topics. Connect with beginners and experts alike to kickstart your Databricks experience.
cancel
Showing results for 
Search instead for 
Did you mean: 

Access Databricks Table with Simple Python3 Script

redapplesonly
New Contributor II

Hi, I'm super new to Databricks. I'm trying to do a little API scripting against my company's DB instance.
I have this supersimple python (ver 3) which is meant to run a remote host.  The script tries to a simple SQL query against my Databricks instance:

import requests
import json

HOST = 'https://**********************************'
TOKEN = '**************************'
endpoint = f'{HOST}/api/2.0/sql/queries'

headers = {
    'Authorization': f'Bearer {TOKEN}',
    'Content-Type': 'application/json'
}

data = {
    "query": "SELECT * FROM table1 LIMIT 10",
    "data_source": {
        "catalog_name": "hive_metastore",
        "schema_name": "default"
    },
    "output_format": "json"
}

response = requests.post(endpoint, headers=headers, data=json.dumps(data))

if response.status_code == 200:
    query_result = response.json()
    print("Query executed successfully. Results:")
    print(query_result)
else:
    print(f"Failed to execute query. Status code: {response.status_code}, Message: {response.text}")

What could be simpler? "Give me the first ten rows of 'table1'."
(I have verified that the URL and token are valid.)
The problem is that when I run the program, Databricks doesn't like the formatting of the `data` JSON dictionary. Here's the error I see when the script runs:

Failed to execute query. Status code: 400, Message: {"error_code":"MALFORMED_REQUEST","message":"Could not parse request object: Expected 'START_OBJECT' not 'VALUE_STRING'\n at [Source: (ByteArrayInputStream); line: 1, column: 11]\n at [Source: java.io.ByteArrayInputStream@1cb8bdf; line: 1, column: 11]"}

I don't really understand what this error message means, but the AI chatbot I'm using assures me that Databricks is gagging on the format of my JSON `data` payload. "Consult the Databricks documentation" is its best advice.

So I guess I was hoping for some general advice. Is my approach naive? If so, could someone recommend an example program I could consult? thank you.

1 ACCEPTED SOLUTION

Accepted Solutions

gchandra
Databricks Employee
Databricks Employee
2 REPLIES 2

gchandra
Databricks Employee
Databricks Employee

The request sample looks different. 

https://docs.databricks.com/api/workspace/queries/create



~

redapplesonly
New Contributor II

@gchandra Yes!  This is the documentation I was seeking!  Thank you so much

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group