Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →Automate MarkLogic Integration Tasks from PowerShell
Are you in search of a quick and easy way to access MarkLogic data from PowerShell? This article demonstrates how to utilize the MarkLogic Cmdlets for tasks like connecting to MarkLogic data, automating operations, downloading data, and more.
The CData Cmdlets for MarkLogic 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 MarkLogic.
PowerShell Cmdlets or ADO.NET Provider?
The Cmdlets are not only a PowerShell interface to MarkLogic, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete MarkLogic data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for MarkLogic. To access MarkLogic data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for MarkLogic.
Once you have acquired the necessary connection properties, accessing MarkLogic data in PowerShell can be enabled in three steps.
Set User, Password, and Server to the credentials for the MarkLogic account and the address of the server you want to connect to. You should also specify the REST API Port if you want to use a specific instance of a REST Server.
PowerShell
-
Install the module:
Install-Module MarkLogicCmdlets
-
Connect:
$marklogic = Connect-MarkLogic -User "$User" -Password "$Password" -Server "$Server"
-
Search for and retrieve data:
$id = "1" $customer = Select-MarkLogic -Connection $marklogic -Table "Customer" -Where "Id = `'$Id`'" $customer
You can also use the Invoke-MarkLogic cmdlet to execute SQL commands:
$customer = Invoke-MarkLogic -Connection $marklogic -Query 'SELECT * FROM Customer WHERE Id = @Id' -Params @{'@Id'='1'}
ADO.NET
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for MarkLogic\lib\System.Data.CData.MarkLogic.dll")
-
Connect to MarkLogic:
$conn= New-Object System.Data.CData.MarkLogic.MarkLogicConnection("User='myusername';Password='mypassword';Server='http://marklogic';") $conn.Open()
-
Instantiate the MarkLogicDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Name, TotalDue from Customer" $da= New-Object System.Data.CData.MarkLogic.MarkLogicDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.name $_.totaldue }
Update MarkLogic Data
PowerShell
Update-MarkLogic -Connection $MarkLogic -Columns @('Name','TotalDue') -Values @('MyName', 'MyTotalDue') -Table Customer -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.MarkLogic.MarkLogicCommand("UPDATE Customer SET Id='1' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MarkLogic.MarkLogicParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert MarkLogic Data
PowerShell
Add-MarkLogic -Connection $MarkLogic -Table Customer -Columns @("Name", "TotalDue") -Values @("MyName", "MyTotalDue")
ADO.NET
$cmd = New-Object System.Data.CData.MarkLogic.MarkLogicCommand("INSERT INTO Customer (Id) VALUES (@myId)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MarkLogic.MarkLogicParameter("@myId","1")))
$cmd.ExecuteNonQuery()
Delete MarkLogic Data
PowerShell
Remove-MarkLogic -Connection $MarkLogic -Table "Customer" -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.MarkLogic.MarkLogicCommand("DELETE FROM Customer WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MarkLogic.MarkLogicParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject