![]() |
VOOZH | about |
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.
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.
Install the module:
Install-Module ApacheHiveCmdlets
Connect:
$apachehive = Connect-ApacheHive -Server "$Server" -Port "$Port" -TransportMode "$TransportMode"
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'}
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")
Connect to Hive:
$conn= New-Object System.Data.CData.ApacheHive.ApacheHiveConnection("Server=127.0.0.1;Port=10000;TransportMode=BINARY;")
$conn.Open()
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-ApacheHive -Connection $ApacheHive -Columns @('City','CompanyName') -Values @('MyCity', 'MyCompanyName') -Table Customers -Id "MyId"
$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()
Add-ApacheHive -Connection $ApacheHive -Table Customers -Columns @("City", "CompanyName") -Values @("MyCity", "MyCompanyName")
$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()
Remove-ApacheHive -Connection $ApacheHive -Table "Customers" -Id "MyId"
$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()
CodeProject
Download a free trial of the Apache Hive Data Provider to get started:
Download NowLearn more:
👁 Apache Hadoop Hive IconRapidly create and deploy powerful .NET applications that integrate with Apache Hive-compatible distributions.