How to pipe Databricks Data to CSV in PowerShell



Use standard PowerShell cmdlets to access Databricks tables.

The CData Cmdlets Module for Databricks is a standard PowerShell module offering straightforward integration with Databricks. Below, you will find examples of using our Databricks Cmdlets with native PowerShell cmdlets.

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


Creating a Connection to Your Databricks Data

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).

$conn = Connect-Databricks  -Server "$Server" -Port "$Port" -TransportMode "$TransportMode" -HTTPPath "$HTTPPath" -UseSSL "$UseSSL" -User "$User" -Password "$Password"

Selecting Data

Follow the steps below to retrieve data from the Customers table and pipe the result into to a CSV file:

Select-Databricks -Connection $conn -Table Customers | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myCustomersData.csv -NoTypeInformation

You will notice that we piped the results from Select-Databricks into a Select-Object cmdlet and excluded some properties before piping them into an Export-Csv cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each "row" in the result set, and we do not necessarily want that information in our CSV file.

The Connection, Table, and Columns are appended to the results in order to facilitate piping results from one of the CData Cmdlets directly into another one.

Deleting Data

The following line deletes any records that match the criteria:

Select-Databricks -Connection $conn -Table Customers -Where "Country = US" | Remove-Databricks

Inserting and Updating Data

The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Databricks, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyCustomersUpdates.csv | %{
  $record = Select-Databricks -Connection $Databricks -Table Customers -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-Databricks -Connection $databricks -Table Customers -Columns ("City","CompanyName") -Values ($_.City, $_.CompanyName) -Where ("Id = `'"+$_.Id+"`'")
  }else{
    Add-Databricks -Connection $databricks -Table Customers -Columns ("City","CompanyName") -Values ($_.City, $_.CompanyName)
  }
}

As always, our goal is to simplify the way you connect to data. With cmdlets users can install a data module, set the connection properties, and start building. Download Cmdlets and start working with your data in PowerShell today!

Ready to get started?

Download a free trial of the Databricks Cmdlets to get started:

 Download Now

Learn more:

Databricks Icon Databricks Data Cmdlets

An easy-to-use set of PowerShell Cmdlets offering real-time access to Databricks. The Cmdlets allow users to easily read, write, update, and delete live data - just like working with SQL server.