Connect Python and pyodbc to Databricks

You can connect from your local Python code through ODBC to data in a Databricks cluster or SQL endpoint. To do this, you can use the open source Python code module pyodbc.

Follow these instructions to install, configure, and use pyodbc.

For more information about pyodbc, see the pyodbc Wiki.

Requirements

  • A local development machine running one of the following:
    • macOS
    • Windows
    • A Unix or Linux distribution that supports .rpm or .deb files
  • pip.
  • For Unix, Linux, or macOS, Homebrew.
  • A Databricks cluster, a Databricks SQL endpoint, or both. For more information, see Create a cluster and Create a SQL endpoint.

Follow the instructions for Unix, Linux, or macOS or for Windows.

Unix, Linux, or macOS

If your local Python code is running on a Unix, Linux, or macOS machine, follow these instructions.

Step 1: Install software

In this step, you download and install the Databricks ODBC driver, the unixodbc package, and the pyodbc module. (The pyodbc module requires the unixodbc package on Unix, Linux, and macOS.)

  1. Download the Databricks ODBC driver.
  2. To install the Databricks ODBC driver, open the SimbaSparkODBC.zip file that you downloaded.
  3. Do one of the following:
    • macOS: Double-click the extracted Simba Spark.dmg file. Then double-click the SimbaSparkODBC.pkg file that displays, and follow any on-screen directions.
    • Linux: Use your distribution’s package manager utility to install the extracted simbaspark.rpm or simbaspark.deb file, and follow any on-screen directions.
  4. Install the unixodbc package: from the terminal, run brew install unixodbc. For more information, see unixodbc on the Homebrew website.
  5. Install the pyodbc module: from the terminal, run pip install pyodbc. For more information, see pyodbc on the PyPI website and Install in the pyodbc Wiki.

Step 2: Configure software

Specify connection details for the Databricks cluster and SQL endpoint for pyodbc to use.

  1. Add the following content to the /etc/odbc.ini file on your machine:

    Tip

    If you do not want to or cannot use the /etc/odbc.ini file on your machine, you can specify connection details directly in Python code. To do this, skip the rest of this step and proceed to Step 3: Test your configuration.

    [ODBC Data Sources]
    Databricks_Cluster = Simba Spark ODBC Driver
    
    [Databricks_Cluster]
    Driver          = <driver-path>
    Description     = Simba Spark ODBC Driver DSN
    HOST            = <server-hostname>
    PORT            = 443
    Schema          = default
    SparkServerType = 3
    AuthMech        = 3
    UID             = token
    PWD             = <personal-access-token>
    ThriftTransport = 2
    SSL             = 1
    HTTPPath        = <http-path>
    

    In the preceding configuration file, replace the following placeholders, and then save the file:

    • Replace <driver-path> with one of the following:
      • macOS: /Library/simba/spark/lib/libsparkodbc_sbu.dylib
      • Linux 64-bit: /opt/simba/spark/lib/64/libsparkodbc_sb64.so
      • Linux 32-bit: /opt/simba/spark/lib/32/libsparkodbc_sb32.so
    • Replace <server-hostname> with the Server Hostname value from the Advanced Options > JDBC/ODBC tab for your cluster.
    • Replace <personal-access-token> with the value of your personal access token for your Databricks workspace.
    • Replace <http-path> with the HTTP Path value from the Advanced Options > JDBC/ODBC tab for your cluster.

    Tip

    To allow pyodbc to switch connections to a different cluster, add an entry to the [ODBC Data Sources] section and a matching entry below [Databricks_Cluster] with the specific connection details. Each entry must have a unique name within this file.

    [ODBC Data Sources]
    SQL_Endpoint = Simba Spark ODBC Driver
    
    [SQL_Endpoint]
    Driver          = <driver-path>
    HOST            = <server-hostname>
    PORT            = 443
    Schema          = default
    SparkServerType = 3
    AuthMech        = 3
    UID             = token
    PWD             = <personal-access-token>
    ThriftTransport = 2
    SSL             = 1
    HTTPPath        = <http-path>
    

    In the preceding configuration file, replace the following placeholders, and then save the file:

    • Replace <driver-path> with one of the following:
      • macOS: /Library/simba/spark/lib/libsparkodbc_sbu.dylib
      • Linux 64-bit: /opt/simba/spark/lib/64/libsparkodbc_sb64.so
      • Linux 32-bit: /opt/simba/spark/lib/32/libsparkodbc_sb32.so
    • Replace <server-hostname> with the Server Hostname value from the Connection Details tab for your SQL endpoint.
    • Replace <personal-access-token> with the value of your personal access token for your SQL endpoint.
    • Replace <http-path> with the HTTP Path value from the Connection Details tab for your SQL endpoint.

    Tip

    To allow pyodbc to switch connections to a different SQL endpoint, add an entry to the [ODBC Data Sources] section and a matching entry below [SQL_Endpoint] with the specific connection details. Each entry must have a unique name within this file.

  2. Add the preceding information you just added to the /etc/odbc.ini file to the corresponding /usr/local/etc/odbc.ini file on your machine as well.

  3. Add the following content to the /etc/odbcinst.ini file on your machine:

    [ODBC Drivers]
    Simba SQL Server ODBC Driver = Installed
    
    [Simba Spark ODBC Driver]
    Driver = <driver-path>
    

    In the preceding content, replace <driver-path> with one of the following values, and then save the file:

    • macOS: /Library/simba/spark/lib/libsparkodbc_sbu.dylib
    • Linux 64-bit: /opt/simba/spark/lib/64/libsparkodbc_sb64.so
    • Linux 32-bit: /opt/simba/spark/lib/32/libsparkodbc_sb32.so
  4. Add the information you just added to the /etc/odbcinst.ini file to the corresponding /usr/local/etc/odbcinst.ini file on your machine as well.

  5. Add the following information at the end of the simba.sparkodbc.ini file on your machine, and then save the file. For macOS, this file is in /Library/simba/spark/lib.

    DriverManagerEncoding=UTF16
    ODBCInstLib=/usr/local/Cellar/unixodbc/2.3.9/lib/libodbcinst.dylib
    

