在中映射项目DynamoDB桌子 - Amazon SDK for Java
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

现在,您可以使用Amazon S3转移经理(开发人员预览版)中的Amazon SDK for Java2.x 用于加速文件传输。试试一下然后告诉我们您的想法!顺便说一句,Amazon SDK for Java正在招聘团队软件开发工程师

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

在中映射项目DynamoDB桌子

Amazon DynamoDB 增强型客户端是属于Amazon SDK for Java版本 2 (v2) 一部分的高级库。它提供将客户端类映射到 DynamoDB 表的简单方法。您可以在代码中定义表与其相应模型类之间的关系。然后,您可以直观地对 DynamoDB 中的表或项目执行各种创建、读取、更新或删除 (CRUD) 操作。

Amazon SDK for Java v2 包含一组注释,您可以将其与 Java Bean 结合使用,以快速生成表架构来将类映射到表。或者,如果显式声明每个 TableSchema,则不需要在类中包含注释。

处理中的项目DynamoDB如何使用增强型客户端执行表,首先创建DynamoDbEnhancedClient来自现有的DynamoDbClient对象。

Region region = Region.US_EAST_1; DynamoDbClient ddb = DynamoDbClient.builder() .region(region) .build(); DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder() .dynamoDbClient(ddb) .build(); createDynamoDBTable(enhancedClient);

使用增强型客户端创建表

要使用增强型客户端轻松创建 TableSchema,首先创建一个 Java 数据类,该数据类包含默认的公有构造函数和适用于类中每个属性的 getter 和 setter 的标准化名称。包括一个类级别注释以指示它是 DynamoDbBean,并且至少在 getter 或 setter 上包含对于表记录主键的 DynamoDbPartitionKey 注释。

定义此数据类之后,调用TableSchema'sfromBean()使用该数据类创建表架构。

有关如何执行此操作的示例,请参阅下面的代码段。

导入

import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; import software.amazon.awssdk.enhanced.dynamodb.TableSchema; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey; 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.enhanced.dynamodb.mapper.annotations.DynamoDbBean; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneOffset;

代码

public static void putRecord(DynamoDbEnhancedClient enhancedClient) { try { DynamoDbTable<Customer> custTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class)); // Create an Instant LocalDate localDate = LocalDate.parse("2020-04-07"); LocalDateTime localDateTime = localDate.atStartOfDay(); Instant instant = localDateTime.toInstant(ZoneOffset.UTC); // Populate the Table Customer custRecord = new Customer(); custRecord.setCustName("Susan red"); custRecord.setId("id146"); custRecord.setEmail("sred@noserver.com"); custRecord.setRegistrationDate(instant) ; // Put the customer data into a DynamoDB table custTable.putItem(custRecord); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("done"); }

请参阅 GitHub 上的完整示例

检索(获取)表中的项目

如何从获取项目DynamoDB表中,创建DynamoDbTable对象和调用getItem()用一个GetItemEnhancedRequest对象来获取实际物品。

例如,下面的代码段演示一种使用增强型客户端从 DynamoDB 表中的项目获取信息的方法。

导入

import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; import software.amazon.awssdk.enhanced.dynamodb.Key; import software.amazon.awssdk.enhanced.dynamodb.TableSchema; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import java.time.Instant;

代码

