Amazon Redshift examples using Amazon SDK for .NET (v4) - Amazon SDK for .NET (V4)
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Version 4 (V4) of the Amazon SDK for .NET has been released!

For information about breaking changes and migrating your applications, see the migration topic.

Amazon Redshift examples using Amazon SDK for .NET (v4)

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for .NET (v4) with Amazon Redshift.

Basics are code examples that show you how to perform the essential operations within a service.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Get started

The following code examples show how to get started using Amazon Redshift.

Amazon SDK for .NET (v4)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Main method to run the Hello Amazon Redshift example. /// </summary> /// <param name="args">Command line arguments (not used).</param> public static async Task Main(string[] args) { var redshiftClient = new AmazonRedshiftClient(); Console.WriteLine("Hello, Amazon Redshift! Let's list available clusters:"); var clusters = new List<Cluster>(); try { // Use pagination to retrieve all clusters. var clustersPaginator = redshiftClient.Paginators.DescribeClusters(new DescribeClustersRequest()); await foreach (var response in clustersPaginator.Responses) { if (response.Clusters != null) clusters.AddRange(response.Clusters); } Console.WriteLine($"{clusters.Count} cluster(s) retrieved."); foreach (var cluster in clusters) { Console.WriteLine($"\t{cluster.ClusterIdentifier} (Status: {cluster.ClusterStatus})"); } } catch (AmazonRedshiftException ex) { Console.WriteLine($"Couldn't list clusters. Here's why: {ex.Message}"); } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); } }

Basics

The following code example shows how to:

  • Create a Redshift cluster.

  • List databases in the cluster.

  • Create a table named Movies.

  • Populate the Movies table.

  • Query the Movies table by year.

  • Modify the Redshift cluster.

  • Delete the Amazon Redshift cluster.

Amazon SDK for .NET (v4)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Create a Redshift wrapper class to manage operations.

