Amazon Redshift is a data warehouse service hosted by AWS. Redshift is a columnar data storage RDBMS database with support for storing petabyte-scale  data.  Redshift is typically used to run complex data analysis queries run across a cluster of nodes. Redshift is based on PostgreSQL 8.0.2 and gets some its features such as the public schema from PostgreSQL. In an earlier article we discussed creating a connection to Redshift with Toad Edge. In this continuation article we shall discuss some of the Toad Edge features for Redshift. Toad Edge supports several databases, including MySQL and PostgreSQL, and some of its features are different for different databases. This article has the following sections.

Creating a Database Table

Querying Table

Displaying Result

Exporting Query Result

Creating Multiple Connections

Disconnecting and Reconnecting

Dropping Table

Deleting Cluster

Creating a Database Table

In this section we shall create a database table and add table data. The following SQL script creates a table wlslog.

CREATE TABLE wlslog(logid INTEGER,category VARCHAR(255),type VARCHAR(255),servername VARCHAR(255), code VARCHAR(255),msg VARCHAR(255));

INSERT INTO wlslog VALUES(1,'Notice','WebLogicServer','AdminServer','BEA-000365','Server state changed to STANDBY');

INSERT INTO wlslog VALUES(2,'Notice','WebLogicServer','AdminServer','BEA-000365','Server state changed to STARTING');

INSERT INTO wlslog VALUES(3,'Notice','WebLogicServer','AdminServer','BEA-000365','Server state changed to ADMIN');

INSERT INTO wlslog VALUES(4,'Notice','WebLogicServer','AdminServer','BEA-000365','Server state changed to RESUMING');

INSERT INTO wlslog VALUES(5,'Notice','WebLogicServer','AdminServer','BEA-000361','Started WebLogic AdminServer');

INSERT INTO wlslog VALUES(6,'Notice','WebLogicServer','AdminServer','BEA-000365','Server state changed to RUNNING');

INSERT INTO wlslog VALUES(7,'Notice','WebLogicServer','AdminServer','BEA-000360','Server started in RUNNING mode');

Copy and paste the SQL script into a worksheet and click on Execute>Execute All statements in the toolbar as shown in Figure 1.

d1

Figure 1. Execute>Execute All Statements

Alternatively, select Execute All Statements in the worksheet toolbar as shown in Figure 2.

d2

Figure 2. Execute All Statements

The SQL statements get processed and the table wlslog gets created, as shown in Figure 3.

d3

Figure 3. Table wlslog Created

The various options available for a new table are Refresh, Drop, and Cascade Drop, as shown in Figure 4. Right-click on the wlslog table to display and use the options.

d4

Figure 4. Table Options

Querying Table

In this section we shall query the table created.  Open a new SQL Worksheet by selecting Open SQL Worksheet in toolbar as shown in Figure 5.

d5

Figure  5. Open SQL Worksheet

Add SELECT to the worksheet and using code completion feature of the worksheet select * as shown in Figure 6.

d6

Figure 6. Adding SQL Query Statement using Code Completion

Add a space and code completion displays the options available; from which, select FROM as shown in Figure 7.

d7

Figure 7. Selecting FROM from Code Completion Options

Using further code completion select the wlslog table as shown in Figure 8.

d8

Figure 8. Selecting Table wlslog

The complete SQL query statement is shown in Figure 9. Optionally add a “;” at the statement end.

d9

Figure 9. SQL Query Statement

Click on Execute SQL Statement to run the statement as shown in Figure 10.

d10

Figure 10. Execute SQL Statement

The SQL query runs and a Result Set is displayed, as shown in Figure 11.

d11

Figure 11. Result Set

The SQL Recall lists the SQL Statements that have run successfully or have failed, as shown in Figure 12.

d12

Figure 12. SQL Recall

Displaying Result

Different options are available to customize the display of the result set, including the following:

– Show or Undisplay Result

– Hide individual Columns with the restore option

– Sort individual column values in ascending or descending mode

– Toggle between Vertical and Horizontal Layout

The Show Result button shown in Figure 13 toggles between showing and undisplaying result. With result displayed click on Show Result.

