Automate Databricks Integration Tasks from PowerShell

Ready to get started?

Download for a free trial:

Download Now

Learn more:

Databricks ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Databricks.



Are you looking for a quick and easy way to access Databricks data from PowerShell? We show how to use the Cmdlets for Databricks and the CData ADO.NET Provider for Databricks to connect to Databricks data and synchronize, automate, download, and more.

The CData Cmdlets for Databricks are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Databricks.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the Databricks API, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Databricks data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Databricks. To access Databricks data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Databricks.

After obtaining the needed connection properties, accessing Databricks data in PowerShell consists of three basic steps.

To connect to a Databricks cluster, set the properties as described below.

Note: The needed values can be found in your Databricks instance by navigating to Clusters, and selecting the desired cluster, and selecting the JDBC/ODBC tab under Advanced Options.

  • Server: Set to the Server Hostname of your Databricks cluster.
  • HTTPPath: Set to the HTTP Path of your Databricks cluster.
  • Token: Set to your personal access token (this value can be obtained by navigating to the User Settings page of your Databricks instance and selecting the Access Tokens tab).

PowerShell

  1. Install the module:

    Install-Module DatabricksCmdlets
  2. Connect:

    $databricks = Connect-Databricks -Server "$Server" -Port "$Port" -TransportMode "$TransportMode" -HTTPPath "$HTTPPath" -UseSSL "$UseSSL" -User "$User" -Password "$Password"
  3. Search for and retrieve data:

    $country = "US" $customers = Select-Databricks -Connection $databricks -Table "Customers" -Where "Country = `'$Country`'" $customers

    You can also use the Invoke-Databricks cmdlet to execute SQL commands:

    $customers = Invoke-Databricks -Connection $databricks -Query 'SELECT * FROM Customers WHERE Country = @Country' -Params @{'@Country'='US'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Databricks\lib\System.Data.CData.Databricks.dll")
  2. Connect to Databricks:

    $conn= New-Object System.Data.CData.Databricks.DatabricksConnection("Server=127.0.0.1;Port=443;TransportMode=HTTP;HTTPPath=MyHTTPPath;UseSSL=True;User=MyUser;Password=MyPassword;") $conn.Open()
  3. Instantiate the DatabricksDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT City, CompanyName from Customers" $da= New-Object System.Data.CData.Databricks.DatabricksDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.city $_.companyname }

Update Databricks Data

PowerShell

Update-Databricks -Connection $Databricks -Columns @('City','CompanyName') -Values @('MyCity', 'MyCompanyName') -Table Customers -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Databricks.DatabricksCommand("UPDATE Customers SET Country='US' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.Databricks.DatabricksParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert Databricks Data

PowerShell

Add-Databricks -Connection $Databricks -Table Customers -Columns @("City", "CompanyName") -Values @("MyCity", "MyCompanyName")

ADO.NET

$cmd = New-Object System.Data.CData.Databricks.DatabricksCommand("INSERT INTO Customers (Country) VALUES (@myCountry)", $conn) $cmd.Parameters.Add(new System.Data.CData.Databricks.DatabricksParameter("@myCountry","US")) $cmd.ExecuteNonQuery()

Delete Databricks Data

PowerShell

Remove-Databricks -Connection $Databricks -Table "Customers" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Databricks.DatabricksCommand("DELETE FROM Customers WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.Databricks.DatabricksParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()