/// <summary> /// Wrapper class for Amazon Redshift operations. /// </summary> public class RedshiftWrapper { private readonly IAmazonRedshift _redshiftClient; private readonly IAmazonRedshiftDataAPIService _redshiftDataClient; /// <summary> /// Constructor for RedshiftWrapper. /// </summary> /// <param name="redshiftClient">Amazon Redshift client.</param> /// <param name="redshiftDataClient">Amazon Redshift Data API client.</param> public RedshiftWrapper(IAmazonRedshift redshiftClient, IAmazonRedshiftDataAPIService redshiftDataClient) { _redshiftClient = redshiftClient; _redshiftDataClient = redshiftDataClient; } /// <summary> /// Create a new Amazon Redshift cluster. /// </summary> /// <param name="clusterIdentifier">The identifier for the cluster.</param> /// <param name="databaseName">The name of the database.</param> /// <param name="masterUsername">The master username.</param> /// <param name="masterUserPassword">The master user password.</param> /// <param name="nodeType">The node type for the cluster.</param> /// <returns>The cluster that was created.</returns> public async Task<Cluster> CreateClusterAsync(string clusterIdentifier, string databaseName, string masterUsername, string masterUserPassword, string nodeType = "ra3.large") { try { var request = new CreateClusterRequest { ClusterIdentifier = clusterIdentifier, DBName = databaseName, MasterUsername = masterUsername, MasterUserPassword = masterUserPassword, NodeType = nodeType, NumberOfNodes = 1, ClusterType = "single-node" }; var response = await _redshiftClient.CreateClusterAsync(request); Console.WriteLine($"Created cluster {clusterIdentifier}"); return response.Cluster; } catch (ClusterAlreadyExistsException ex) { Console.WriteLine($"Cluster already exists: {ex.Message}"); throw; } catch (Exception ex) { Console.WriteLine($"Couldn't create cluster. Here's why: {ex.Message}"); throw; } } /// <summary> /// Describe Amazon Redshift clusters. /// </summary> /// <param name="clusterIdentifier">Optional cluster identifier to describe a specific cluster.</param> /// <returns>A list of clusters.</returns> public async Task<List<Cluster>> DescribeClustersAsync(string? clusterIdentifier = null) { try { var clusters = new List<Cluster>(); var request = new DescribeClustersRequest(); if (!string.IsNullOrEmpty(clusterIdentifier)) { request.ClusterIdentifier = clusterIdentifier; } var clustersPaginator = _redshiftClient.Paginators.DescribeClusters(request); await foreach (var response in clustersPaginator.Responses) { if (response.Clusters != null) clusters.AddRange(response.Clusters); } Console.WriteLine($"{clusters.Count} cluster(s) retrieved."); foreach (var cluster in clusters) { Console.WriteLine($"\t{cluster.ClusterIdentifier} (Status: {cluster.ClusterStatus})"); } return clusters; } catch (ClusterNotFoundException ex) { Console.WriteLine($"Cluster {clusterIdentifier} not found: {ex.Message}"); throw; } catch (Exception ex) { Console.WriteLine($"Couldn't describe clusters. Here's why: {ex.Message}"); throw; } } /// <summary> /// Modify an Amazon Redshift cluster. /// </summary> /// <param name="clusterIdentifier">The identifier for the cluster.</param> /// <param name="preferredMaintenanceWindow">The preferred maintenance window.</param> /// <returns>True if successful.</returns> public async Task<bool> ModifyClusterAsync(string clusterIdentifier, string preferredMaintenanceWindow) { try { var request = new ModifyClusterRequest { ClusterIdentifier = clusterIdentifier, PreferredMaintenanceWindow = preferredMaintenanceWindow }; var response = await _redshiftClient.ModifyClusterAsync(request); Console.WriteLine($"The modified cluster was successfully modified and has {response.Cluster.PreferredMaintenanceWindow} as the maintenance window"); return true; } catch (ClusterNotFoundException ex) { Console.WriteLine($"Cluster {clusterIdentifier} not found: {ex.Message}"); return false; } catch (Exception ex) { Console.WriteLine($"Couldn't modify cluster. Here's why: {ex.Message}"); return false; } } /// <summary> /// Delete an Amazon Redshift cluster without a final snapshot. /// </summary> /// <param name="clusterIdentifier">The identifier for the cluster.</param> /// <returns>True if successful.</returns> public async Task<bool> DeleteClusterWithoutSnapshotAsync(string clusterIdentifier) { try { var request = new DeleteClusterRequest { ClusterIdentifier = clusterIdentifier, SkipFinalClusterSnapshot = true }; var response = await _redshiftClient.DeleteClusterAsync(request); Console.WriteLine($"The {clusterIdentifier} was deleted"); return true; } catch (ClusterNotFoundException ex) { Console.WriteLine($"Cluster not found: {ex.Message}"); return false; } catch (Exception ex) { Console.WriteLine($"Couldn't delete cluster. Here's why: {ex.Message}"); return false; } } /// <summary> /// List databases in a Redshift cluster. /// </summary> /// <param name="clusterIdentifier">The cluster identifier.</param> /// <param name="dbUser">The database user.</param> /// <param name="dbUser">The database name for authentication.</param> /// <returns>A list of database names.</returns> public async Task<List<string>> ListDatabasesAsync(string clusterIdentifier, string dbUser, string databaseName) { try { var request = new ListDatabasesRequest { ClusterIdentifier = clusterIdentifier, DbUser = dbUser, Database = databaseName }; var response = await _redshiftDataClient.ListDatabasesAsync(request); var databases = new List<string>(); foreach (var database in response.Databases) { Console.WriteLine($"The database name is : {database}"); databases.Add(database); } return databases; } catch (Amazon.RedshiftDataAPIService.Model.ValidationException ex) { Console.WriteLine($"Validation error: {ex.Message}"); throw; } catch (Exception ex) { Console.WriteLine($"Couldn't list databases. Here's why: {ex.Message}"); throw; } } /// <summary> /// Create a table in the Redshift database. /// </summary> /// <param name="clusterIdentifier">The cluster identifier.</param> /// <param name="database">The database name.</param> /// <param name="dbUser">The database user.</param> /// <returns>The statement ID.</returns> public async Task<string> CreateTableAsync(string clusterIdentifier, string database, string dbUser) { try { var sqlStatement = @" CREATE TABLE Movies ( id INTEGER PRIMARY KEY, title VARCHAR(250) NOT NULL, year INTEGER NOT NULL )"; var request = new ExecuteStatementRequest { ClusterIdentifier = clusterIdentifier, Database = database, DbUser = dbUser, Sql = sqlStatement }; var response = await _redshiftDataClient.ExecuteStatementAsync(request); await WaitForStatementToCompleteAsync(response.Id); Console.WriteLine("Table created: Movies"); return response.Id; } catch (Amazon.RedshiftDataAPIService.Model.ValidationException ex) { Console.WriteLine($"Validation error: {ex.Message}"); throw; } catch (Exception ex) { Console.WriteLine($"Couldn't create table. Here's why: {ex.Message}"); throw; } } /// <summary> /// Insert a record into the Movies table using parameterized query. /// </summary> /// <param name="clusterIdentifier">The cluster identifier.</param> /// <param name="database">The database name.</param> /// <param name="dbUser">The database user.</param> /// <param name="id">The movie ID.</param> /// <param name="title">The movie title.</param> /// <param name="year">The movie year.</param> /// <returns>The statement ID.</returns> public async Task<string> InsertMovieAsync(string clusterIdentifier, string database, string dbUser, int id, string title, int year) { try { var sqlStatement = "INSERT INTO Movies (id, title, year) VALUES (:id, :title, :year)"; var request = new ExecuteStatementRequest { ClusterIdentifier = clusterIdentifier, Database = database, DbUser = dbUser, Sql = sqlStatement, Parameters = new List<SqlParameter> { new SqlParameter { Name = "id", Value = id.ToString() }, new SqlParameter { Name = "title", Value = title }, new SqlParameter { Name = "year", Value = year.ToString() } } }; var response = await _redshiftDataClient.ExecuteStatementAsync(request); await WaitForStatementToCompleteAsync(response.Id); Console.WriteLine($"Inserted: {title} ({year})"); return response.Id; } catch (Amazon.RedshiftDataAPIService.Model.ValidationException ex) { Console.WriteLine($"Validation error: {ex.Message}"); throw; } catch (Exception ex) { Console.WriteLine($"Couldn't insert movie. Here's why: {ex.Message}"); throw; } } /// <summary> /// Query movies by year using parameterized query. /// </summary> /// <param name="clusterIdentifier">The cluster identifier.</param> /// <param name="database">The database name.</param> /// <param name="dbUser">The database user.</param> /// <param name="year">The year to query.</param> /// <returns>A list of movie titles.</returns> public async Task<List<string>> QueryMoviesByYearAsync(string clusterIdentifier, string database, string dbUser, int year) { try { var sqlStatement = "SELECT title FROM Movies WHERE year = :year"; var request = new ExecuteStatementRequest { ClusterIdentifier = clusterIdentifier, Database = database, DbUser = dbUser, Sql = sqlStatement, Parameters = new List<SqlParameter> { new SqlParameter { Name = "year", Value = year.ToString() } } }; var response = await _redshiftDataClient.ExecuteStatementAsync(request); Console.WriteLine($"The identifier of the statement is {response.Id}"); await WaitForStatementToCompleteAsync(response.Id); var results = await GetStatementResultAsync(response.Id); var movieTitles = new List<string>(); foreach (var row in results) { if (row.Count > 0) { var title = row[0].StringValue; Console.WriteLine($"The Movie title field is {title}"); movieTitles.Add(title); } } return movieTitles; } catch (Amazon.RedshiftDataAPIService.Model.ValidationException ex) { Console.WriteLine($"Validation error: {ex.Message}"); throw; } catch (Exception ex) { Console.WriteLine($"Couldn't query movies. Here's why: {ex.Message}"); throw; } } /// <summary> /// Describe a statement execution. /// </summary> /// <param name="statementId">The statement ID.</param> /// <returns>The statement description.</returns> public async Task<DescribeStatementResponse> DescribeStatementAsync(string statementId) { try { var request = new DescribeStatementRequest { Id = statementId }; var response = await _redshiftDataClient.DescribeStatementAsync(request); return response; } catch (Amazon.RedshiftDataAPIService.Model.ResourceNotFoundException ex) { Console.WriteLine($"Statement not found: {ex.Message}"); throw; } catch (Exception ex) { Console.WriteLine($"Couldn't describe statement. Here's why: {ex.Message}"); throw; } } /// <summary> /// Get the results of a statement execution. /// </summary> /// <param name="statementId">The statement ID.</param> /// <returns>A list of result rows.</returns> public async Task<List<List<Field>>> GetStatementResultAsync(string statementId) { try { var request = new GetStatementResultRequest { Id = statementId }; var response = await _redshiftDataClient.GetStatementResultAsync(request); return response.Records; } catch (Amazon.RedshiftDataAPIService.Model.ResourceNotFoundException ex) { Console.WriteLine($"Statement not found: {ex.Message}"); throw; } catch (Exception ex) { Console.WriteLine($"Couldn't get statement result. Here's why: {ex.Message}"); throw; } } /// <summary> /// Wait for a statement to complete execution. /// </summary> /// <param name="statementId">The statement ID.</param> /// <returns>A task representing the asynchronous operation.</returns> private async Task WaitForStatementToCompleteAsync(string statementId) { var status = StatusString.SUBMITTED; DescribeStatementResponse? response = null; while (status == StatusString.SUBMITTED || status == StatusString.PICKED || status == StatusString.STARTED) { await Task.Delay(1000); // Wait 1 second response = await DescribeStatementAsync(statementId); status = response.Status; Console.WriteLine($"...{status}"); } if (status == StatusString.FINISHED) { Console.WriteLine("The statement is finished!"); } else { var errorMessage = response?.Error ?? "Unknown error"; Console.WriteLine($"The statement failed with status: {status}"); Console.WriteLine($"Error message: {errorMessage}"); } } /// <summary> /// Wait for a cluster to become available. /// </summary> /// <param name="clusterIdentifier">The cluster identifier.</param> /// <returns>A task representing the asynchronous operation.</returns> public async Task WaitForClusterAvailableAsync(string clusterIdentifier) { Console.WriteLine($"Wait until {clusterIdentifier} is available. This may take a few minutes."); var startTime = DateTime.Now; var clusters = await DescribeClustersAsync(clusterIdentifier); while (clusters[0].ClusterStatus != "available") { var elapsed = DateTime.Now - startTime; Console.WriteLine($"Elapsed Time: {elapsed:mm\\:ss} - Waiting for cluster..."); await Task.Delay(5000); // Wait 5 seconds clusters = await DescribeClustersAsync(clusterIdentifier); } var totalElapsed = DateTime.Now - startTime; Console.WriteLine($"Cluster is available! Total Elapsed Time: {totalElapsed:mm\\:ss}"); } }

Run an interactive scenario demonstrating Redshift basics.

/// <summary> /// Amazon Redshift Getting Started Scenario. /// </summary> public class RedshiftBasics { public static bool IsInteractive = true; public static RedshiftWrapper? Wrapper = null; public static ILogger logger = null!; private static readonly string _moviesFilePath = "../../../../../../resources/sample_files/movies.json"; /// <summary> /// Main method for the Amazon Redshift Getting Started scenario. /// </summary> /// <param name="args">Command line arguments.</param> public static async Task Main(string[] args) { using var host = Host.CreateDefaultBuilder(args) .ConfigureServices((_, services) => services.AddAWSService<IAmazonRedshift>() .AddAWSService<IAmazonRedshiftDataAPIService>() .AddTransient<RedshiftWrapper>() ) .Build(); logger = LoggerFactory.Create(builder => { builder.AddConsole(); }) .CreateLogger<RedshiftBasics>(); Wrapper = host.Services.GetRequiredService<RedshiftWrapper>(); await RunScenarioAsync(); } /// <summary> /// Run the complete Amazon Redshift scenario. /// </summary> public static async Task RunScenarioAsync() { // Set all variables to default values string userName = "awsuser"; string userPassword = "AwsUser1000"; string clusterIdentifier = "redshift-cluster-movies"; var databaseName = "dev"; int recordCount = 50; int year = 2013; try { Console.WriteLine( "================================================================================"); Console.WriteLine("Welcome to the Amazon Redshift SDK Getting Started scenario."); Console.WriteLine( "This .NET program demonstrates how to interact with Amazon Redshift by using the AWS SDK for .NET."); Console.WriteLine("Let's get started..."); Console.WriteLine( "================================================================================"); // Step 1: Get user credentials (if interactive) if (IsInteractive) { Console.WriteLine("Please enter a user name for the cluster (default is awsuser):"); var userInput = Console.ReadLine(); if (!string.IsNullOrEmpty(userInput)) userName = userInput; Console.WriteLine("================================================================================"); Console.WriteLine("Please enter a user password for the cluster (default is AwsUser1000):"); var passwordInput = Console.ReadLine(); if (!string.IsNullOrEmpty(passwordInput)) userPassword = passwordInput; Console.WriteLine("================================================================================"); // Step 2: Get cluster identifier Console.WriteLine("Enter a cluster id value (default is redshift-cluster-movies):"); var clusterInput = Console.ReadLine(); if (!string.IsNullOrEmpty(clusterInput)) clusterIdentifier = clusterInput; } else { Console.WriteLine($"Using default values: userName={userName}, clusterIdentifier={clusterIdentifier}"); } // Step 3: Create Redshift cluster await Wrapper!.CreateClusterAsync(clusterIdentifier, databaseName, userName, userPassword); Console.WriteLine("================================================================================"); // Step 4: Wait for cluster to become available Console.WriteLine("================================================================================"); await Wrapper.WaitForClusterAvailableAsync(clusterIdentifier); Console.WriteLine("================================================================================"); // Step 5: List databases Console.WriteLine("================================================================================"); Console.WriteLine($" When you created {clusterIdentifier}, the dev database is created by default and used in this scenario."); Console.WriteLine(" To create a custom database, you need to have a CREATEDB privilege."); Console.WriteLine(" For more information, see the documentation here: https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_DATABASE.html."); if (IsInteractive) { Console.WriteLine("Press Enter to continue..."); Console.ReadLine(); } Console.WriteLine("================================================================================"); Console.WriteLine("================================================================================"); Console.WriteLine($"List databases in {clusterIdentifier}"); if (IsInteractive) { Console.WriteLine("Press Enter to continue..."); Console.ReadLine(); } await Wrapper.ListDatabasesAsync(clusterIdentifier, userName, databaseName); Console.WriteLine("================================================================================"); // Step 6: Create Movies table Console.WriteLine("================================================================================"); Console.WriteLine("Now you will create a table named Movies."); if (IsInteractive) { Console.WriteLine("Press Enter to continue..."); Console.ReadLine(); } await Wrapper.CreateTableAsync(clusterIdentifier, databaseName, userName); Console.WriteLine("================================================================================"); // Step 7: Populate the Movies table Console.WriteLine("================================================================================"); Console.WriteLine("Populate the Movies table using the Movies.json file."); if (IsInteractive) { Console.WriteLine("Specify the number of records you would like to add to the Movies Table."); Console.WriteLine("Please enter a value between 50 and 200."); Console.Write("Enter a value: "); var recordCountInput = Console.ReadLine(); if (int.TryParse(recordCountInput, out var inputCount) && inputCount is >= 50 and <= 200) { recordCount = inputCount; } else { Console.WriteLine($"Invalid input. Using default value of {recordCount}."); } } else { Console.WriteLine($"Using default record count: {recordCount}"); } await PopulateMoviesTableAsync(clusterIdentifier, databaseName, userName, recordCount); Console.WriteLine($"{recordCount} records were added to the Movies table."); Console.WriteLine("================================================================================"); // Step 8 & 9: Query movies by year Console.WriteLine("================================================================================"); Console.WriteLine("Query the Movies table by year. Enter a value between 2012-2014."); if (IsInteractive) { Console.Write("Enter a year: "); var yearInput = Console.ReadLine(); if (int.TryParse(yearInput, out var inputYear) && inputYear is >= 2012 and <= 2014) { year = inputYear; } else { Console.WriteLine($"Invalid input. Using default value of {year}."); } } else { Console.WriteLine($"Using default year: {year}"); } await Wrapper.QueryMoviesByYearAsync(clusterIdentifier, databaseName, userName, year); Console.WriteLine("================================================================================"); // Step 10: Modify the cluster Console.WriteLine("================================================================================"); Console.WriteLine("Now you will modify the Redshift cluster."); if (IsInteractive) { Console.WriteLine("Press Enter to continue..."); Console.ReadLine(); } await Wrapper.ModifyClusterAsync(clusterIdentifier, "wed:07:30-wed:08:00"); Console.WriteLine("================================================================================"); // Step 11 & 12: Delete cluster confirmation Console.WriteLine("================================================================================"); if (IsInteractive) { Console.WriteLine("Would you like to delete the Amazon Redshift cluster? (y/n)"); var deleteResponse = Console.ReadLine(); if (deleteResponse?.ToLower() == "y") { await Wrapper.DeleteClusterWithoutSnapshotAsync(clusterIdentifier); } } else { Console.WriteLine("Deleting the Amazon Redshift cluster..."); await Wrapper.DeleteClusterWithoutSnapshotAsync(clusterIdentifier); } Console.WriteLine("================================================================================"); Console.WriteLine("================================================================================"); Console.WriteLine("This concludes the Amazon Redshift SDK Getting Started scenario."); Console.WriteLine("================================================================================"); } catch (Exception ex) { Console.WriteLine($"An error occurred during the scenario: {ex.Message}"); Console.WriteLine("Deleting the Amazon Redshift cluster..."); await Wrapper!.DeleteClusterWithoutSnapshotAsync(clusterIdentifier); throw; } } /// <summary> /// Populate the Movies table with data from the JSON file. /// </summary> /// <param name="clusterIdentifier">The cluster identifier.</param> /// <param name="database">The database name.</param> /// <param name="dbUser">The database user.</param> /// <param name="recordCount">Number of records to insert.</param> private static async Task PopulateMoviesTableAsync(string clusterIdentifier, string database, string dbUser, int recordCount) { if (!File.Exists(_moviesFilePath)) { throw new FileNotFoundException($"Required movies data file not found at: {_moviesFilePath}"); } var jsonContent = await File.ReadAllTextAsync(_moviesFilePath); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; var movies = JsonSerializer.Deserialize<List<Movie>>(jsonContent, options); if (movies == null || movies.Count == 0) { throw new InvalidOperationException("Failed to parse movies JSON file or file is empty."); } var insertCount = Math.Min(recordCount, movies.Count); for (int i = 0; i < insertCount; i++) { var movie = movies[i]; await Wrapper!.InsertMovieAsync(clusterIdentifier, database, dbUser, i, movie.Title, movie.Year); } } /// <summary> /// Movie data model. /// </summary> private class Movie { public string Title { get; set; } = string.Empty; public int Year { get; set; } } }

Actions

The following code example shows how to use CreateCluster.

Amazon SDK for .NET (v4)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Create a new Amazon Redshift cluster. /// </summary> /// <param name="clusterIdentifier">The identifier for the cluster.</param> /// <param name="databaseName">The name of the database.</param> /// <param name="masterUsername">The master username.</param> /// <param name="masterUserPassword">The master user password.</param> /// <param name="nodeType">The node type for the cluster.</param> /// <returns>The cluster that was created.</returns> public async Task<Cluster> CreateClusterAsync(string clusterIdentifier, string databaseName, string masterUsername, string masterUserPassword, string nodeType = "ra3.large") { try { var request = new CreateClusterRequest { ClusterIdentifier = clusterIdentifier, DBName = databaseName, MasterUsername = masterUsername, MasterUserPassword = masterUserPassword, NodeType = nodeType, NumberOfNodes = 1, ClusterType = "single-node" }; var response = await _redshiftClient.CreateClusterAsync(request); Console.WriteLine($"Created cluster {clusterIdentifier}"); return response.Cluster; } catch (ClusterAlreadyExistsException ex) { Console.WriteLine($"Cluster already exists: {ex.Message}"); throw; } catch (Exception ex) { Console.WriteLine($"Couldn't create cluster. Here's why: {ex.Message}"); throw; } }
  • For API details, see CreateCluster in Amazon SDK for .NET API Reference.

