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:
- Log in to the IBM Cloud dashboard.
- Navigate to the Menu icon > Resource List, and open your IBM Cloudant service instance.
- In the menu, click Service credentials.
- Click New credential. Cloudant displays a Add new credential window.
- Enter a name for the new credential.
- Click Add. Your credentials are added to the Service credentials table.
- Click Actions > View credentials.
- 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:
- Url: The Url of your local instance. For example: http://localhost:8006
- User: Your username.
- 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) } }