Establishing a Connection
With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our Greenplum Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.
Installing and Connecting
If you have PSGet, installing the cmdlets can be accomplished from the PowerShell Gallery with the following command. You can also obtain a setup from the CData site.
Install-Module GreenplumCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module GreenplumCmdlets;
You can then use the Connect-Greenplum cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-Greenplum -User 'user' -Password 'admin' -Server '127.0.0.1' -Port '5432' -Database 'dbname'
Connecting to Greenplum
To connect to Greenplum, set the Server, Port (the default port is 5432), and Database connection properties and set the User and Password you want to use to authenticate to the server. If the Database property is not specified, the cmdlet connects to the user's default database (it is the same name as the user).
Authenticating to Greenplum
The CData Cmdlets PowerShell Module for Greenplum supports the md5, password, Kerberos and SASL (particulary, SCRAM-SHA-256) authentication methods.The specific authentication method is setup in the pg_hba.conf file on the Greenplum Server. You can find instructions about authentication setup on the Greenplum Server here. The md5, password and SASL authentication methods do not require additional setup by the CData Cmdlets PowerShell Module for Greenplum.
Kerberos
The Greenplum Server initiates authentication with the Kerberos Server when the CData Cmdlets PowerShell Module for Greenplum attempts a connection. You need to setup Kerberos on the Greenplum Server to activate this authentication method. After you have Kerberos authentication setup on the Greenplum Server, see Using Kerberos for details on how to authenticate with Kerberos by the cmdlet.
Retrieving Data
After you have created a connection, you can use the other cmdlets to perform operations that you would normally expect to be able
to perform against a relational database. The Select-Greenplum cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-Greenplum -Connection $conn -Table ""template1"."public".Orders" -Columns @("ShipName, ShipCity") -Where "ShipCountry='USA'"The Invoke-Greenplum cmdlet provides an SQL interface. This cmdlet can be used to execute an SQL query via the Query parameter.
Piping Cmdlet Output
The cmdlets return row objects to the pipeline one row at a time. The following line exports results to a CSV file:
Select-Greenplum -Connection $conn -Table "template1"."public".Orders -Where "ShipCountry = 'USA'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\my"template1"."public".OrdersData.csv -NoTypeInformation
You will notice that we piped the results from Select-Greenplum into a Select-Object cmdlet and excluded some properties before piping them into an Export-CSV cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each row object in the result set, and we do not necessarily want that information in our CSV file.
However, this makes it easy to pipe the output of one cmdlet to another. The following is an example of converting a result set to JSON:
PS C:\> $conn = Connect-Greenplum -User 'user' -Password 'admin' -Server '127.0.0.1' -Port '5432' -Database 'dbname' PS C:\> $row = Select-Greenplum -Connection $conn -Table ""template1"."public".Orders" -Columns (ShipName, ShipCity) -Where "ShipCountry = 'USA'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": ""template1"."public".Orders", "Columns": [ ], "ShipName": "MyShipName", "ShipCity": "MyShipCity" }
Deleting Data
The following line deletes any records that match the criteria:
Select-Greenplum -Connection $conn -Table "template1"."public".Orders -Where "ShipCountry = 'USA'" | Remove-Greenplum
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Greenplum, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\My"template1"."public".OrdersUpdates.csv | %{ $record = Select-Greenplum -Connection $conn -Table "template1"."public".Orders -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-Greenplum -Connection $conn -Table "template1"."public".Orders -Columns @("ShipName","ShipCity") -Values @($_.ShipName, $_.ShipCity) -Where "Id = `'$_.Id`'" }else{ Add-Greenplum -Connection $conn -Table "template1"."public".Orders -Columns @("ShipName","ShipCity") -Values @($_.ShipName, $_.ShipCity) } }