![]() |
VOOZH | about |
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.
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.
Install the module:
Install-Module CassandraCmdlets
Connect:
$cassandra = Connect-Cassandra -Database "$Database" -Port "$Port" -Server "$Server"
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'}
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")
Connect to Cassandra:
$conn= New-Object System.Data.CData.Cassandra.CassandraConnection("Database=MyCassandraDB;Port=7000;Server=127.0.0.1;")
$conn.Open()
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 -Connection $Cassandra -Columns @('City','TotalDue') -Values @('MyCity', 'MyTotalDue') -Table Customer -Id "MyId"
$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()
Add-Cassandra -Connection $Cassandra -Table Customer -Columns @("City", "TotalDue") -Values @("MyCity", "MyTotalDue")
$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()
Remove-Cassandra -Connection $Cassandra -Table "Customer" -Id "MyId"
$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()
CodeProject
Download a free trial of the Cassandra Data Provider to get started:
Download NowLearn more:
👁 Apache Cassandra IconConnect .NET applications with the Cassandra real-time NoSQL cloud database service. Use Apache Cassandra as the big data backend that powers your .NET applications.