使用软件开发工具包的 DynamoDB 的代码示例 Amazon - Amazon DynamoDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用软件开发工具包的 DynamoDB 的代码示例 Amazon

以下代码示例展示了如何将 DynamoDB 与软件开发套件 (SD Amazon K) 配合使用。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景和跨服务示例的上下文查看操作。

场景 是展示如何通过在同一服务中调用多个函数来完成特定任务的代码示例。

跨服务示例 是指跨多个 Amazon Web Services 工作的示例应用程序。

有关 S Amazon DK 开发者指南和代码示例的完整列表,请参阅结合使用 DynamoDB 与 Amazon SDK。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。

开始使用

以下代码示例展示了如何开始使用 DynamoDB。

.NET
Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.Model; namespace DynamoDB_Actions; public static class HelloDynamoDB { static async Task Main(string[] args) { var dynamoDbClient = new AmazonDynamoDBClient(); Console.WriteLine($"Hello Amazon Dynamo DB! Following are some of your tables:"); Console.WriteLine(); // You can use await and any of the async methods to get a response. // Let's get the first five tables. var response = await dynamoDbClient.ListTablesAsync( new ListTablesRequest() { Limit = 5 }); foreach (var table in response.TableNames) { Console.WriteLine($"\tTable: {table}"); Console.WriteLine(); } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NET API 参考ListTables中的。

C++
适用于 C++ 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

C MakeLists .txt cMake 文件的代码。

# Set the minimum required version of CMake for this project. cmake_minimum_required(VERSION 3.13) # Set the AWS service components used by this project. set(SERVICE_COMPONENTS dynamodb) # Set this project's name. project("hello_dynamodb") # Set the C++ standard to use to build this target. # At least C++ 11 is required for the AWS SDK for C++. set(CMAKE_CXX_STANDARD 11) # Use the MSVC variable to determine if this is a Windows build. set(WINDOWS_BUILD ${MSVC}) if (WINDOWS_BUILD) # Set the location where CMake can find the installed libraries for the AWS SDK. string(REPLACE ";" "/aws-cpp-sdk-all;" SYSTEM_MODULE_PATH "${CMAKE_SYSTEM_PREFIX_PATH}/aws-cpp-sdk-all") list(APPEND CMAKE_PREFIX_PATH ${SYSTEM_MODULE_PATH}) endif () # Find the AWS SDK for C++ package. find_package(AWSSDK REQUIRED COMPONENTS ${SERVICE_COMPONENTS}) if (WINDOWS_BUILD) # Copy relevant AWS SDK for C++ libraries into the current binary directory for running and debugging. # set(BIN_SUB_DIR "/Debug") # if you are building from the command line you may need to uncomment this # and set the proper subdirectory to the executables' location. AWSSDK_CPY_DYN_LIBS(SERVICE_COMPONENTS "" ${CMAKE_CURRENT_BINARY_DIR}${BIN_SUB_DIR}) endif () add_executable(${PROJECT_NAME} hello_dynamodb.cpp) target_link_libraries(${PROJECT_NAME} ${AWSSDK_LINK_LIBRARIES})

hello_dynamodb.cpp 源文件的代码。

#include <aws/core/Aws.h> #include <aws/dynamodb/DynamoDBClient.h> #include <aws/dynamodb/model/ListTablesRequest.h> #include <iostream> /* * A "Hello DynamoDB" starter application which initializes an Amazon DynamoDB (DynamoDB) client and lists the * DynamoDB tables. * * main function * * Usage: 'hello_dynamodb' * */ int main(int argc, char **argv) { Aws::SDKOptions options; // Optionally change the log level for debugging. // options.loggingOptions.logLevel = Utils::Logging::LogLevel::Debug; Aws::InitAPI(options); // Should only be called once. int result = 0; { Aws::Client::ClientConfiguration clientConfig; // Optional: Set to the AWS Region (overrides config file). // clientConfig.region = "us-east-1"; Aws::DynamoDB::DynamoDBClient dynamodbClient(clientConfig); Aws::DynamoDB::Model::ListTablesRequest listTablesRequest; listTablesRequest.SetLimit(50); do { const Aws::DynamoDB::Model::ListTablesOutcome &outcome = dynamodbClient.ListTables( listTablesRequest); if (!outcome.IsSuccess()) { std::cout << "Error: " << outcome.GetError().GetMessage() << std::endl; result = 1; break; } for (const auto &tableName: outcome.GetResult().GetTableNames()) { std::cout << tableName << std::endl; } listTablesRequest.SetExclusiveStartTableName( outcome.GetResult().GetLastEvaluatedTableName()); } while (!listTablesRequest.GetExclusiveStartTableName().empty()); } Aws::ShutdownAPI(options); // Should only be called once. return result; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for C++ API 参考ListTables中的。

Java
适用于 Java 2.x 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.ListTablesRequest; import software.amazon.awssdk.services.dynamodb.model.ListTablesResponse; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListTables { public static void main(String[] args) { System.out.println("Listing your Amazon DynamoDB tables:\n"); Region region = Region.US_EAST_1; DynamoDbClient ddb = DynamoDbClient.builder() .region(region) .build(); listAllTables(ddb); ddb.close(); } public static void listAllTables(DynamoDbClient ddb) { boolean moreTables = true; String lastName = null; while (moreTables) { try { ListTablesResponse response = null; if (lastName == null) { ListTablesRequest request = ListTablesRequest.builder().build(); response = ddb.listTables(request); } else { ListTablesRequest request = ListTablesRequest.builder() .exclusiveStartTableName(lastName).build(); response = ddb.listTables(request); } List<String> tableNames = response.tableNames(); if (tableNames.size() > 0) { for (String curName : tableNames) { System.out.format("* %s\n", curName); } } else { System.out.println("No tables found!"); System.exit(0); } lastName = response.lastEvaluatedTableName(); if (lastName == null) { moreTables = false; } } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } } System.out.println("\nDone!"); } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for Java 2.x API 参考ListTables中的。

JavaScript
适用于 JavaScript (v3) 的软件开发工具包
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

import { ListTablesCommand, DynamoDBClient } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new ListTablesCommand({}); const response = await client.send(command); console.log(response.TableNames.join("\n")); return response; };
  • 有关 API 的详细信息,请参阅 Amazon SDK for JavaScript API 参考ListTables中的。