를 사용한 Amazon Keyspaces 예제 AWS SDK for .NET - AWS SDK 코드 예제

AWS 문서 예제 리포지토리에서 더 많은 SDK GitHub AWS SDK 예제를 사용할 수 있습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

를 사용한 Amazon Keyspaces 예제 AWS SDK for .NET

다음 코드 예제에서는 Amazon Keyspaces와 AWS SDK for .NET 함께 를 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.

기본 사항은 서비스 내에서 필수 작업을 수행하는 방법을 보여주는 코드 예제입니다.

작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 개별 서비스 함수를 직접적으로 호출하는 방법을 보여주며 관련 시나리오의 컨텍스트에 맞는 작업을 볼 수 있습니다.

각 예제에는 컨텍스트에서 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있는 전체 소스 코드에 대한 링크가 포함되어 있습니다.

시작하기

다음 코드 예시에서는 Amazon Keyspaces를 시작하는 방법을 보여줍니다.

AWS SDK for .NET
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

namespace KeyspacesActions; public class HelloKeyspaces { private static ILogger logger = null!; static async Task Main(string[] args) { // Set up dependency injection for Amazon Keyspaces (for Apache Cassandra). using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Debug) .AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information) .AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace)) .ConfigureServices((_, services) => services.AddAWSService<IAmazonKeyspaces>() .AddTransient<KeyspacesWrapper>() ) .Build(); logger = LoggerFactory.Create(builder => { builder.AddConsole(); }) .CreateLogger<HelloKeyspaces>(); var keyspacesClient = host.Services.GetRequiredService<IAmazonKeyspaces>(); var keyspacesWrapper = new KeyspacesWrapper(keyspacesClient); Console.WriteLine("Hello, Amazon Keyspaces! Let's list your keyspaces:"); await keyspacesWrapper.ListKeyspaces(); } }
  • 자세한 API 내용은 참조ListKeyspaces의 섹션을 참조하세요. AWS SDK for .NET API

기본 사항

다음 코드 예시는 다음과 같은 작업을 수행하는 방법을 보여줍니다.

  • 키스페이스와 테이블을 생성하세요. 테이블 스키마에는 영화 데이터가 저장되고 복구가 point-in-time 활성화되어 있습니다.

  • SigV4 인증으로 보안 TLS 연결을 사용하여 키스페이스에 연결합니다.

  • 테이블을 쿼리합니다. 영화 데이터를 추가, 검색 및 업데이트합니다.

  • 테이블을 업데이트 하세요. 열을 추가하여 시청한 영화를 추적합니다.

