CData Cmdlets for Amazon Redshift 2019 - Online Help
Questions / Feedback?

Establishing a Connection

CData Cmdlets for Amazon Redshift 2019 - Build 19.0.7354

With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our Redshift 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 RedshiftCmdlets

The following line is then added to your profile, loading the cmdlets on the next session:

Import-Module RedshiftCmdlets;

You can then use the Connect-Redshift cmdlet to create a connection object that can be passed to other cmdlets:

$conn = Connect-Redshift -User 'admin' -Password 'admin' -Server 'examplecluster.my.us-west-2.redshift.amazonaws.com' -Port '5439' -Database 'dev'

Connecting to Redshift

The following connection properties are required in order to connect to data.

  • Server: The host name or IP of the server hosting the Redshift database.
  • Port: The port of the server hosting the Redshift database.

You can also optionally set the following:

  • Database: The default database to connect to when connecting to the Redshift Server. If this is not set, the user's default database will be used.

You can obtain these values in the AWS Management Console:

  1. Open the Amazon Redshift console (http://console.aws.amazon.com/redshift).
  2. On the Clusters page, click the name of the cluster.
  3. On the Configuration tab, obtain the properties from the Cluster Database Properties section. The connection property values will be the same as the values set in the ODBC URL.

Connect Using Standard Authentication

To authenticate using standard authentication, set the following:

  • User: The user which will be used to authenticate with the Redshift server.
  • Password: The password which will be used to authenticate with the Redshift server.

Connect Using SSL Authentication

You can leverage SSL authentication to connect to Redshift data via a secure session. Configure the following connection properties to connect to data:

  • SSLClientCert: Set this to the name of the certificate store for the client certificate. Used in the case of 2-way SSL, where truststore and keystore are kept on both the client and server machines.
  • SSLClientCertPassword: If a client certificate store is password-protected, set this value to the store's password.
  • SSLClientCertSubject: The subject of the TLS/SSL client certificate. Used to locate the certificate in the store.
  • SSLClientCertType:; The certificate type of the client store.
  • SSLServerCert: The certificate to be accepted from the server.

Connect Using SSO Authentication

Alternatively, you can connect to Redshift data using single sign-on (SSO). Configure the following connection properties to connect to data:

  • SSOProvider: The name of the SSO provider you are trying to authenticate. The following options are available: OKTA, OneLogin, and ADFS.
  • SSOLoginUrl: Set this to the login url associated with the SSO provider.
  • SSOUser: The username of the SSO Provider used to authenticate the user.
  • SSOPassword: The password of the SSO Provider used to authenticate the user.

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-Redshift cmdlet provides a native PowerShell interface for retrieving data:

$results = Select-Redshift -Connection $conn -Table "Orders" -Columns @("ShipName, ShipCity") -Where "ShipCountry='USA'"
The Invoke-Redshift 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-Redshift -Connection $conn -Table Orders -Where "ShipCountry = 'USA'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myOrdersData.csv -NoTypeInformation

You will notice that we piped the results from Select-Redshift 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-Redshift -User 'admin' -Password 'admin' -Server 'examplecluster.my.us-west-2.redshift.amazonaws.com' -Port '5439' -Database 'dev'
PS C:\> $row = Select-Redshift -Connection $conn -Table "Orders" -Columns (ShipName, ShipCity) -Where "ShipCountry = 'USA'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "Orders",
  "Columns":  [

  ],
  "ShipName":  "MyShipName",
  "ShipCity":  "MyShipCity"
} 

Deleting Data

The following line deletes any records that match the criteria:

Select-Redshift -Connection $conn -Table Orders -Where "ShipCountry = 'USA'" | Remove-Redshift

Updating Data

The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Redshift, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyOrdersUpdates.csv | %{
  $record = Select-Redshift -Connection $conn -Table Orders -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-Redshift -Connection $conn -Table Orders -Columns @("ShipName","ShipCity") -Values @($_.ShipName, $_.ShipCity) -Where "Id  = `'$_.Id`'"
  }else{
    Add-Redshift -Connection $conn -Table Orders -Columns @("ShipName","ShipCity") -Values @($_.ShipName, $_.ShipCity)
  }
}

 
 
Copyright (c) 2020 CData Software, Inc. - All rights reserved.
Build 19.0.7354.0