Ready to get started?

Download a free trial of the Apache Hive Data Provider to get started:

 Download Now

Learn more:

Apache Hadoop Hive Icon Apache Hive ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Apache Hive-compatible distributions.

Automate Hive Integration Tasks from PowerShell



Are you in search of a quick and easy way to access Hive data from PowerShell? This article demonstrates how to utilize the Hive Cmdlets for tasks like connecting to Hive data, automating operations, downloading data, and more.

The CData Cmdlets for Hive 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 Hive.

PowerShell Cmdlets or ADO.NET Provider?

The Cmdlets are not only a PowerShell interface to Hive, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Hive data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Apache Hive. To access Hive data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Apache Hive.

Once you have acquired the necessary connection properties, accessing Hive data in PowerShell can be enabled in three steps.

Set the Server, Port, TransportMode, and AuthScheme connection properties to connect to Hive.

PowerShell

  1. Install the module:

    Install-Module ApacheHiveCmdlets
  2. Connect:

    $apachehive = Connect-ApacheHive -Server "$Server" -Port "$Port" -TransportMode "$TransportMode"
  3. Search for and retrieve data:

    $country = "US" $customers = Select-ApacheHive -Connection $apachehive -Table "Customers" -Where "Country = `'$Country`'" $customers

    You can also use the Invoke-ApacheHive cmdlet to execute SQL commands:

    $customers = Invoke-ApacheHive -Connection $apachehive -Query 'SELECT * FROM Customers WHERE Country = @Country' -Params @{'@Country'='US'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Apache Hive\lib\System.Data.CData.ApacheHive.dll")
  2. Connect to Hive:

    $conn= New-Object System.Data.CData.ApacheHive.ApacheHiveConnection("Server=127.0.0.1;Port=10000;TransportMode=BINARY;") $conn.Open()
  3. Instantiate the ApacheHiveDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT City, CompanyName from Customers" $da= New-Object System.Data.CData.ApacheHive.ApacheHiveDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.city $_.companyname }

Update Hive Data

PowerShell

Update-ApacheHive -Connection $ApacheHive -Columns @('City','CompanyName') -Values @('MyCity', 'MyCompanyName') -Table Customers -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.ApacheHive.ApacheHiveCommand("UPDATE Customers SET Country='US' WHERE Id = @myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.ApacheHive.ApacheHiveParameter("@myId","10456255-0015501366"))) $cmd.ExecuteNonQuery()

Insert Hive Data

PowerShell

Add-ApacheHive -Connection $ApacheHive -Table Customers -Columns @("City", "CompanyName") -Values @("MyCity", "MyCompanyName")

ADO.NET

$cmd = New-Object System.Data.CData.ApacheHive.ApacheHiveCommand("INSERT INTO Customers (Country) VALUES (@myCountry)", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.ApacheHive.ApacheHiveParameter("@myCountry","US"))) $cmd.ExecuteNonQuery()

Delete Hive Data

PowerShell

Remove-ApacheHive -Connection $ApacheHive -Table "Customers" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.ApacheHive.ApacheHiveCommand("DELETE FROM Customers WHERE Id=@myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.ApacheHive.ApacheHiveParameter("@myId","001d000000YBRseAAH"))) $cmd.ExecuteNonQuery()