Step 3: Test your configuration

In this step, you write and run Python code to use your Databricks cluster or Databricks SQL endpoint to query a database table and display the first two rows of query results.

To query by using a cluster:

  1. Create a file named pyodbc-test-cluster.py with the following content. Replace <table-name> with the name of the database table to query, save the file, and then run the file with your Python interpreter.

    import pyodbc
    
    # Replace <table-name> with the name of the database table to query.
    table_name = "<table-name>"
    
    # Connect to the Databricks cluster by using the
    # Data Source Name (DSN) that you created earlier.
    conn = pyodbc.connect("DSN=Databricks_Cluster", autocommit=True)
    
    # Run a SQL query by using the preceding connection.
    cursor = conn.cursor()
    cursor.execute(f"SELECT * FROM {table_name} LIMIT 2")
    
    # Print the rows retrieved from the query.
    print(f"Query output: SELECT * FROM {table_name} LIMIT 2\n")
    for row in cursor.fetchall():
      print(row)
    

    Note

    If you skipped Step 2: Configure software and did not use an /etc/odbc.ini file, then specify connection details in the call to pyodbc.connect, for example:

    conn = pyodbc.connect("Driver=<driver-path>;" +
                          "HOST=<server-hostname>;" +
                          "PORT=443;" +
                          "Schema=default;" +
                          "SparkServerType=3;" +
                          "AuthMech=3;" +
                          "UID=token;" +
                          "PWD=<personal-access-token>;" +
                          "ThriftTransport=2;" +
                          "SSL=1;" +
                          "HTTPPath=<http-path>",
                          autocommit=True)
    

    Replace the placeholders with the values as described in Step 2: Configure software.

  2. To speed up running the code, start the cluster that corresponds to the HTTPPath setting in your odbc.ini file.

  3. Run the pyodbc-test-cluster.py file with your Python interpreter. The first two rows of the database table are displayed.

