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

步骤 1:创建 DynamoDB 客户端

Microsoft .NET 和 DynamoDB 教程中的第一步是创建为您提供对 Amazon DynamoDB API 的访问权限的客户端。DynamoDB_intro 中的 Main 函数通过调用在 01_CreateClient.cs 文件中实施的 createClient 函数来完成此操作:

using System; using System.Net.Sockets; using Amazon.DynamoDBv2; namespace DynamoDB_intro { public static partial class Ddb_Intro { /*----------------------------------------------------------------------------------- * If you are creating a client for the DynamoDB service, make sure your credentials * are set up first, as explained in: * https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SettingUp.DynamoWebService.html, * * If you are creating a client for DynamoDBLocal (for testing purposes), * DynamoDB-Local should be started first. For most simple testing, you can keep * data in memory only, without writing anything to disk. To do this, use the * following command line: * * java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -inMemory * * For information about DynamoDBLocal, see: * https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html. *-----------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------- * createClient *--------------------------------------------------------------------------*/ public static bool createClient( bool useDynamoDBLocal ) { if( useDynamoDBLocal ) { operationSucceeded = false; operationFailed = false; // First, check to see whether anyone is listening on the DynamoDB local port // (by default, this is port 8000, so if you are using a different port, modify this accordingly) bool localFound = false; try { using (var tcp_client = new TcpClient()) { var result = tcp_client.BeginConnect("localhost", 8000, null, null); localFound = result.AsyncWaitHandle.WaitOne(3000); // Wait 3 seconds tcp_client.EndConnect(result); } } catch { localFound = false; } if( !localFound ) { Console.WriteLine("\n ERROR: DynamoDB Local does not appear to have been started..." + "\n (checked port 8000)"); operationFailed = true; return (false); } // If DynamoDB-Local does seem to be running, so create a client Console.WriteLine( " -- Setting up a DynamoDB-Local client (DynamoDB Local seems to be running)" ); AmazonDynamoDBConfig ddbConfig = new AmazonDynamoDBConfig(); ddbConfig.ServiceURL = "http://localhost:8000"; try { client = new AmazonDynamoDBClient( ddbConfig ); } catch( Exception ex ) { Console.WriteLine( " FAILED to create a DynamoDBLocal client; " + ex.Message ); operationFailed = true; return false; } } else { try { client = new AmazonDynamoDBClient( ); } catch( Exception ex ) { Console.WriteLine( " FAILED to create a DynamoDB client; " + ex.Message ); operationFailed = true; } } operationSucceeded = true; return true; } } }

Main 调用此函数,并将 useDynamoDBLocal 参数设置为 true。因此,DynamoDB 的本地测试版本必须已使用默认端口 (8000) 在您的计算机上运行,否则调用将失败。

useDynamoDBLocal 参数设置为 false 会为 DynamoDB 服务本身而不是本地测试程序创建客户端。

下一步

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

本页内容: