Databricks SQL Driver for Go

Note

This article covers the Databricks SQL Driver for Go, which is provided as-is and is not supported by Databricks through customer technical support channels. Questions and feature requests can be communicated through the Issues page of the databricks/databricks-sql-go repo on GitHub.

The Databricks SQL Driver for Go is a Go library that allows you to use Go code to run SQL commands on Databricks compute resources.

Requirements

Specify the DSN connection string

The Databricks SQL Driver for Go uses a data source name (DSN) connection string to access clusters and SQL warehouses. To specify the DSN connection string in the correct format, use the following syntax, where:

  • [your token] is your Databricks personal access token from the requirements.

  • [Workspace hostname] is the Server Hostname value from the requirements.

  • [Endpoint HTTP Path] is the HTTP Path value from the requirements.

databricks://:[your token]@[Workspace hostname][Endpoint HTTP Path]

For example, for a cluster:

databricks://:dapi1ab2c34defabc567890123d4efa56789@dbc-a1b2345c-d6e7.cloud.databricks.com/sql/protocolv1/o/1234567890123456/1234-567890-abcdefgh

For example, for a SQL warehouse:

databricks://:dapi1ab2c34defabc567890123d4efa56789@dbc-a1b2345c-d6e7.cloud.databricks.com/sql/1.0/endpoints/a1b234c5678901d2

Note

As a security best practice, you should not hard-code this DSN connection string into your Go code. Instead, you should retrieve this DSN connection string from a secure location. For example, the code example later in this article uses an environment variable.

Query data

The following code example demonstrates how to call the Databricks SQL Driver for Go to run a basic SQL query on a Databricks compute resource. This command returns the first two rows from the diamonds table.

The diamonds table is included in Sample datasets. This table is also featured in Tutorial: Query data with notebooks.

This code example retrieves the DSN connection string from an environment variable named DATABRICKS_DSN.

package main

import (
  "database/sql"
  "fmt"
  "os"

  _ "github.com/databricks/databricks-sql-go"
)

func main() {
  dsn := os.Getenv("DATABRICKS_DSN")

  if dsn == "" {
    panic("No connection string found." +
          "Set the DATABRICKS_DSN environment variable, and try again.")
  }

  db, err := sql.Open("databricks", dsn)

  if err != nil {
    panic(err)
  }

  var (
    _c0     string
    carat   string
    cut     string
    color   string
    clarity string
    depth   string
    table   string
    price   string
    x       string
    y       string
    z       string
  )

  rows, err := db.Query("SELECT * FROM default.diamonds LIMIT 2")

  if err != nil {
    panic(err)
  }

  defer rows.Close()

  for rows.Next() {
    err := rows.Scan(&_c0,
      &carat,
      &cut,
      &color,
      &clarity,
      &depth,
      &table,
      &price,
      &x,
      &y,
      &z)

    if err != nil {
      panic(err)
    }

    fmt.Print(_c0, ",",
      carat, ",",
      cut, ",",
      color, ",",
      clarity, ",",
      depth, ",",
      table, ",",
      price, ",",
      x, ",",
      y, ",",
      z, "\n")
  }

  err = rows.Err()

  if err != nil {
    panic(err)
  }
}

Output:

1,0.23,Ideal,E,SI2,61.5,55,326,3.95,3.98,2.43
2,0.21,Premium,E,SI1,59.8,61,326,3.89,3.84,2.31

For additional examples, see the examples folder in the databricks/databricks-sql-go repository on GitHub.

Additional resources