Automate Redshift Integration Tasks from PowerShell
The CData ADO.NET Provider for Redshift is a standard ADO.NET Provider that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Redshift.
ADO.NET Provider
The ADO.NET Provider provides a SQL interface for Redshift; this tutorial shows how to use the Provider to create, retrieve, update, and delete Redshift data.
Once you have acquired the necessary connection properties, accessing Redshift data in PowerShell can be enabled in three steps.
To connect to Redshift, set the following:
- Server: Set this to the host name or IP address of the cluster hosting the Database you want to connect to.
- Port: Set this to the port of the cluster.
- Database: Set this to the name of the database. Or, leave this blank to use the default database of the authenticated user.
- User: Set this to the username you want to use to authenticate to the Server.
- Password: Set this to the password you want to use to authenticate to the Server.
You can obtain the Server and Port values in the AWS Management Console:
- Open the Amazon Redshift console (http://console.aws.amazon.com/redshift).
- On the Clusters page, click the name of the cluster.
- On the Configuration tab for the cluster, copy the cluster URL from the connection strings displayed.
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Redshift\lib\System.Data.CData.Redshift.dll") -
Connect to Redshift:
$conn= New-Object System.Data.CData.Redshift.RedshiftConnection("User=admin;Password=admin;Database=dev;Server=examplecluster.my.us-west-2.redshift.amazonaws.com;Port=5439;") $conn.Open() -
Instantiate the RedshiftDataAdapter, execute an SQL query, and output the results:
$sql="SELECT ShipName, ShipCity from Orders" $da= New-Object System.Data.CData.Redshift.RedshiftDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.shipname $_.shipcity }
Update Redshift Data
$cmd = New-Object System.Data.CData.Redshift.RedshiftCommand("UPDATE Orders SET ShipCountry='USA' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Redshift.RedshiftParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert Redshift Data
$cmd = New-Object System.Data.CData.Redshift.RedshiftCommand("INSERT INTO Orders (ShipCountry) VALUES (@myShipCountry)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Redshift.RedshiftParameter("@myShipCountry","USA")))
$cmd.ExecuteNonQuery()
Delete Redshift Data
$cmd = New-Object System.Data.CData.Redshift.RedshiftCommand("DELETE FROM Orders WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Redshift.RedshiftParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject