Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →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.
About Databricks Data Integration
Accessing and integrating live data from Databricks has never been easier with CData. Customers rely on CData connectivity to:
- Access all versions of Databricks from Runtime Versions 9.1 - 13.X to both the Pro and Classic Databricks SQL versions.
- Leave Databricks in their preferred environment thanks to compatibility with any hosting solution.
- Secure authenticate in a variety of ways, including personal access token, Azure Service Principal, and Azure AD.
- Upload data to Databricks using Databricks File System, Azure Blog Storage, and AWS S3 Storage.
While many customers are using CData's solutions to migrate data from different systems into their Databricks data lakehouse, several customers use our live connectivity solutions to federate connectivity between their databases and Databricks. These customers are using SQL Server Linked Servers or Polybase to get live access to Databricks from within their existing RDBMs.
Read more about common Databricks use-cases and how CData's solutions help solve data problems in our blog: What is Databricks Used For? 6 Use Cases.
Getting Started
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
-
Install the module:
Install-Module DatabricksCmdlets
-
Connect:
$databricks = Connect-Databricks -Server "$Server" -Port "$Port" -TransportMode "$TransportMode" -HTTPPath "$HTTPPath" -UseSSL "$UseSSL" -User "$User" -Password "$Password"
-
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
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Databricks\lib\System.Data.CData.Databricks.dll")
-
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()
-
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()
CodeProject