Amazon DynamoDB
开发人员指南 (API 版本 2012-08-10)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

步骤 2:使用低级别 API 创建表

适用于 .NET 的 AWS 开发工具包 中的文档模型未提供用于创建表的 API,因此您必须使用低级 API。有关更多信息,请参阅处理表:.NET

Microsoft .NET 和 DynamoDB 教程的本步骤中,您将在 Amazon DynamoDB 中创建一个名为 Movies 的表。表的主键由以下属性组成:

  • year – 分区键。AttributeTypeN,表示数字。

    注意

    由于“year”是 DynamoDB 中的保留字,当在低级别表达式中引用“year”时,您需要使用 ExpressionAttributeNames 对象为它创建一个别名(如 #yr)。

  • title – 排序键。AttributeTypeS,表示字符串。

DynamoDB_intro 中的 Main 函数通过等待在 02_CreatingTable.cs 文件中实施的异步 CreatingTable_async 函数来执行此操作:

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.DynamoDBv2.Model; namespace DynamoDB_intro { public static partial class Ddb_Intro { /*-------------------------------------------------------------------------- * CreatingTable_async *--------------------------------------------------------------------------*/ public static async Task CreatingTable_async( string new_table_name, List<AttributeDefinition> table_attributes, List<KeySchemaElement> table_key_schema, ProvisionedThroughput provisionedThroughput ) { Console.WriteLine( " -- Creating a new table named {0}...", new_table_name ); if( await checkingTableExistence_async( new_table_name ) ) { Console.WriteLine( " -- No need to create a new table..." ); return; } if( operationFailed ) return; operationSucceeded = false; Task<bool> newTbl = CreateNewTable_async( new_table_name, table_attributes, table_key_schema, provisionedThroughput ); await newTbl; } /*-------------------------------------------------------------------------- * checkingTableExistence_async *--------------------------------------------------------------------------*/ static async Task<bool> checkingTableExistence_async( string tblNm ) { DescribeTableResponse descResponse; operationSucceeded = false; operationFailed = false; ListTablesResponse tblResponse = await Ddb_Intro.client.ListTablesAsync(); if (tblResponse.TableNames.Contains(tblNm)) { Console.WriteLine(" A table named {0} already exists in DynamoDB!", tblNm); // If the table exists, get its description try { descResponse = await Ddb_Intro.client.DescribeTableAsync(Ddb_Intro.movies_table_name); operationSucceeded = true; } catch (Exception ex) { Console.WriteLine(" However, its description is not available ({0})", ex.Message); Ddb_Intro.moviesTableDescription = null; operationFailed = true; return ( true ); } Ddb_Intro.moviesTableDescription = descResponse.Table; return ( true ); } return ( false ); } /*-------------------------------------------------------------------------- * CreateNewTable_async *--------------------------------------------------------------------------*/ public static async Task<bool> CreateNewTable_async( string table_name, List<AttributeDefinition> table_attributes, List<KeySchemaElement> table_key_schema, ProvisionedThroughput provisioned_throughput ) { CreateTableRequest request; CreateTableResponse response; // Build the 'CreateTableRequest' structure for the new table request = new CreateTableRequest { TableName = table_name, AttributeDefinitions = table_attributes, KeySchema = table_key_schema, // Provisioned-throughput settings are always required, // although the local test version of DynamoDB ignores them. ProvisionedThroughput = provisioned_throughput }; operationSucceeded = false; operationFailed = false; try { Task<CreateTableResponse> makeTbl = Ddb_Intro.client.CreateTableAsync( request ); response = await makeTbl; Console.WriteLine( " -- Created the \"{0}\" table successfully!", table_name ); operationSucceeded = true; } catch( Exception ex ) { Console.WriteLine( " FAILED to create the new table, because: {0}.", ex.Message ); operationFailed = true; return( false ); } // Report the status of the new table... Console.WriteLine( " Status of the new table: '{0}'.", response.TableDescription.TableStatus ); Ddb_Intro.moviesTableDescription = response.TableDescription; return ( true ); } } }

CreatingTable_async 函数首先等待异步 checkingTableExistence_async 函数确定名为“Movies”的表是否已存在。如果该表存在,则 checkingTableExistence_async 会检索现有 TableTableDescription

如果该表尚不存在,CreatingTable_async 将等待 CreateNewTable_async 使用 DynamoDB 客户端 API CreateTableAsync 创建 Movies

DynamoDB_intro 示例将尽可能使用异步方法而不是同步方法。这是因为 .NET 内核仅支持异步方式,并且在性能很重要的情况下,异步模式通常是首选。有关更多信息,请参阅适用于 .NET 的 AWS 异步 API

要了解有关管理表的更多信息,请参阅在 DynamoDB 中使用表

下一步

步骤 3:将示例数据加载到 Movies 表

本页内容: