Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →Automate Greenplum Integration Tasks from PowerShell
Are you in search of a quick and easy way to access Greenplum data from PowerShell? This article demonstrates how to utilize the Greenplum Cmdlets for tasks like connecting to Greenplum data, automating operations, downloading data, and more.
The CData Cmdlets for Greenplum 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 Greenplum.
PowerShell Cmdlets or ADO.NET Provider?
The Cmdlets are not only a PowerShell interface to Greenplum, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Greenplum data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Greenplum. To access Greenplum data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Greenplum.
Once you have acquired the necessary connection properties, accessing Greenplum data in PowerShell can be enabled in three steps.
To connect to Greenplum, set the Server, Port (the default port is 5432), and Database connection properties and set the User and Password you wish to use to authenticate to the server. If the Database property is not specified, the default database for the authenticate user is used.
PowerShell
-
Install the module:
Install-Module GreenplumCmdlets
-
Connect:
$greenplum = Connect-Greenplum -User "$User" -Password "$Password" -Database "$Database" -Server "$Server" -Port "$Port"
-
Search for and retrieve data:
$shipcountry = "USA" $orders = Select-Greenplum -Connection $greenplum -Table "Orders" -Where "ShipCountry = `'$ShipCountry`'" $orders
You can also use the Invoke-Greenplum cmdlet to execute SQL commands:
$orders = Invoke-Greenplum -Connection $greenplum -Query 'SELECT * FROM Orders WHERE ShipCountry = @ShipCountry' -Params @{'@ShipCountry'='USA'}
ADO.NET
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Greenplum\lib\System.Data.CData.Greenplum.dll")
-
Connect to Greenplum:
$conn= New-Object System.Data.CData.Greenplum.GreenplumConnection("User=user;Password=admin;Database=dbname;Server=127.0.0.1;Port=5432;") $conn.Open()
-
Instantiate the GreenplumDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Freight, ShipName from Orders" $da= New-Object System.Data.CData.Greenplum.GreenplumDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.freight $_.shipname }
Update Greenplum Data
PowerShell
Update-Greenplum -Connection $Greenplum -Columns @('Freight','ShipName') -Values @('MyFreight', 'MyShipName') -Table Orders -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.Greenplum.GreenplumCommand("UPDATE Orders SET ShipCountry='USA' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Greenplum.GreenplumParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert Greenplum Data
PowerShell
Add-Greenplum -Connection $Greenplum -Table Orders -Columns @("Freight", "ShipName") -Values @("MyFreight", "MyShipName")
ADO.NET
$cmd = New-Object System.Data.CData.Greenplum.GreenplumCommand("INSERT INTO Orders (ShipCountry) VALUES (@myShipCountry)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Greenplum.GreenplumParameter("@myShipCountry","USA")))
$cmd.ExecuteNonQuery()
Delete Greenplum Data
PowerShell
Remove-Greenplum -Connection $Greenplum -Table "Orders" -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.Greenplum.GreenplumCommand("DELETE FROM Orders WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Greenplum.GreenplumParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject