Cmdlets for MarkLogic

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 MarkLogic 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 MarkLogicCmdlets

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

Import-Module MarkLogicCmdlets;

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

$conn = Connect-MarkLogic -User 'myusername' -Password 'mypassword' -Server 'marklogic'

Connecting to MarkLogic

Picking an API

The CData Cmdlets PowerShell Module for MarkLogic offers access to both the REST and ODBC APIs for MarkLogic. Please see Picking the right API for a discussion on which API is right for your use case.

Connection Details

Regardless of which API you are connecting to, you will need to include the following connection details:

  • Server - The server being connected to.
  • Database - (Optional) The specific database to connect to.
  • Port - (Optional) The specific port to use if the default port for the selected API is not being used default.

Create an ODBC Server

If connecting to the ODBC API, please follow the guide in MarkLogic's official website on how to create an ODBC Server, https://docs.marklogic.com/guide/admin/odbc.

Authenticating to MarkLogic

To authenticate to MarkLogic, specify the following connection properties:

  • User - The user to authenticate as.
  • Password - The password of the user.

Retrieving Data

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

$results = Select-MarkLogic -Connection $conn -Table "[CData].[main].Customer" -Columns @("Name, TotalDue") -Where "CustomerId='12345'"
The Invoke-MarkLogic 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-MarkLogic -Connection $conn -Table [CData].[main].Customer -Where "CustomerId = '12345'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\my[CData].[main].CustomerData.csv -NoTypeInformation

You will notice that we piped the results from Select-MarkLogic 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-MarkLogic -User 'myusername' -Password 'mypassword' -Server 'marklogic'
PS C:\> $row = Select-MarkLogic -Connection $conn -Table "[CData].[main].Customer" -Columns (Name, TotalDue) -Where "CustomerId = '12345'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "[CData].[main].Customer",
  "Columns":  [

  ],
  "Name":  "MyName",
  "TotalDue":  "MyTotalDue"
} 

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