The following code example shows how to use DeleteCluster.

Amazon SDK for .NET (v4)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Delete an Amazon Redshift cluster without a final snapshot. /// </summary> /// <param name="clusterIdentifier">The identifier for the cluster.</param> /// <returns>True if successful.</returns> public async Task<bool> DeleteClusterWithoutSnapshotAsync(string clusterIdentifier) { try { var request = new DeleteClusterRequest { ClusterIdentifier = clusterIdentifier, SkipFinalClusterSnapshot = true }; var response = await _redshiftClient.DeleteClusterAsync(request); Console.WriteLine($"The {clusterIdentifier} was deleted"); return true; } catch (ClusterNotFoundException ex) { Console.WriteLine($"Cluster not found: {ex.Message}"); return false; } catch (Exception ex) { Console.WriteLine($"Couldn't delete cluster. Here's why: {ex.Message}"); return false; } }
  • For API details, see DeleteCluster in Amazon SDK for .NET API Reference.

The following code example shows how to use DescribeClusters.

Amazon SDK for .NET (v4)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Describe Amazon Redshift clusters. /// </summary> /// <param name="clusterIdentifier">Optional cluster identifier to describe a specific cluster.</param> /// <returns>A list of clusters.</returns> public async Task<List<Cluster>> DescribeClustersAsync(string? clusterIdentifier = null) { try { var clusters = new List<Cluster>(); var request = new DescribeClustersRequest(); if (!string.IsNullOrEmpty(clusterIdentifier)) { request.ClusterIdentifier = clusterIdentifier; } var clustersPaginator = _redshiftClient.Paginators.DescribeClusters(request); await foreach (var response in clustersPaginator.Responses) { if (response.Clusters != null) clusters.AddRange(response.Clusters); } Console.WriteLine($"{clusters.Count} cluster(s) retrieved."); foreach (var cluster in clusters) { Console.WriteLine($"\t{cluster.ClusterIdentifier} (Status: {cluster.ClusterStatus})"); } return clusters; } catch (ClusterNotFoundException ex) { Console.WriteLine($"Cluster {clusterIdentifier} not found: {ex.Message}"); throw; } catch (Exception ex) { Console.WriteLine($"Couldn't describe clusters. Here's why: {ex.Message}"); throw; } }

