Cmdlets for Apache Hive

Build 22.0.8479

接続の確立

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

インストールおよび接続

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

Install-Module ApacheHiveCmdlets

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

Import-Module ApacheHiveCmdlets;

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

$conn = Connect-ApacheHive -Server '127.0.0.1'

Apache Hive への接続

セルフホステッドインスタンス

Apache Hive への接続を確立するには以下を指定します。

  • TransportMode:Hive サーバーとの通信に使用するトランスポートモード。有効な入力値は、BINARY およびHTTP です。デフォルトではBINARY が選択されます。
  • Server:HiveServer2 をホストするサーバーのホスト名またはIP アドレスに設定。
  • Port:HiveServer2 インスタンスへの接続用のポートに設定。
  • UseSSL(オプション):TLS/SSL を有効にするには、これを設定。

Amazon EMR インスタンス

Connections to Amazon EMR will need to be established using an SSH tunnel.

Use the following procedure to create an SSH tunnel to EMR.

  1. To begin, you will need an active EMR cluster and an EC2 key pair. The key pair can be in .ppk or .pem format.
  2. Next, authorize inbound traffic in your cluster settings.

Set the following to connect (while running an active tunnel session to EMR):

  • Server:Set this to the master node (master-public-dns-name) where the Apache Hive server is running.
  • Port:Set this to the port required to connect to Apache Hive.
  • UseSSH:Set this to true.
  • SSHServer:Set this to the master node (master-public-dns-name).
  • SSHPort:Set this to 22.
  • SSHAuthMode:Set this to PUBLIC_KEY.
  • SSHUser:Set this to hadoop.
  • SSHClientCert:Set this to the full path to the key file.
  • SSHClientCertType:Set this to type that corresponds to the key file.Typically either PEMKEY_FILE or PPKFILE.

Hadoop Cluster on Azure HDInsight Instances

You will need to supply the following to establish a connection to a Hadoop cluster hosted on Azure HDInsight:

  • User:Set this to the cluster username that you specified when creating the cluster on Azure HDInsight.
  • Password:Set this to the cluster password that you specified when creating the cluster on Azure HDInsight.
  • Server:The server corresponding to your cluster.For example: myclustername.azurehdinsight.net.
  • Port:Set this to the port running HiveServer2.This will be 443 by default.
  • HTTPPath:Set this to the HTTP path for the hive2 service.This will be hive2 by default.
  • TransportMode:Set this to HTTP.
  • UseSSL:Set this to true.
  • QueryPassthrough(オプション):QueryPassthrough をtrue に設定して本製品 のSQL エンジンをバイパスし、Apache Hive に直接HiveQL クエリを実行します。

Google インスタンス

接続の前に

Ensure that the Apache Hive server on DataProc was created with the DataProc Component Gateway enabled.

Next, obtain the external IP address of the Hive Cluster. To find this, load up the Cloud Shell and list the instances.

gcloud compute instances list

Note the external IP of the relevant machine.

Build an SSH Tunnel to the Hive Cluster Web Interface

Navigate to the Hive cluster on DataProc and select the WEB INTERFACES tab. Select Create an SSH tunnel to connect to a web interface.

A cloud console command will be shown that can be used to create an SSH key pair. Download the private key from the directory specified in the console.

Configure the SSH tunnel in an SSH utility:

  • ホスト名:Set this to the external IP noted above.
  • ポート:22
  • Point the tool to your private SSH key.
  • For the Tunnel, map an open port to localhost:10000. localhost will be resolved properly on the server.

Connecting to Hive on Google DataProc

Specify the following information to connect to Apache Hive:

  • TransportMode:Set this to BINARY.
  • AuthScheme:Set this to Plain.
  • Port:Set this to the chosen SSH Tunnel port on the local machine.

Apache Hive への認証

PLAIN

Set AuthScheme to PLAIN when the hive.server2.authentication property is set to None (uses Plain SASL), PAM, or CUSTOM. In addition, set the following connection properties:

  • User:ログインするユーザーに設定。何も設定しない場合は、代わりに'anonymous' が送信されます。
  • Password:ユーザーのパスワードに設定。何も設定しない場合は、代わりに'anonymous' が送信されます。

LDAP

Set AuthScheme to LDAP when the hive.server2.authentication property is set to LDAP. さらに、次の接続プロパティを設定します。

  • User:ログインするユーザーに設定。
  • Password:ユーザーのパスワードに設定。

NOSASL

Set AuthScheme to NOSASL when the hive.server2.authentication property is set to NOSASL. There are no user credentials submitted in this auth scheme.

Kerberos

Set AuthScheme to Kerberos when the hive.server2.authentication property is set to Kerberos. Kerberos で認証する方法についての詳細は、Kerberos の使用 を参照してください。

データの取得

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

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

cmdlet 出力のパイプ処理

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

Select-ApacheHive -Connection $conn -Table [CData].[Default].Customers -Where "Country = 'US'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\my[CData].[Default].CustomersData.csv -NoTypeInformation

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

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

 
PS C:\> $conn  = Connect-ApacheHive -Server '127.0.0.1'
PS C:\> $row = Select-ApacheHive -Connection $conn -Table "[CData].[Default].Customers" -Columns (City, CompanyName) -Where "Country = 'US'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

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

  ],
  "City":  "MyCity",
  "CompanyName":  "MyCompanyName"
} 

データの削除

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

Select-ApacheHive -Connection $conn -Table [CData].[Default].Customers -Where "Country = 'US'" | Remove-ApacheHive

データの変更

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

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

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