接続の確立
CData Cmdlets ユーザーは、データモジュールをインストールし、接続プロパティを設定してスクリプトを開始できます。このセクションでは、CSV インポートおよびエクスポートcmdlet などのネイティブPowerShell cmdlet でMarkLogic Cmdlets を使用する例を示します。
MarkLogic への接続
Picking an API
The CData Cmdlets PowerShell Module for MarkLogic offers access to both the REST and ODBC APIs for MarkLogic. Please see 適切な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:
接続オブジェクトの作成
Connect-MarkLogic cmdlet を使って、別のcmdlet に渡すことができる接続オブジェクトを作成します。
$conn = Connect-MarkLogic -User 'myusername' -Password 'mypassword' -Server 'marklogic'
データの取得
Select-MarkLogic cmdlet はデータを取得するためのネイティブなPowerShell インターフェースを提供します。
$results = Select-MarkLogic -Connection $conn -Table "[CData].[main].Customer" -Columns @("Name, TotalDue") -Where "CustomerId='12345'"
Invoke-MarkLogic cmdlet はSQL インターフェースを提供します。このcmdlet を使うと、Query パラメータを介してSQL クエリを実行できます。
cmdlet 出力のパイプ処理
cmdlet は行オブジェクトをパイプラインに一度に一行ずつ返します。以下は、結果をCSV ファイルにエクスポートします。
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
Select-MarkLogic からの結果をSelect-Object cmdlet にパイプして、Export-CSV cmdlet にパイプする前にいくつかのプロパティを実行していることがわかるでしょう。これをする理由は、CData Cmdlets は接続、テーブル、およびカラムの情報を結果セットの各行オブジェクトに追加しますが、必ずしもその情報がCSV ファイルに必要ではないからです。
ただし、これによってcmdlet の出力を別のcmdlet にパイプすることが容易になります。以下に、結果セットを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"
}