The following code example shows how to use DescribeStatement.

Amazon SDK for .NET (v4)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Describe a statement execution. /// </summary> /// <param name="statementId">The statement ID.</param> /// <returns>The statement description.</returns> public async Task<DescribeStatementResponse> DescribeStatementAsync(string statementId) { try { var request = new DescribeStatementRequest { Id = statementId }; var response = await _redshiftDataClient.DescribeStatementAsync(request); return response; } catch (Amazon.RedshiftDataAPIService.Model.ResourceNotFoundException ex) { Console.WriteLine($"Statement not found: {ex.Message}"); throw; } catch (Exception ex) { Console.WriteLine($"Couldn't describe statement. Here's why: {ex.Message}"); throw; } }

The following code example shows how to use GetStatementResult.

Amazon SDK for .NET (v4)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Get the results of a statement execution. /// </summary> /// <param name="statementId">The statement ID.</param> /// <returns>A list of result rows.</returns> public async Task<List<List<Field>>> GetStatementResultAsync(string statementId) { try { var request = new GetStatementResultRequest { Id = statementId }; var response = await _redshiftDataClient.GetStatementResultAsync(request); return response.Records; } catch (Amazon.RedshiftDataAPIService.Model.ResourceNotFoundException ex) { Console.WriteLine($"Statement not found: {ex.Message}"); throw; } catch (Exception ex) { Console.WriteLine($"Couldn't get statement result. Here's why: {ex.Message}"); throw; } }

