Ready to get started?

Download a free trial of the Databricks Data Provider to get started:

 Download Now

Learn more:

Databricks Icon Databricks ADO.NET Provider

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

Automate Databricks Integration Tasks from PowerShell



Are you in search of a quick and easy way to access Databricks data from PowerShell? This article demonstrates how to utilize the Databricks Cmdlets for tasks like connecting to Databricks data, automating operations, downloading data, 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.

PowerShell Cmdlets or ADO.NET Provider?

The Cmdlets are not only a PowerShell interface to Databricks, 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.

Once you have acquired the necessary connection properties, accessing Databricks data in PowerShell can be enabled in three 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-Object 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-Object 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-Object System.Data.CData.Databricks.DatabricksParameter("@myId","001d000000YBRseAAH"))) $cmd.ExecuteNonQuery()