Cmdlets for Presto

Build 22.0.8479

接続の確立

CData Cmdlets ユーザーは、データモジュールをインストールし、接続プロパティを設定してスクリプトを開始できます。このセクションでは、CSV インポートおよびエクスポートcmdlet などのネイティブPowerShell cmdlet でPresto Cmdlets を使用する例を示します。

インストールおよび接続

PSGet がある場合は、PowerShell Gallery から次のコマンドを使ってcmdlet をインストールできます。CData サイトからセットアップを取得することもできます。

Install-Module PrestoCmdlets

プロファイルに以下を追加すると、次のセッションでcmdlet がロードされます。

Import-Module PrestoCmdlets;

Connect-Presto cmdlet を使って、別のcmdlet に渡すことができる接続オブジェクトを作成します。

$conn = Connect-Presto -Server '127.0.0.1' -Port 9200

Connecting to Presto

Set the Server and Port connection properties to connect, in addition to any authentication properties that may be required.

Securing Presto Connections

To enable TLS/SSL in the 本製品, set UseSSL to true.

Authenticating to Presto

Presto may be authenticated to by either LDAP or Kerberos. If the Presto server does not have authentication set up, leave AuthScheme set to NONE (default). In order to authenticate with either LDAP or Kerberos, follow the below steps:

LDAP

In order to authenticate with LDAP, set the following connection properties:

  • AuthScheme: Set this to LDAP.
  • User: The username being authenticated with in LDAP.
  • Password: The password associated with the User you are authenticating against LDAP with.

Kerberos

Please see Kerberos の使用 for details on how to authenticate with Kerberos.

データの取得

Select-Presto cmdlet はデータを取得するためのネイティブなPowerShell インターフェースを提供します。

$results = Select-Presto -Connection $conn -Table "[Hive].[Default].Customers" -Columns @("Id, Name") -Where "Industry='Floppy Disks'"
Invoke-Presto cmdlet はSQL インターフェースを提供します。このcmdlet を使うと、Query パラメータを介してSQL クエリを実行できます。

cmdlet 出力のパイプ処理

cmdlet は行オブジェクトをパイプラインに一度に一行ずつ返します。以下は、結果をCSV ファイルにエクスポートします。

Select-Presto -Connection $conn -Table [Hive].[Default].Customers -Where "Industry = 'Floppy Disks'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\my[Hive].[Default].CustomersData.csv -NoTypeInformation

Select-Presto からの結果をSelect-Object cmdlet にパイプして、Export-CSV cmdlet にパイプする前にいくつかのプロパティを実行していることがわかるでしょう。これをする理由は、CData Cmdlets は接続、テーブル、およびカラムの情報を結果セットの各行オブジェクトに追加しますが、必ずしもその情報がCSV ファイルに必要ではないからです。

ただし、これによってcmdlet の出力を別のcmdlet にパイプすることが容易になります。以下に、結果セットをJSON に変換する例を示します。

 
PS C:\> $conn  = Connect-Presto -Server '127.0.0.1' -Port 9200
PS C:\> $row = Select-Presto -Connection $conn -Table "[Hive].[Default].Customers" -Columns (Id, Name) -Where "Industry = 'Floppy Disks'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "[Hive].[Default].Customers",
  "Columns":  [

  ],
  "Id":  "MyId",
  "Name":  "MyName"
} 

データの削除

以下は、抽出条件に合うあらゆるレコードを削除します。

Select-Presto -Connection $conn -Table [Hive].[Default].Customers -Where "Industry = 'Floppy Disks'" | Remove-Presto

データの変更

cmdlet はデータクレンジング同様、データの変換を容易にします。次の例は、レコードがすでに存在するかどうか、挿入する前に更新が必要かどうかをチェックしてから、CSV ファイルのデータをPresto にロードします。

Import-Csv -Path C:\My[Hive].[Default].CustomersUpdates.csv | %{
  $record = Select-Presto -Connection $conn -Table [Hive].[Default].Customers -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-Presto -Connection $conn -Table [Hive].[Default].Customers -Columns @("Id","Name") -Values @($_.Id, $_.Name) -Where "Id  = `'$_.Id`'"
  }else{
    Add-Presto -Connection $conn -Table [Hive].[Default].Customers -Columns @("Id","Name") -Values @($_.Id, $_.Name)
  }
}

Copyright (c) 2023 CData Software, Inc. - All rights reserved.
Build 22.0.8479