public static String getItem(DynamoDbEnhancedClient enhancedClient) { try { //Create a DynamoDbTable object DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class)); //Create a KEY object Key key = Key.builder() .partitionValue("id146") .build(); // Get the item by using the key Customer result = mappedTable.getItem(r->r.key(key)); return "The email value is "+result.getEmail(); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; }

请参阅 GitHub 上的完整示例

批量创建 (put) 和删除项目

您可以将一系列 put 请求 (PutItemEnhancedRequest) 和 delete 请求 (DeleteItemEnhancedRequest) 批处理到一个或多个表,然后在单个请求中发送所有更改。

在下面的代码段中,创建一个 DynamoDbTable 对象,将两个项目排队以添加到表中,然后在单次调用中将项目写入表中。根据需要,在每个批处理中包含 addDeleteItem()addPutItem()WriteBatch.Builder 的一部分)的多个条目。要将更改排入其他表的队列,请添加另一个实例WriteBatch.builder()并提供相应的DynamoDbTable中的对象mappedTableResource().

导入

import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; import software.amazon.awssdk.enhanced.dynamodb.TableSchema; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey; import software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteItemEnhancedRequest; import software.amazon.awssdk.enhanced.dynamodb.model.WriteBatch; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneOffset;

代码

public static void putBatchRecords(DynamoDbEnhancedClient enhancedClient) { try { DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class)); LocalDate localDate = LocalDate.parse("2020-04-07"); LocalDateTime localDateTime = localDate.atStartOfDay(); Instant instant = localDateTime.toInstant(ZoneOffset.UTC); Customer record2 = new Customer(); record2.setCustName("Fred Pink"); record2.setId("id110"); record2.setEmail("fredp@noserver.com"); record2.setRegistrationDate(instant) ; Customer record3 = new Customer(); record3.setCustName("Susan Pink"); record3.setId("id120"); record3.setEmail("spink@noserver.com"); record3.setRegistrationDate(instant) ; // Create a BatchWriteItemEnhancedRequest object BatchWriteItemEnhancedRequest batchWriteItemEnhancedRequest = BatchWriteItemEnhancedRequest.builder() .writeBatches( WriteBatch.builder(Customer.class) .mappedTableResource(mappedTable) .addPutItem(r -> r.item(record2)) .addPutItem(r -> r.item(record3)) .build()) .build(); // Add these two items to the table enhancedClient.batchWriteItem(batchWriteItemEnhancedRequest); System.out.println("done"); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } }

请参阅 GitHub 上的完整示例

使用筛选的查询从表中获取项目

您可以根据可筛选的查询从表中获取项目,然后对查询结果中的一个或多个项目执行操作(例如,返回项目值)。

在下面的代码段中,您可以通过首先将您要搜索的一个或多个值定义为 AttributeValue 对象来构建筛选条件。然后你把这个放进一个HashMap构建表达式来自 的HashMap. 构建 QueryConditional 对象以指定要在查询中匹配的主键,然后对 DynamoDbTable 对象执行此查询。

注意

QueryConditional 接口有若干种可用来构建查询的方法,包括常见的条件语句,如大于、小于和介于两者之间。

导入

import java.time.Instant; import java.util.Map; import java.util.Iterator; import java.util.HashMap; import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; import software.amazon.awssdk.enhanced.dynamodb.Expression; import software.amazon.awssdk.enhanced.dynamodb.Key; import software.amazon.awssdk.enhanced.dynamodb.TableSchema; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;

代码

public static void queryTableFilter(DynamoDbEnhancedClient enhancedClient) { try{ DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class)); AttributeValue att = AttributeValue.builder() .s("sblue@noserver.com") .build(); Map<String, AttributeValue> expressionValues = new HashMap<>(); expressionValues.put(":value", att); Expression expression = Expression.builder() .expression("email = :value") .expressionValues(expressionValues) .build(); // Create a QueryConditional object that is used in the query operation. QueryConditional queryConditional = QueryConditional .keyEqualTo(Key.builder().partitionValue("id103") .build()); // Get items in the Customer table and write out the ID value. Iterator<Customer> results = mappedTable.query(r -> r.queryConditional(queryConditional).filterExpression(expression)).items().iterator(); while (results.hasNext()) { Customer rec = results.next(); System.out.println("The record id is "+rec.getId()); } } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("Done"); }

请参阅 GitHub 上的完整示例

从表中检索(获取)所有项目

当你想获取给定的所有记录时DynamoDB表格中,使用scan()你的方法DynamoDbTable对象和items()方法来创建一组结果,您可以对其执行各种项目操作。例如,以下代码段打印出 Record 表中每个项目的 ID 值。

导入

import java.time.Instant; import java.util.Iterator; import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; import software.amazon.awssdk.enhanced.dynamodb.TableSchema; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey; import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;

代码

public static void scan( DynamoDbEnhancedClient enhancedClient) { try{ // Create a DynamoDbTable object DynamoDbTable<Customer> custTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class)); Iterator<Customer> results = custTable.scan().items().iterator(); while (results.hasNext()) { Customer rec = results.next(); System.out.println("The record id is "+rec.getId()); System.out.println("The name is " +rec.getCustName()); } } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("Done"); }

请参阅 GitHub 上的完整示例

有关更多信息,请参阅 。中处理项目DynamoDB中的Amazon DynamoDB开发人员指南 的第一个版本。