Cmdlets for Cloudant

Build 24.0.9060

Establishing a Connection

With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our Cloudant Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.

Installing and Connecting

If you have PSGet, installing the cmdlets can be accomplished from the PowerShell Gallery with the following command. You can also obtain a setup from the CData site.

Install-Module CloudantCmdlets

The following line is then added to your profile, loading the cmdlets on the next session:

Import-Module CloudantCmdlets;

You can then use the Connect-Cloudant cmdlet to create a connection object that can be passed to other cmdlets:

$conn = Connect-Cloudant -User 'abc123' -Password 'abcdef'

Authenticating to Cloudant

Cloudant supports two types of authentication:

  • OAuth: Performs authentication based on the OAuth standard. Set AuthScheme to OAuth and supply the values for the Cloudant instance APIKey and URL.
  • Basic: Basic username/password authentication. Set AuthScheme to Basic.

IBM Cloudant Legacy

To connect via IBM Cloudant Legacy, ensure that you have a valid IBM Cloudant service credential.

To create an IBM Cloudant service credential:

  1. Log in to the IBM Cloud dashboard.
  2. Navigate to the Menu icon > Resource List, and open your IBM Cloudant service instance.
  3. In the menu, click Service credentials.
  4. Click New credential. Cloudant displays a Add new credential window.
  5. Enter a name for the new credential.
  6. Click Add. Your credentials are added to the Service credentials table.
  7. Click Actions > View credentials.
  8. Extract the values for User and Password from the JSON file.

Use the values you just obtained to set the User and Password.

Authenticating to a Local Instance

Cloudant supports authenticating to data in local instances from version 1.1.0 and above.

To authenticate to your local instance, set these parameters:

  1. Url: The Url of your local instance. For example: http://localhost:8006
  2. User: Your username.
  3. Password: Your password.

Retrieving Data

The Select-Cloudant cmdlet provides a native PowerShell interface for retrieving data:

$results = Select-Cloudant -Connection $conn -Table "Movies" -Columns @("MovieRuntime, MovieRating") -Where "MovieRating='R'"
The Invoke-Cloudant cmdlet provides an SQL interface. This cmdlet can be used to execute an SQL query via the Query parameter.

Piping Cmdlet Output

The cmdlets return row objects to the pipeline one row at a time. The following line exports results to a CSV file:

Select-Cloudant -Connection $conn -Table Movies -Where "MovieRating = 'R'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myMoviesData.csv -NoTypeInformation

You will notice that we piped the results from Select-Cloudant 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 object in the result set, and we do not necessarily want that information in our CSV file.

However, this makes it easy to pipe the output of one cmdlet to another. The following is an example of converting a result set to JSON:

 
PS C:\> $conn  = Connect-Cloudant -User 'abc123' -Password 'abcdef'
PS C:\> $row = Select-Cloudant -Connection $conn -Table "Movies" -Columns (MovieRuntime, MovieRating) -Where "MovieRating = 'R'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "Movies",
  "Columns":  [

  ],
  "MovieRuntime":  "MyMovieRuntime",
  "MovieRating":  "MyMovieRating"
} 

Deleting Data

The following line deletes any records that match the criteria:

Select-Cloudant -Connection $conn -Table Movies -Where "MovieRating = 'R'" | Remove-Cloudant

Modifying Data

The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Cloudant, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyMoviesUpdates.csv | %{
  $record = Select-Cloudant -Connection $conn -Table Movies -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-Cloudant -Connection $conn -Table Movies -Columns @("MovieRuntime","MovieRating") -Values @($_.MovieRuntime, $_.MovieRating) -Where "Id  = `'$_.Id`'"
  }else{
    Add-Cloudant -Connection $conn -Table Movies -Columns @("MovieRuntime","MovieRating") -Values @($_.MovieRuntime, $_.MovieRating)
  }
}

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 24.0.9060