Databricks SDK for Go
In this article, you learn how to automate Databricks operations and accelerate development with the Databricks SDK for Go. This article supplements the Databricks SDK for Go README, API reference, and examples.
This feature is in Beta and is okay to use in production.
During the Beta period, Databricks recommends that you pin a dependency on the specific minor version of the Databricks SDK for Go that your code depends on, for example, in a project's go.mod file. For more information about pinning dependencies, see Managing dependencies.
Requirements
To use the Databricks SDK for Go, your development machine must have:
- Go installed.
- Databricks authentication configured.
Get started with the Databricks SDK for Go
-
On your development machine with Go already installed, an existing Go code project already created, and Databricks authentication configured, create a
go.modfile to track your Go code's dependencies by running thego mod initcommand, for example:Bashgo mod init sample -
Take a dependency on the Databricks SDK for Go package by running the
go mod edit -requirecommand, replacing0.8.0with the latest version of the Databricks SDK for Go package as listed in the CHANGELOG:Bashgo mod edit -require github.com/databricks/databricks-sdk-go@v0.8.0Your
go.modfile should now look like this:Gomodule sample
go 1.18
require github.com/databricks/databricks-sdk-go v0.8.0 -
Within your project, create a Go code file that imports the Databricks SDK for Go. The following example, in a file named
main.gowith the following contents, lists all the clusters in your Databricks workspace:Gopackage main
import (
"context"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/service/compute"
)
func main() {
w := databricks.Must(databricks.NewWorkspaceClient())
all, err := w.Clusters.ListAll(context.Background(), compute.ListClustersRequest{})
if err != nil {
panic(err)
}
for _, c := range all {
println(c.ClusterName)
}
} -
Add any missing module dependencies by running the
go mod tidycommand:Bashgo mod tidynoteIf you get the error
go: warning: "all" matched no packages, you forgot to add a Go code file that imports the Databricks SDK for Go. -
Grab copies of all packages needed to support builds and tests of packages in your
mainmodule, by running thego mod vendorcommand:Bash