Pipe Databricks Data to CSV in PowerShell

Ready to get started?

Download for a free trial:

Download Now

Learn more:

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.



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.

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!