Automate Couchbase Integration Tasks from PowerShell



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

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

PowerShell Cmdlets or ADO.NET Provider?

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

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

To connect using the Login method, set User, Password, and Server to the credentials for the account and the address of the server you want to connect to.

PowerShell

  1. Install the module:

    Install-Module CouchbaseCmdlets
  2. Connect:

    $couchbase = Connect-Couchbase -User "$User" -Password "$Password" -Server "$Server"
  3. Search for and retrieve data:

    $firstname = "Bob" $customer = Select-Couchbase -Connection $couchbase -Table "Customer" -Where "FirstName = `'$FirstName`'" $customer

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

    $customer = Invoke-Couchbase -Connection $couchbase -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 Couchbase\lib\System.Data.CData.Couchbase.dll")
  2. Connect to Couchbase:

    $conn= New-Object System.Data.CData.Couchbase.CouchbaseConnection("User=myuseraccount;Password=mypassword;Server=http://mycouchbaseserver;") $conn.Open()
  3. Instantiate the CouchbaseDataAdapter, execute an SQL query, and output the results:

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

Update Couchbase Data

PowerShell

Update-Couchbase -Connection $Couchbase -Columns @('FirstName','TotalDue') -Values @('MyFirstName', 'MyTotalDue') -Table Customer -Id "MyId"

ADO.NET

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

Insert Couchbase Data

PowerShell

Add-Couchbase -Connection $Couchbase -Table Customer -Columns @("FirstName", "TotalDue") -Values @("MyFirstName", "MyTotalDue")

ADO.NET

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

Delete Couchbase Data

PowerShell

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

ADO.NET

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

Ready to get started?

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

 Download Now

Learn more:

Couchbase Icon Couchbase ADO.NET Provider

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