![]() |
VOOZH | about |
The CData Cmdlets for Redshift 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 Redshift.
The Cmdlets are not only a PowerShell interface to Redshift, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Redshift data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Amazon Redshift. To access Redshift data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Amazon Redshift.
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:
You can obtain the and values in the AWS Management Console:
Install the module:
Install-Module RedshiftCmdlets
Connect:
$redshift = Connect-Redshift -User "$User" -Password "$Password" -Database "$Database" -Server "$Server" -Port "$Port"
Search for and retrieve data:
$shipcountry = "USA" $orders = Select-Redshift -Connection $redshift -Table "Orders" -Where "ShipCountry = `'$ShipCountry`'" $orders
You can also use the Invoke-Redshift cmdlet to execute SQL commands:
$orders = Invoke-Redshift -Connection $redshift -Query 'SELECT * FROM Orders WHERE ShipCountry = @ShipCountry' -Params @{'@ShipCountry'='USA'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Amazon 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 -Connection $Redshift -Columns @('ShipName','ShipCity') -Values @('MyShipName', 'MyShipCity') -Table Orders -Id "MyId"
$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()
Add-Redshift -Connection $Redshift -Table Orders -Columns @("ShipName", "ShipCity") -Values @("MyShipName", "MyShipCity")
$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()
Remove-Redshift -Connection $Redshift -Table "Orders" -Id "MyId"
$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
Download a free trial of the Amazon Redshift Data Provider to get started:
Download NowLearn more:
👁 Amazon Redshift IconRapidly create and deploy powerful .NET applications that integrate with Amazon Redshift data.