The following code example shows how to use ListDatabases.

Amazon SDK for .NET (v4)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// List databases in a Redshift cluster. /// </summary> /// <param name="clusterIdentifier">The cluster identifier.</param> /// <param name="dbUser">The database user.</param> /// <param name="dbUser">The database name for authentication.</param> /// <returns>A list of database names.</returns> public async Task<List<string>> ListDatabasesAsync(string clusterIdentifier, string dbUser, string databaseName) { try { var request = new ListDatabasesRequest { ClusterIdentifier = clusterIdentifier, DbUser = dbUser, Database = databaseName }; var response = await _redshiftDataClient.ListDatabasesAsync(request); var databases = new List<string>(); foreach (var database in response.Databases) { Console.WriteLine($"The database name is : {database}"); databases.Add(database); } return databases; } catch (Amazon.RedshiftDataAPIService.Model.ValidationException ex) { Console.WriteLine($"Validation error: {ex.Message}"); throw; } catch (Exception ex) { Console.WriteLine($"Couldn't list databases. Here's why: {ex.Message}"); throw; } }
  • For API details, see ListDatabases in Amazon SDK for .NET API Reference.

The following code example shows how to use ModifyCluster.

Amazon SDK for .NET (v4)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

/// <summary> /// Modify an Amazon Redshift cluster. /// </summary> /// <param name="clusterIdentifier">The identifier for the cluster.</param> /// <param name="preferredMaintenanceWindow">The preferred maintenance window.</param> /// <returns>True if successful.</returns> public async Task<bool> ModifyClusterAsync(string clusterIdentifier, string preferredMaintenanceWindow) { try { var request = new ModifyClusterRequest { ClusterIdentifier = clusterIdentifier, PreferredMaintenanceWindow = preferredMaintenanceWindow }; var response = await _redshiftClient.ModifyClusterAsync(request); Console.WriteLine($"The modified cluster was successfully modified and has {response.Cluster.PreferredMaintenanceWindow} as the maintenance window"); return true; } catch (ClusterNotFoundException ex) { Console.WriteLine($"Cluster {clusterIdentifier} not found: {ex.Message}"); return false; } catch (Exception ex) { Console.WriteLine($"Couldn't modify cluster. Here's why: {ex.Message}"); return false; } }
  • For API details, see ModifyCluster in Amazon SDK for .NET API Reference.