Ready to get started?

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

 Download Now

Learn more:

Apache Spark Icon Apache Spark ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Apache Spark.

Automate Spark Integration Tasks from PowerShell



Are you in search of a quick and easy way to access Spark data from PowerShell? This article demonstrates how to utilize the Spark Cmdlets for tasks like connecting to Spark data, automating operations, downloading data, and more.

The CData Cmdlets for Spark 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 Spark.

PowerShell Cmdlets or ADO.NET Provider?

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

Once you have acquired the necessary connection properties, accessing Spark data in PowerShell can be enabled in three steps.

Set the Server, Database, User, and Password connection properties to connect to SparkSQL.

PowerShell

  1. Install the module:

    Install-Module SparkSQLCmdlets
  2. Connect:

    $sparksql = Connect-SparkSQL -Server "$Server"
  3. Search for and retrieve data:

    $country = "US" $customers = Select-SparkSQL -Connection $sparksql -Table "Customers" -Where "Country = `'$Country`'" $customers

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

    $customers = Invoke-SparkSQL -Connection $sparksql -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 Apache Spark\lib\System.Data.CData.SparkSQL.dll")
  2. Connect to Spark:

    $conn= New-Object System.Data.CData.SparkSQL.SparkSQLConnection("Server=127.0.0.1;") $conn.Open()
  3. Instantiate the SparkSQLDataAdapter, execute an SQL query, and output the results:

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

Update Spark Data

PowerShell

Update-SparkSQL -Connection $SparkSQL -Columns @('City','Balance') -Values @('MyCity', 'MyBalance') -Table Customers -Id "MyId"

ADO.NET

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

Insert Spark Data

PowerShell

Add-SparkSQL -Connection $SparkSQL -Table Customers -Columns @("City", "Balance") -Values @("MyCity", "MyBalance")

ADO.NET

$cmd = New-Object System.Data.CData.SparkSQL.SparkSQLCommand("INSERT INTO Customers (Country) VALUES (@myCountry)", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.SparkSQL.SparkSQLParameter("@myCountry","US"))) $cmd.ExecuteNonQuery()

Delete Spark Data

PowerShell

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

ADO.NET

$cmd = New-Object System.Data.CData.SparkSQL.SparkSQLCommand("DELETE FROM Customers WHERE Id=@myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.SparkSQL.SparkSQLParameter("@myId","001d000000YBRseAAH"))) $cmd.ExecuteNonQuery()