d13

Figure 13. Show Result Toggle

The result gets undisplayed, as shown in Figure 14.

d14

Figure 14. Result undisplayed

Click on Show Result again to redisplay the result. 

To hide an individual column right-click in the column and select Hide Column as shown in Figure 15. Hide Column is selected for the code column in Figure 15.

d15

Figure 15. Hide Column

The code column gets removed from the result set, as shown in Figure 16. Similarly, select Hide Column for the servername column as shown in Figure 16.

d16

Figure 16. Selecting Hide Column for servername

Remove the category and type columns similarly to display only two columns in result, logid and msg, as shown in Figure 17.

d17

Figure 17. Result with only 2 Columns

To restore hidden columns right-click in the result set and select Restore hidden columns as shown in Figure 18.

d18

Figure 18. Restore Hidden Columns

All the hidden columns get restored, as shown in Figure 19. Columns may be hidden one at a time and restored together.

d19

Figure 19. Hidden Columns Restored

Individual attribute or column values may be displayed in ascending or descending mode. As the attribute values are correlated with other attributes in a data record, setting one column in ascending/descending mode would modify the ordering of the other columns and each column cannot be ordered in ascending/descending mode independently. For numeric values ascending is from lower numeric value to higher numeric value and for non-numeric values ascending and descending are based alphabetically. Display the msg column values in ascending mode by setting the toggle to ^ as shown in Figure 20.

d20

Figure 20. Setting msg Column toggle to ^

To display in alphabetically descending mode set the toggle to v as shown in Figure 21.

d21

Figure 21. Displaying msg Column in Descending Mode

Similarly, the code column values are shown in descending mode in Figure 22.

d22

Figure 22. Code Column Values in Descending Mode

And the code column values are shown in ascending mode in Figure 23.

d23

Figure 23. Code Column Values in Ascending Mode

The logid column type is a numeric data type and is shown in ascending mode in Figure 24.

d24

Figure 24. Logid Column Values in Ascending Mode

The logid column values in descending mode are shown in Figure 25.

d25

Figure 25. Logid column Values in Descending Mode

By default the layout of the Worksheet and the Result is Horizontal, as shown in Figure 26. To display vertically click on Vertical Layout as shown in Figure 26.

d26

Figure 26. Selecting Vertical Layout

The SQL Worksheet and Result get displayed vertically as shown in Figure 27.

d27

Figure 27. Vertical Layout

To revert to horizontal layout select Horizontal Layout as shown in Figure 28.

d28

Figure 28. Selecting Horizontal Layout

The layout gets reverted to horizontal as shown in Figure 29.

d29

Figure 29. Horizontal Layout

Exporting Query Result

A result set may be exported to one of the supported formats (CSV,HTML,SQL, and XML). The complete result may be exported or selected rows may be exported. To export the complete result right-click in the result set and select Export Result>CSV (or SQL) as shown in Figure 30.

d30

Figure 30. Export Result>CSV

In Export Data select the output format as CSV and select Destination for Exported Data as Worksheet as shown in Figure 31. The export.csv file gets generated in the Users directory by default. With Format exported queries selected click on Next.

d31

Figure 31. Export Data>Destination and Format

Next, define detailed CSV settings as shown in Figure 32. Keeping the default settings for Field Delimiter, Text Delimiter, Escape Character, and with the Include Column Headers selected, click on Finish.

d32

Figure 32. CSV Settings

The CSV file exported is shown in Figure 33.

d33

Figure 33. CSV File Exported

As another example of exporting the result set, select a single row and right-click and select Export Selection>HTML as shown in Figure 34.

d34

Figure 34. Exporting Single Row to HTML

In the Save As dialog specify a File name to save to and click on Save. The HTML file exported is shown in Figure 35.

d35

Figure 35. HTML File Exported

To select the complete result select all the rows with Shift+Row. Right-click and select Export Selection>HTML as shown in Figure 36.

d36

Figure 36. Exporting All Rows to HTML

Specify File name to save to as before. The exported HTML is shown in Figure 37.

d37

Figure 37. Exported HTML

Creating Multiple Connections