To query by using a SQL endpoint:

  1. Create a file named pyodbc-test-cluster.py. Replace <table-name> with the name of the database table to query, and then save the file.

    import pyodbc
    
    # Replace <table-name> with the name of the database table to query.
    table_name = "<table-name>"
    
    # Connect to the SQL endpoint by using the
    # Data Source Name (DSN) that you created earlier.
    conn = pyodbc.connect("DSN=SQL_Endpoint", autocommit=True)
    
    # Run a SQL query by using the preceding connection.
    cursor = conn.cursor()
    cursor.execute(f"SELECT * FROM {table_name} LIMIT 2")
    
    # Print the rows retrieved from the query.
    print(f"Query output: SELECT * FROM {table_name} LIMIT 2\n")
    for row in cursor.fetchall():
    print(row)
    

    Note

    If you skipped Step 2: Configure software and did not use an /etc/odbc.ini file, then specify connection details in the call to pyodbc.connect, for example:

    conn = pyodbc.connect("Driver=<driver-path>;" +
                          "HOST=<server-hostname>;" +
                          "PORT=443;" +
                          "Schema=default;" +
                          "SparkServerType=3;" +
                          "AuthMech=3;" +
                          "UID=token;" +
                          "PWD=<personal-access-token>;" +
                          "ThriftTransport=2;" +
                          "SSL=1;" +
                          "HTTPPath=<http-path>",
                          autocommit=True)
    

    Replace the placeholders with the values as described in Step 2: Configure software.

  2. To speed up running the code, start the SQL endpoint that corresponds to the HTTPPath setting in your odbc.ini file.

  3. Run the pyodbc-test-endpoint.py file with your Python interpreter. The first two rows of the database table are displayed.

Next steps

  • To run the Python test code against a different cluster or SQL endpoint, change the settings in the preceding two odbc.ini files. Or add a new entry to the [ODBC Data Sources] section, along with matching connection details, to the two odbc.ini files. Then change the DSN name in the test code to match the related name in [ODBC Data Sources].
  • To run the Python test code against a different database tables, change the table_name value.
  • To run the Python test code with a different SQL query, change the execute command string.

Windows

If your local Python code is running on a Windows machine, follow these instructions.

Step 1: Install software

  1. Download the Databricks ODBC driver.
  2. To install the Databricks ODBC driver, open the SimbaSparkODBC.zip file that you downloaded.
  3. Double-click the extracted Simba Spark.msi file, and follow any on-screen directions.
  4. Install the pyodbc module: from an administrative command prompt, run pip install pyodbc. For more information, see pyodbc on the PyPI website and Install in the pyodbc Wiki.

Step 2: Configure software

Specify connection details for the Databricks cluster or Databricks SQL endpoint for pyodbc to use.

To specify connection details for a cluster:

  1. Add a data source name (DSN) that contains information about your cluster: start the ODBC Data Sources application: on the Start menu, begin typing ODBC, and then click ODBC Data Sources.
  2. On the User DSN tab, click Add. In the Create New Data Source dialog box, click Simba Spark ODBC Driver, and then click Finish.
  3. In the Simba Spark ODBC Driver DSN Setup dialog box, change the following values:
    • Data Source Name: Databricks_Cluster
    • Description: My cluster
    • Spark Server Type: SparkThriftServer (Spark 1.1 and later)
    • Host(s): The Server Hostname value from the Advanced Options, JDBC/ODBC tab for your cluster.
    • Port: 443
    • Database: default
    • Mechanism: User Name and Password
    • User Name: token
    • Password: The value of your personal access token for your Databricks workspace.
    • Thrift Transport: HTTP
  4. Click HTTP Options. In the HTTP Properties dialog box, for HTTP Path, enter the HTTP Path value from the Advanced Options, JDBC/ODBC tab for your cluster, and then click OK.
  5. Click SSL Options. In the SSL Options dialog box, check the Enable SSL box, and then click OK.
  6. Click Test. If the test succeeds, click OK.

Tip