  • 테이블을 이전 상태로 복원하고 리소스를 정리합니다.

AWS SDK for .NET
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

global using System.Security.Cryptography.X509Certificates; global using Amazon.Keyspaces; global using Amazon.Keyspaces.Model; global using KeyspacesActions; global using KeyspacesScenario; global using Microsoft.Extensions.Configuration; global using Microsoft.Extensions.DependencyInjection; global using Microsoft.Extensions.Hosting; global using Microsoft.Extensions.Logging; global using Microsoft.Extensions.Logging.Console; global using Microsoft.Extensions.Logging.Debug; global using Newtonsoft.Json; namespace KeyspacesBasics; /// <summary> /// Amazon Keyspaces (for Apache Cassandra) scenario. Shows some of the basic /// actions performed with Amazon Keyspaces. /// </summary> public class KeyspacesBasics { private static ILogger logger = null!; static async Task Main(string[] args) { // Set up dependency injection for the Amazon service. using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Debug) .AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information) .AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace)) .ConfigureServices((_, services) => services.AddAWSService<IAmazonKeyspaces>() .AddTransient<KeyspacesWrapper>() .AddTransient<CassandraWrapper>() ) .Build(); logger = LoggerFactory.Create(builder => { builder.AddConsole(); }) .CreateLogger<KeyspacesBasics>(); var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("settings.json") // Load test settings from .json file. .AddJsonFile("settings.local.json", true) // Optionally load local settings. .Build(); var keyspacesWrapper = host.Services.GetRequiredService<KeyspacesWrapper>(); var uiMethods = new UiMethods(); var keyspaceName = configuration["KeyspaceName"]; var tableName = configuration["TableName"]; bool success; // Used to track the results of some operations. uiMethods.DisplayOverview(); uiMethods.PressEnter(); // Create the keyspace. var keyspaceArn = await keyspacesWrapper.CreateKeyspace(keyspaceName); // Wait for the keyspace to be available. GetKeyspace results in a // resource not found error until it is ready for use. try { var getKeyspaceArn = ""; Console.Write($"Created {keyspaceName}. Waiting for it to become available. "); do { getKeyspaceArn = await keyspacesWrapper.GetKeyspace(keyspaceName); Console.Write(". "); } while (getKeyspaceArn != keyspaceArn); } catch (ResourceNotFoundException) { Console.WriteLine("Waiting for keyspace to be created."); } Console.WriteLine($"\nThe keyspace {keyspaceName} is ready for use."); uiMethods.PressEnter(); // Create the table. // First define the schema. var allColumns = new List<ColumnDefinition> { new ColumnDefinition { Name = "title", Type = "text" }, new ColumnDefinition { Name = "year", Type = "int" }, new ColumnDefinition { Name = "release_date", Type = "timestamp" }, new ColumnDefinition { Name = "plot", Type = "text" }, }; var partitionKeys = new List<PartitionKey> { new PartitionKey { Name = "year", }, new PartitionKey { Name = "title" }, }; var tableSchema = new SchemaDefinition { AllColumns = allColumns, PartitionKeys = partitionKeys, }; var tableArn = await keyspacesWrapper.CreateTable(keyspaceName, tableSchema, tableName); // Wait for the table to be active. try { var resp = new GetTableResponse(); Console.Write("Waiting for the new table to be active. "); do { try { resp = await keyspacesWrapper.GetTable(keyspaceName, tableName); Console.Write("."); } catch (ResourceNotFoundException) { Console.Write("."); } } while (resp.Status != TableStatus.ACTIVE); // Display the table's schema. Console.WriteLine($"\nTable {tableName} has been created in {keyspaceName}"); Console.WriteLine("Let's take a look at the schema."); uiMethods.DisplayTitle("All columns"); resp.SchemaDefinition.AllColumns.ForEach(column => { Console.WriteLine($"{column.Name,-40}\t{column.Type,-20}"); }); uiMethods.DisplayTitle("Cluster keys"); resp.SchemaDefinition.ClusteringKeys.ForEach(clusterKey => { Console.WriteLine($"{clusterKey.Name,-40}\t{clusterKey.OrderBy,-20}"); }); uiMethods.DisplayTitle("Partition keys"); resp.SchemaDefinition.PartitionKeys.ForEach(partitionKey => { Console.WriteLine($"{partitionKey.Name}"); }); uiMethods.PressEnter(); } catch (ResourceNotFoundException ex) { Console.WriteLine($"Error: {ex.Message}"); } // Access Apache Cassandra using the Cassandra drive for C#. var cassandraWrapper = host.Services.GetRequiredService<CassandraWrapper>(); var movieFilePath = configuration["MovieFile"]; Console.WriteLine("Let's add some movies to the table we created."); var inserted = await cassandraWrapper.InsertIntoMovieTable(keyspaceName, tableName, movieFilePath); uiMethods.PressEnter(); Console.WriteLine("Added the following movies to the table:"); var rows = await cassandraWrapper.GetMovies(keyspaceName, tableName); uiMethods.DisplayTitle("All Movies"); foreach (var row in rows) { var title = row.GetValue<string>("title"); var year = row.GetValue<int>("year"); var plot = row.GetValue<string>("plot"); var release_date = row.GetValue<DateTime>("release_date"); Console.WriteLine($"{release_date}\t{title}\t{year}\n{plot}"); Console.WriteLine(uiMethods.SepBar); } // Update the table schema uiMethods.DisplayTitle("Update table schema"); Console.WriteLine("Now we will update the table to add a boolean field called watched."); // First save the current time as a UTC Date so the original // table can be restored later. var timeChanged = DateTime.UtcNow; // Now update the schema. var resourceArn = await keyspacesWrapper.UpdateTable(keyspaceName, tableName); uiMethods.PressEnter(); Console.WriteLine("Now let's mark some of the movies as watched."); // Pick some files to mark as watched. var movieToWatch = rows[2].GetValue<string>("title"); var watchedMovieYear = rows[2].GetValue<int>("year"); var changedRows = await cassandraWrapper.MarkMovieAsWatched(keyspaceName, tableName, movieToWatch, watchedMovieYear); movieToWatch = rows[6].GetValue<string>("title"); watchedMovieYear = rows[6].GetValue<int>("year"); changedRows = await cassandraWrapper.MarkMovieAsWatched(keyspaceName, tableName, movieToWatch, watchedMovieYear); movieToWatch = rows[9].GetValue<string>("title"); watchedMovieYear = rows[9].GetValue<int>("year"); changedRows = await cassandraWrapper.MarkMovieAsWatched(keyspaceName, tableName, movieToWatch, watchedMovieYear); movieToWatch = rows[10].GetValue<string>("title"); watchedMovieYear = rows[10].GetValue<int>("year"); changedRows = await cassandraWrapper.MarkMovieAsWatched(keyspaceName, tableName, movieToWatch, watchedMovieYear); movieToWatch = rows[13].GetValue<string>("title"); watchedMovieYear = rows[13].GetValue<int>("year"); changedRows = await cassandraWrapper.MarkMovieAsWatched(keyspaceName, tableName, movieToWatch, watchedMovieYear); uiMethods.DisplayTitle("Watched movies"); Console.WriteLine("These movies have been marked as watched:"); rows = await cassandraWrapper.GetWatchedMovies(keyspaceName, tableName); foreach (var row in rows) { var title = row.GetValue<string>("title"); var year = row.GetValue<int>("year"); Console.WriteLine($"{title,-40}\t{year,8}"); } uiMethods.PressEnter(); Console.WriteLine("We can restore the table to its previous state but that can take up to 20 minutes to complete."); string answer; do { Console.WriteLine("Do you want to restore the table? (y/n)"); answer = Console.ReadLine(); } while (answer.ToLower() != "y" && answer.ToLower() != "n"); if (answer == "y") { var restoredTableName = $"{tableName}_restored"; var restoredTableArn = await keyspacesWrapper.RestoreTable( keyspaceName, tableName, restoredTableName, timeChanged); // Loop and call GetTable until the table is gone. Once it has been // deleted completely, GetTable will raise a ResourceNotFoundException. bool wasRestored = false; try { do { var resp = await keyspacesWrapper.GetTable(keyspaceName, restoredTableName); wasRestored = (resp.Status == TableStatus.ACTIVE); } while (!wasRestored); } catch (ResourceNotFoundException) { // If the restored table raised an error, it isn't // ready yet. Console.Write("."); } } uiMethods.DisplayTitle("Clean up resources."); // Delete the table. success = await keyspacesWrapper.DeleteTable(keyspaceName, tableName); Console.WriteLine($"Table {tableName} successfully deleted from {keyspaceName}."); Console.WriteLine("Waiting for the table to be removed completely. "); // Loop and call GetTable until the table is gone. Once it has been // deleted completely, GetTable will raise a ResourceNotFoundException. bool wasDeleted = false; try { do { var resp = await keyspacesWrapper.GetTable(keyspaceName, tableName); } while (!wasDeleted); } catch (ResourceNotFoundException ex) { wasDeleted = true; Console.WriteLine($"{ex.Message} indicates that the table has been deleted."); } // Delete the keyspace. success = await keyspacesWrapper.DeleteKeyspace(keyspaceName); Console.WriteLine("The keyspace has been deleted and the demo is now complete."); } }
namespace KeyspacesActions; /// <summary> /// Performs Amazon Keyspaces (for Apache Cassandra) actions. /// </summary> public class KeyspacesWrapper { private readonly IAmazonKeyspaces _amazonKeyspaces; /// <summary> /// Constructor for the KeyspaceWrapper. /// </summary> /// <param name="amazonKeyspaces">An Amazon Keyspaces client object.</param> public KeyspacesWrapper(IAmazonKeyspaces amazonKeyspaces) { _amazonKeyspaces = amazonKeyspaces; } /// <summary> /// Create a new keyspace. /// </summary> /// <param name="keyspaceName">The name for the new keyspace.</param> /// <returns>The Amazon Resource Name (ARN) of the new keyspace.</returns> public async Task<string> CreateKeyspace(string keyspaceName) { var response = await _amazonKeyspaces.CreateKeyspaceAsync( new CreateKeyspaceRequest { KeyspaceName = keyspaceName }); return response.ResourceArn; } /// <summary> /// Create a new Amazon Keyspaces table. /// </summary> /// <param name="keyspaceName">The keyspace where the table will be created.</param> /// <param name="schema">The schema for the new table.</param> /// <param name="tableName">The name of the new table.</param> /// <returns>The Amazon Resource Name (ARN) of the new table.</returns> public async Task<string> CreateTable(string keyspaceName, SchemaDefinition schema, string tableName) { var request = new CreateTableRequest { KeyspaceName = keyspaceName, SchemaDefinition = schema, TableName = tableName, PointInTimeRecovery = new PointInTimeRecovery { Status = PointInTimeRecoveryStatus.ENABLED } }; var response = await _amazonKeyspaces.CreateTableAsync(request); return response.ResourceArn; } /// <summary> /// Delete an existing keyspace. /// </summary> /// <param name="keyspaceName"></param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> DeleteKeyspace(string keyspaceName) { var response = await _amazonKeyspaces.DeleteKeyspaceAsync( new DeleteKeyspaceRequest { KeyspaceName = keyspaceName }); return response.HttpStatusCode == HttpStatusCode.OK; } /// <summary> /// Delete an Amazon Keyspaces table. /// </summary> /// <param name="keyspaceName">The keyspace containing the table.</param> /// <param name="tableName">The name of the table to delete.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> DeleteTable(string keyspaceName, string tableName) { var response = await _amazonKeyspaces.DeleteTableAsync( new DeleteTableRequest { KeyspaceName = keyspaceName, TableName = tableName }); return response.HttpStatusCode == HttpStatusCode.OK; } /// <summary> /// Get data about a keyspace. /// </summary> /// <param name="keyspaceName">The name of the keyspace.</param> /// <returns>The Amazon Resource Name (ARN) of the keyspace.</returns> public async Task<string> GetKeyspace(string keyspaceName) { var response = await _amazonKeyspaces.GetKeyspaceAsync( new GetKeyspaceRequest { KeyspaceName = keyspaceName }); return response.ResourceArn; } /// <summary> /// Get information about an Amazon Keyspaces table. /// </summary> /// <param name="keyspaceName">The keyspace containing the table.</param> /// <param name="tableName">The name of the Amazon Keyspaces table.</param> /// <returns>The response containing data about the table.</returns> public async Task<GetTableResponse> GetTable(string keyspaceName, string tableName) { var response = await _amazonKeyspaces.GetTableAsync( new GetTableRequest { KeyspaceName = keyspaceName, TableName = tableName }); return response; } /// <summary> /// Lists all keyspaces for the account. /// </summary> /// <returns>Async task.</returns> public async Task ListKeyspaces() { var paginator = _amazonKeyspaces.Paginators.ListKeyspaces(new ListKeyspacesRequest()); Console.WriteLine("{0, -30}\t{1}", "Keyspace name", "Keyspace ARN"); Console.WriteLine(new string('-', Console.WindowWidth)); await foreach (var keyspace in paginator.Keyspaces) { Console.WriteLine($"{keyspace.KeyspaceName,-30}\t{keyspace.ResourceArn}"); } } /// <summary> /// Lists the Amazon Keyspaces tables in a keyspace. /// </summary> /// <param name="keyspaceName">The name of the keyspace.</param> /// <returns>A list of TableSummary objects.</returns> public async Task<List<TableSummary>> ListTables(string keyspaceName) { var response = await _amazonKeyspaces.ListTablesAsync(new ListTablesRequest { KeyspaceName = keyspaceName }); response.Tables.ForEach(table => { Console.WriteLine($"{table.KeyspaceName}\t{table.TableName}\t{table.ResourceArn}"); }); return response.Tables; } /// <summary> /// Restores the specified table to the specified point in time. /// </summary> /// <param name="keyspaceName">The keyspace containing the table.</param> /// <param name="tableName">The name of the table to restore.</param> /// <param name="timestamp">The time to which the table will be restored.</param> /// <returns>The Amazon Resource Name (ARN) of the restored table.</returns> public async Task<string> RestoreTable(string keyspaceName, string tableName, string restoredTableName, DateTime timestamp) { var request = new RestoreTableRequest { RestoreTimestamp = timestamp, SourceKeyspaceName = keyspaceName, SourceTableName = tableName, TargetKeyspaceName = keyspaceName, TargetTableName = restoredTableName }; var response = await _amazonKeyspaces.RestoreTableAsync(request); return response.RestoredTableARN; } /// <summary> /// Updates the movie table to add a boolean column named watched. /// </summary> /// <param name="keyspaceName">The keyspace containing the table.</param> /// <param name="tableName">The name of the table to change.</param> /// <returns>The Amazon Resource Name (ARN) of the updated table.</returns> public async Task<string> UpdateTable(string keyspaceName, string tableName) { var newColumn = new ColumnDefinition { Name = "watched", Type = "boolean" }; var request = new UpdateTableRequest { KeyspaceName = keyspaceName, TableName = tableName, AddColumns = new List<ColumnDefinition> { newColumn } }; var response = await _amazonKeyspaces.UpdateTableAsync(request); return response.ResourceArn; } }
using System.Net; using Cassandra; namespace KeyspacesScenario; /// <summary> /// Class to perform CRUD methods on an Amazon Keyspaces (for Apache Cassandra) database. /// /// NOTE: This sample uses a plain text authenticator for example purposes only. /// Recommended best practice is to use a SigV4 authentication plugin, if available. /// </summary> public class CassandraWrapper { private readonly IConfiguration _configuration; private readonly string _localPathToFile; private const string _certLocation = "https://certs.secureserver.net/repository/sf-class2-root.crt"; private const string _certFileName = "sf-class2-root.crt"; private readonly X509Certificate2Collection _certCollection; private X509Certificate2 _amazoncert; private Cluster _cluster; // User name and password for the service. private string _userName = null!; private string _pwd = null!; public CassandraWrapper() { _configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("settings.json") // Load test settings from .json file. .AddJsonFile("settings.local.json", true) // Optionally load local settings. .Build(); _localPathToFile = Path.GetTempPath(); // Get the Starfield digital certificate and save it locally. var client = new WebClient(); client.DownloadFile(_certLocation, $"{_localPathToFile}/{_certFileName}"); //var httpClient = new HttpClient(); //var httpResult = httpClient.Get(fileUrl); //using var resultStream = await httpResult.Content.ReadAsStreamAsync(); //using var fileStream = File.Create(pathToSave); //resultStream.CopyTo(fileStream); _certCollection = new X509Certificate2Collection(); _amazoncert = new X509Certificate2($"{_localPathToFile}/{_certFileName}"); // Get the user name and password stored in the configuration file. _userName = _configuration["UserName"]!; _pwd = _configuration["Password"]!; // For a list of Service Endpoints for Amazon Keyspaces, see: // https://docs.aws.amazon.com/keyspaces/latest/devguide/programmatic.endpoints.html var awsEndpoint = _configuration["ServiceEndpoint"]; _cluster = Cluster.Builder() .AddContactPoints(awsEndpoint) .WithPort(9142) .WithAuthProvider(new PlainTextAuthProvider(_userName, _pwd)) .WithSSL(new SSLOptions().SetCertificateCollection(_certCollection)) .WithQueryOptions( new QueryOptions() .SetConsistencyLevel(ConsistencyLevel.LocalQuorum) .SetSerialConsistencyLevel(ConsistencyLevel.LocalSerial)) .Build(); } /// <summary> /// Loads the contents of a JSON file into a list of movies to be /// added to the Apache Cassandra table. /// </summary> /// <param name="movieFileName">The full path to the JSON file.</param> /// <returns>A list of movie objects.</returns> public List<Movie> ImportMoviesFromJson(string movieFileName, int numToImport = 0) { if (!File.Exists(movieFileName)) { return null!; } using var sr = new StreamReader(movieFileName); string json = sr.ReadToEnd(); var allMovies = JsonConvert.DeserializeObject<List<Movie>>(json); // If numToImport = 0, return all movies in the collection. if (numToImport == 0) { // Now return the entire list of movies. return allMovies; } else { // Now return the first numToImport entries. return allMovies.GetRange(0, numToImport); } } /// <summary> /// Insert movies into the movie table. /// </summary> /// <param name="keyspaceName">The keyspace containing the table.</param> /// <param name="movieTableName">The Amazon Keyspaces table.</param> /// <param name="movieFilePath">The path to the resource file containing /// movie data to insert into the table.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> InsertIntoMovieTable(string keyspaceName, string movieTableName, string movieFilePath, int numToImport = 20) { // Get some movie data from the movies.json file var movies = ImportMoviesFromJson(movieFilePath, numToImport); var session = _cluster.Connect(keyspaceName); string insertCql; RowSet rs; // Now we insert the numToImport movies into the table. foreach (var movie in movies) { // Escape single quote characters in the plot. insertCql = $"INSERT INTO {keyspaceName}.{movieTableName} (title, year, release_date, plot) values($${movie.Title}$$, {movie.Year}, '{movie.Info.Release_Date.ToString("yyyy-MM-dd")}', $${movie.Info.Plot}$$)"; rs = await session.ExecuteAsync(new SimpleStatement(insertCql)); } return true; } /// <summary> /// Gets all of the movies in the movies table. /// </summary> /// <param name="keyspaceName">The keyspace containing the table.</param> /// <param name="tableName">The name of the table.</param> /// <returns>A list of row objects containing movie data.</returns> public async Task<List<Row>> GetMovies(string keyspaceName, string tableName) { var session = _cluster.Connect(); RowSet rs; try { rs = await session.ExecuteAsync(new SimpleStatement($"SELECT * FROM {keyspaceName}.{tableName}")); // Extract the row data from the returned RowSet. var rows = rs.GetRows().ToList(); return rows; } catch (Exception ex) { Console.WriteLine(ex.Message); return null!; } } /// <summary> /// Mark a movie in the movie table as watched. /// </summary> /// <param name="keyspaceName">The keyspace containing the table.</param> /// <param name="tableName">The name of the table.</param> /// <param name="title">The title of the movie to mark as watched.</param> /// <param name="year">The year the movie was released.</param> /// <returns>A set of rows containing the changed data.</returns> public async Task<List<Row>> MarkMovieAsWatched(string keyspaceName, string tableName, string title, int year) { var session = _cluster.Connect(); string updateCql = $"UPDATE {keyspaceName}.{tableName} SET watched=true WHERE title = $${title}$$ AND year = {year};"; var rs = await session.ExecuteAsync(new SimpleStatement(updateCql)); var rows = rs.GetRows().ToList(); return rows; } /// <summary> /// Retrieve the movies in the movies table where watched is true. /// </summary> /// <param name="keyspaceName">The keyspace containing the table.</param> /// <param name="tableName">The name of the table.</param> /// <returns>A list of row objects containing information about movies /// where watched is true.</returns> public async Task<List<Row>> GetWatchedMovies(string keyspaceName, string tableName) { var session = _cluster.Connect(); RowSet rs; try { rs = await session.ExecuteAsync(new SimpleStatement($"SELECT title, year, plot FROM {keyspaceName}.{tableName} WHERE watched = true ALLOW FILTERING")); // Extract the row data from the returned RowSet. var rows = rs.GetRows().ToList(); return rows; } catch (Exception ex) { Console.WriteLine(ex.Message); return null!; } } }

작업

다음 코드 예시에서는 CreateKeyspace을 사용하는 방법을 보여 줍니다.

AWS SDK for .NET
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

/// <summary> /// Create a new keyspace. /// </summary> /// <param name="keyspaceName">The name for the new keyspace.</param> /// <returns>The Amazon Resource Name (ARN) of the new keyspace.</returns> public async Task<string> CreateKeyspace(string keyspaceName) { var response = await _amazonKeyspaces.CreateKeyspaceAsync( new CreateKeyspaceRequest { KeyspaceName = keyspaceName }); return response.ResourceArn; }
  • 자세한 API 내용은 참조CreateKeyspace의 섹션을 참조하세요. AWS SDK for .NET API

다음 코드 예시에서는 CreateTable을 사용하는 방법을 보여 줍니다.

AWS SDK for .NET
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

/// <summary> /// Create a new Amazon Keyspaces table. /// </summary> /// <param name="keyspaceName">The keyspace where the table will be created.</param> /// <param name="schema">The schema for the new table.</param> /// <param name="tableName">The name of the new table.</param> /// <returns>The Amazon Resource Name (ARN) of the new table.</returns> public async Task<string> CreateTable(string keyspaceName, SchemaDefinition schema, string tableName) { var request = new CreateTableRequest { KeyspaceName = keyspaceName, SchemaDefinition = schema, TableName = tableName, PointInTimeRecovery = new PointInTimeRecovery { Status = PointInTimeRecoveryStatus.ENABLED } }; var response = await _amazonKeyspaces.CreateTableAsync(request); return response.ResourceArn; }
  • 자세한 API 내용은 참조CreateTable의 섹션을 참조하세요. AWS SDK for .NET API

다음 코드 예시에서는 DeleteKeyspace을 사용하는 방법을 보여 줍니다.

AWS SDK for .NET
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

/// <summary> /// Delete an existing keyspace. /// </summary> /// <param name="keyspaceName"></param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> DeleteKeyspace(string keyspaceName) { var response = await _amazonKeyspaces.DeleteKeyspaceAsync( new DeleteKeyspaceRequest { KeyspaceName = keyspaceName }); return response.HttpStatusCode == HttpStatusCode.OK; }
  • 자세한 API 내용은 참조DeleteKeyspace의 섹션을 참조하세요. AWS SDK for .NET API

다음 코드 예시에서는 DeleteTable을 사용하는 방법을 보여 줍니다.

AWS SDK for .NET
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

/// <summary> /// Delete an Amazon Keyspaces table. /// </summary> /// <param name="keyspaceName">The keyspace containing the table.</param> /// <param name="tableName">The name of the table to delete.</param> /// <returns>A Boolean value indicating the success of the action.</returns> public async Task<bool> DeleteTable(string keyspaceName, string tableName) { var response = await _amazonKeyspaces.DeleteTableAsync( new DeleteTableRequest { KeyspaceName = keyspaceName, TableName = tableName }); return response.HttpStatusCode == HttpStatusCode.OK; }
  • 자세한 API 내용은 참조DeleteTable의 섹션을 참조하세요. AWS SDK for .NET API

다음 코드 예시에서는 GetKeyspace을 사용하는 방법을 보여 줍니다.

AWS SDK for .NET
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

/// <summary> /// Get data about a keyspace. /// </summary> /// <param name="keyspaceName">The name of the keyspace.</param> /// <returns>The Amazon Resource Name (ARN) of the keyspace.</returns> public async Task<string> GetKeyspace(string keyspaceName) { var response = await _amazonKeyspaces.GetKeyspaceAsync( new GetKeyspaceRequest { KeyspaceName = keyspaceName }); return response.ResourceArn; }
  • 자세한 API 내용은 참조GetKeyspace의 섹션을 참조하세요. AWS SDK for .NET API

다음 코드 예시에서는 GetTable을 사용하는 방법을 보여 줍니다.

AWS SDK for .NET
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

/// <summary> /// Get information about an Amazon Keyspaces table. /// </summary> /// <param name="keyspaceName">The keyspace containing the table.</param> /// <param name="tableName">The name of the Amazon Keyspaces table.</param> /// <returns>The response containing data about the table.</returns> public async Task<GetTableResponse> GetTable(string keyspaceName, string tableName) { var response = await _amazonKeyspaces.GetTableAsync( new GetTableRequest { KeyspaceName = keyspaceName, TableName = tableName }); return response; }
  • 자세한 API 내용은 참조GetTable의 섹션을 참조하세요. AWS SDK for .NET API

다음 코드 예시에서는 ListKeyspaces을 사용하는 방법을 보여 줍니다.

AWS SDK for .NET
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

/// <summary> /// Lists all keyspaces for the account. /// </summary> /// <returns>Async task.</returns> public async Task ListKeyspaces() { var paginator = _amazonKeyspaces.Paginators.ListKeyspaces(new ListKeyspacesRequest()); Console.WriteLine("{0, -30}\t{1}", "Keyspace name", "Keyspace ARN"); Console.WriteLine(new string('-', Console.WindowWidth)); await foreach (var keyspace in paginator.Keyspaces) { Console.WriteLine($"{keyspace.KeyspaceName,-30}\t{keyspace.ResourceArn}"); } }
  • 자세한 API 내용은 참조ListKeyspaces의 섹션을 참조하세요. AWS SDK for .NET API

다음 코드 예시에서는 ListTables을 사용하는 방법을 보여 줍니다.

AWS SDK for .NET
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

/// <summary> /// Lists the Amazon Keyspaces tables in a keyspace. /// </summary> /// <param name="keyspaceName">The name of the keyspace.</param> /// <returns>A list of TableSummary objects.</returns> public async Task<List<TableSummary>> ListTables(string keyspaceName) { var response = await _amazonKeyspaces.ListTablesAsync(new ListTablesRequest { KeyspaceName = keyspaceName }); response.Tables.ForEach(table => { Console.WriteLine($"{table.KeyspaceName}\t{table.TableName}\t{table.ResourceArn}"); }); return response.Tables; }
  • 자세한 API 내용은 참조ListTables의 섹션을 참조하세요. AWS SDK for .NET API

다음 코드 예시에서는 RestoreTable을 사용하는 방법을 보여 줍니다.

AWS SDK for .NET
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

/// <summary> /// Restores the specified table to the specified point in time. /// </summary> /// <param name="keyspaceName">The keyspace containing the table.</param> /// <param name="tableName">The name of the table to restore.</param> /// <param name="timestamp">The time to which the table will be restored.</param> /// <returns>The Amazon Resource Name (ARN) of the restored table.</returns> public async Task<string> RestoreTable(string keyspaceName, string tableName, string restoredTableName, DateTime timestamp) { var request = new RestoreTableRequest { RestoreTimestamp = timestamp, SourceKeyspaceName = keyspaceName, SourceTableName = tableName, TargetKeyspaceName = keyspaceName, TargetTableName = restoredTableName }; var response = await _amazonKeyspaces.RestoreTableAsync(request); return response.RestoredTableARN; }
  • 자세한 API 내용은 참조RestoreTable의 섹션을 참조하세요. AWS SDK for .NET API

다음 코드 예시에서는 UpdateTable을 사용하는 방법을 보여 줍니다.

AWS SDK for .NET
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

/// <summary> /// Updates the movie table to add a boolean column named watched. /// </summary> /// <param name="keyspaceName">The keyspace containing the table.</param> /// <param name="tableName">The name of the table to change.</param> /// <returns>The Amazon Resource Name (ARN) of the updated table.</returns> public async Task<string> UpdateTable(string keyspaceName, string tableName) { var newColumn = new ColumnDefinition { Name = "watched", Type = "boolean" }; var request = new UpdateTableRequest { KeyspaceName = keyspaceName, TableName = tableName, AddColumns = new List<ColumnDefinition> { newColumn } }; var response = await _amazonKeyspaces.UpdateTableAsync(request); return response.ResourceArn; }
  • 자세한 API 내용은 참조UpdateTable의 섹션을 참조하세요. AWS SDK for .NET API