In this section we shall discuss using multiple connections to the same or a different Redshift cluster.  Multiple connections may be created with New Connection or a connection may be copied. To copy a connection right-click on the connection and select Copy Connection as shown in Figure 38.

d38

Figure 38. Copy Connection

The Copy Connection dialog has all the connection settings copied over from the connection being copied, as shown in Figure 39. Click on Test Connection to test the connection. If the connection gets established the message Connection is OK is displayed. Click on Finish.

d39

Figure 39. Copy Connection

The copied connection is shown in the  Connections view in Figure 40, with a SQL Worksheet associated with it. The tables also get copied to the new connection. Thewlslog table is listed for the copied connection.

d40

Figure 40. Copied Connection

The table data also gets copied. Thewlslog table data is shown in Figure 41.

d41

Figure 41. Table Data is also copied

The Connect in the tool bar displays both the copied and the copied-from connection, as shown in Figure 42.

d42

Figure 42. Copied and Copied-from Connections

Disconnecting, Reconnecting, and Removing a Connection

To disconnect a connection either select Disconnect in the toolbar or right-click on the connection in Connections and select Disconnect as shown in Figure 43.

d43

Figure 43. Disconnect

To connect again right-click on the connection and select Connect as shown in Figure 44.

d44

Figure 44. Connecting Again

In the Enter Password dialog specify Password and click on OK as shown in Figure 45.

d45

Figure 45. Enter Password

A connection may also be connected and disconnected from the Connections drop-down, as shown in Figure 46.

d46

Figure 46. Connections

Disconnect Selectedconnection may also be selected from the  Connectionsview, as shown in Figure 47.

d47

Figure 47. Selecting Disconnect Selected in Connections View

To remove a connection right-click on the connection and select Remove Connection as shown in Figure 48. Only a connection that has first been disconnected may be removed.

d48

Figure 48. Removing a Connection

Dropping Table

To drop a table right-click on the table in Object Explorer and select Drop as shown in Figure 49.

d49

Figure 49. Dropping Table

Click on OK in Confirm dialog as shown in Figure 50.

d50

Figure 50. Confirm>OK

The table gets dropped, as indicated by absence of tables in Figure 51.

d51

Figure 51. Table Dropped

Deleting a Cluster

To delete a Redshift cluster select the cluster in the console and select Cluster>Delete Cluster as shown in Figure 52.

d52

Figure 52. Cluster>Delete Cluster

In the Delete Cluster dialog an option create a snapshot is provided, as shown in Figure 53. To not create a snapshot select No for Create Snapshot and click on Delete.

d53

Figure 53. Delete Cluster

The cluster starts to get deleted as shown by the Cluster Status as deleting in Figure 54.

d54

Figure 54. Cluster getting Deleted

If a snapshot is to be made select Yes for Create Snapshot and specify a snapshot name, and click on Delete as shown in Figure 55.

d55

Figure 55. Creating Snapshot before Deleting

A snapshot gets created, as shown in Figure 56.

d56

Figure 56. Snapshot Created

Only after the snapshot has been created does the cluster start to get deleted, as shown in Figure 57.

d57

Figure 57. Cluster Deleting

A cluster that has been deleted may be restored by selecting Actions>Restore from Snapshot as shown in Figure 58.

d58

Figure 58. Actions>Restore from Snapshot

In the Restore Cluster from Snapshot dialog select the default settings as shown in Figure 59 or modify as needed. Scroll down and click on Restore.

d59

Figure 59. Restore Cluster from Snapshot

The cluster starts to get restored, as shown in Figure 60.

d60

Figure 60. Cluster getting restored

Conclusion

In two articles we have discussed using the Amazon Redshift data warehouse with Toad Edge 2.0.2.  Toad Edge is one of the very few client IDEs for Redshift and provides all the features for Redshift that are available for the open source databases MySQL and PostgreSQL.

 

 

 

About the Author

Deepak Vohra

Deepak Vohra is an Oracle Certified Associate, Oracle Database 10g, and Sun Certified Java Programmer. Deepak has published on OTN and in Oracle Magazine.

Start the discussion at forums.toadworld.com