Automate Cloudant Integration Tasks from PowerShell

Ready to get started?

Download a free trial:

Download Now

Learn more:

Cloudant ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with IBM Cloudant NoSQL databases.



Are you looking for a quick and easy way to access Cloudant data from PowerShell? We show how to use the Cmdlets for Cloudant and the CData ADO.NET Provider for Cloudant to connect to Cloudant data and synchronize, automate, download, and more.

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

Cmdlets or ADO.NET?

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

After obtaining the needed connection properties, accessing Cloudant data in PowerShell consists of three basic steps.

Set the following connection properties to connect to Cloudant:

  • User: Set this to your username.
  • Password: Set this to your password.

PowerShell

  1. Install the module:

    Install-Module CloudantCmdlets
  2. Connect:

    $cloudant = Connect-Cloudant -User "$User" -Password "$Password"
  3. Search for and retrieve data:

    $movierating = "R" $movies = Select-Cloudant -Connection $cloudant -Table "Movies" -Where "MovieRating = `'$MovieRating`'" $movies

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

    $movies = Invoke-Cloudant -Connection $cloudant -Query 'SELECT * FROM Movies WHERE MovieRating = @MovieRating' -Params @{'@MovieRating'='R'}

ADO.NET

  1. Load the provider's assembly:

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

    $conn= New-Object System.Data.CData.Cloudant.CloudantConnection("User=abc123; Password=abcdef;") $conn.Open()
  3. Instantiate the CloudantDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT MovieRuntime, MovieRating from Movies" $da= New-Object System.Data.CData.Cloudant.CloudantDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.movieruntime $_.movierating }

Update Cloudant Data

PowerShell

Update-Cloudant -Connection $Cloudant -Columns @('MovieRuntime','MovieRating') -Values @('MyMovieRuntime', 'MyMovieRating') -Table Movies -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Cloudant.CloudantCommand("UPDATE Movies SET MovieRating='R' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.Cloudant.CloudantParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert Cloudant Data

PowerShell

Add-Cloudant -Connection $Cloudant -Table Movies -Columns @("MovieRuntime", "MovieRating") -Values @("MyMovieRuntime", "MyMovieRating")

ADO.NET

$cmd = New-Object System.Data.CData.Cloudant.CloudantCommand("INSERT INTO Movies (MovieRating) VALUES (@myMovieRating)", $conn) $cmd.Parameters.Add(new System.Data.CData.Cloudant.CloudantParameter("@myMovieRating","R")) $cmd.ExecuteNonQuery()

Delete Cloudant Data

PowerShell

Remove-Cloudant -Connection $Cloudant -Table "Movies" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Cloudant.CloudantCommand("DELETE FROM Movies WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.Cloudant.CloudantParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()