Ready to get started?

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

 Download Now

Learn more:

Apache Cassandra Icon Cassandra ADO.NET Provider

Connect .NET applications with the Cassandra real-time NoSQL cloud database service. Use Apache Cassandra as the big data backend that powers your .NET applications.

Automate Cassandra Integration Tasks from PowerShell



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

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

PowerShell Cmdlets or ADO.NET Provider?

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

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

Set the Server, Port, and Database connection properties to connect to Cassandra. Additionally, to use internal authentication set the User and Password connection properties.

PowerShell

  1. Install the module:

    Install-Module CassandraCmdlets
  2. Connect:

    $cassandra = Connect-Cassandra -Database "$Database" -Port "$Port" -Server "$Server"
  3. Search for and retrieve data:

    $firstname = "Bob" $customer = Select-Cassandra -Connection $cassandra -Table "Customer" -Where "FirstName = `'$FirstName`'" $customer

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

    $customer = Invoke-Cassandra -Connection $cassandra -Query 'SELECT * FROM Customer WHERE FirstName = @FirstName' -Params @{'@FirstName'='Bob'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Apache Cassandra\lib\System.Data.CData.Cassandra.dll")
  2. Connect to Cassandra:

    $conn= New-Object System.Data.CData.Cassandra.CassandraConnection("Database=MyCassandraDB;Port=7000;Server=127.0.0.1;") $conn.Open()
  3. Instantiate the CassandraDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT City, TotalDue from Customer" $da= New-Object System.Data.CData.Cassandra.CassandraDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.city $_.totaldue }

Update Cassandra Data

PowerShell

Update-Cassandra -Connection $Cassandra -Columns @('City','TotalDue') -Values @('MyCity', 'MyTotalDue') -Table Customer -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Cassandra.CassandraCommand("UPDATE Customer SET FirstName='Bob' WHERE Id = @myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.Cassandra.CassandraParameter("@myId","10456255-0015501366"))) $cmd.ExecuteNonQuery()

Insert Cassandra Data

PowerShell

Add-Cassandra -Connection $Cassandra -Table Customer -Columns @("City", "TotalDue") -Values @("MyCity", "MyTotalDue")

ADO.NET

$cmd = New-Object System.Data.CData.Cassandra.CassandraCommand("INSERT INTO Customer (FirstName) VALUES (@myFirstName)", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.Cassandra.CassandraParameter("@myFirstName","Bob"))) $cmd.ExecuteNonQuery()

Delete Cassandra Data

PowerShell

Remove-Cassandra -Connection $Cassandra -Table "Customer" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Cassandra.CassandraCommand("DELETE FROM Customer WHERE Id=@myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.Cassandra.CassandraParameter("@myId","001d000000YBRseAAH"))) $cmd.ExecuteNonQuery()