To allow pyodbc to switch connections to a different cluster, repeat this procedure with the specific connection details. Each DSN must have a unique name.

To specify connection details for a SQL endpoint:

  1. In the ODBC Data Sources application, on the User DSN tab, click Add. In the Create New Data Source dialog box, click Simba Spark ODBC Driver, and then click Finish.
  2. In the Simba Spark ODBC Driver dialog box, enter the following values:
    • Data Source Name: SQL_Endpoint
    • Description: My endpoint
    • Spark Server Type: SparkThriftServer (Spark 1.1 and later)
    • Host(s): The Server Hostname value from the Connection Details tab your SQL endpoint.
    • Port: 443
    • Database: default
    • Mechanism: User Name and Password
    • User Name: token
    • Password: The value of your personal access token for your SQL endpoint.
    • Thrift Transport: HTTP
  3. Click HTTP Options. In the HTTP Properties dialog box, for HTTP Path, enter the HTTP Path value from the Connection Details tab your SQL endpoint, and then click OK.
  4. Click SSL Options. In the SSL Options dialog box, check the Enable SSL box, and then click OK.
  5. Click Test. If the test succeeds, click OK.

Tip

To allow pyodbc to connect to switch connections to a different SQL endpoint, repeat this procedure with the specific connection details. Each DSN must have a unique name.

Step 3: Test your configuration

In this step, you write and run Python code to use your Databricks cluster or Databricks SQL endpoint to query a database table and display the first two rows of query results.

To query by using a cluster:

  1. Create a file named pyodbc-test-cluster.py with the following content. Replace <table-name> with the name of the database table to query, and then save the file.

    import pyodbc
    
    # Replace <table-name> with the name of the database table to query.
    table_name = "<table-name>"
    
    # Connect to the Databricks cluster by using the
    # Data Source Name (DSN) that you created earlier.
    conn = pyodbc.connect("DSN=Databricks_Cluster", autocommit=True)
    
    # Run a SQL query by using the preceding connection.
    cursor = conn.cursor()
    cursor.execute(f"SELECT * FROM {table_name} LIMIT 2")
    
    # Print the rows retrieved from the query.
    print(f"Query output: SELECT * FROM {table_name} LIMIT 2\n")
    for row in cursor.fetchall():
      print(row)
    
  2. To speed up running the code, start the cluster that corresponds to the Host(s) value in the Simba Spark ODBC Driver DSN Setup dialog box for your Databricks cluster.

  3. Run the pyodbc-test-cluster.py file with your Python interpreter. The first two rows of the database table are displayed.

To query by using a SQL endpoint:

  1. Create a file named pyodbc-test-cluster.py. Replace <table-name> with the name of the database table to query, and then save the file.

    import pyodbc
    
    # Replace <table-name> with the name of the database table to query.
    table_name = "<table-name>"
    
    # Connect to the SQL endpoint by using the
    # Data Source Name (DSN) that you created earlier.
    conn = pyodbc.connect("DSN=SQL_Endpoint", autocommit=True)
    
    # Run a SQL query by using the preceding connection.
    cursor = conn.cursor()
    cursor.execute(f"SELECT * FROM {table_name} LIMIT 2")
    
    # Print the rows retrieved from the query.
    print(f"Query output: SELECT * FROM {table_name} LIMIT 2\n")
    for row in cursor.fetchall():
    print(row)
    
  2. To speed up running the code, start the SQL endpoint that corresponds to the Host(s) value in the Simba Spark ODBC Driver DSN Setup dialog box for your Databricks SQL endpoint.

  3. Run the pyodbc-test-endpoint.py file with your Python interpreter. The first two rows of the database table are displayed.

Next steps

  • To run the Python test code against a different cluster or SQL endpoint, change the Host(s) value in the Simba Spark ODBC Driver DSN Setup dialog box for your Databricks cluster or Databricks SQL endpoint. Or create a new DSN. Then change the DSN name in the test code to match the related Data Source Name.
  • To run the Python test code against a different database table, change the table_name value.
  • To run the Python test code with a different SQL query, change the execute command string.