Amazon DynamoDB
入门指南 (API Version 2012-08-10)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

步骤 3:创建、读取、更新和删除项目

在此步骤中,您将对 Movies 表中的项目执行读写操作。

要了解有关数据读写的更多信息,请参阅 Amazon DynamoDB 开发人员指南 中的处理项目

步骤 3.1:创建新项目

在此步骤中,您将向 Movies 表中添加一个新项目。

  1. 复制以下程序到您的 Java 开发环境中。

    Copy
    // Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. // Licensed under the Apache License, Version 2.0. package com.amazonaws.codesamples.gsg; import java.util.HashMap; import java.util.Map; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.Item; import com.amazonaws.services.dynamodbv2.document.PutItemOutcome; import com.amazonaws.services.dynamodbv2.document.Table; public class MoviesItemOps01 { public static void main(String[] args) throws Exception { AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard() .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")) .build(); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); int year = 2015; String title = "The Big New Movie"; final Map<String, Object> infoMap = new HashMap<String, Object>(); infoMap.put("plot", "Nothing happens at all."); infoMap.put("rating", 0); try { System.out.println("Adding a new item..."); PutItemOutcome outcome = table .putItem(new Item().withPrimaryKey("year", year, "title", title).withMap("info", infoMap)); System.out.println("PutItem succeeded:\n" + outcome.getPutItemResult()); } catch (Exception e) { System.err.println("Unable to add item: " + year + " " + title); System.err.println(e.getMessage()); } } }

    注意

    主键是必填项。此代码将添加一个具有主键 (yeartitle) 和 info 属性的项目。info 属性用于存储示例 JSON,其中提供有关电影的更多信息。

  2. 编译并运行程序。

步骤 3.2:读取项目

在上一个程序中,您已向表中添加了以下项目:

Copy
{ year: 2015, title: "The Big New Movie", info: { plot: "Nothing happens at all.", rating: 0 } }

您可以使用 getItem 方法读取 Movies 表中的项目。您必须指定主键值,以便在知道 Movies 中的任何项目的 yeartitle 的情况下读取该项目。

  1. 复制以下程序到您的 Java 开发环境中。

    Copy
    // Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. // Licensed under the Apache License, Version 2.0. package com.amazonaws.codesamples.gsg; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.Item; import com.amazonaws.services.dynamodbv2.document.Table; import com.amazonaws.services.dynamodbv2.document.spec.GetItemSpec; public class MoviesItemOps02 { public static void main(String[] args) throws Exception { AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard() .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")) .build(); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); int year = 2015; String title = "The Big New Movie"; GetItemSpec spec = new GetItemSpec().withPrimaryKey("year", year, "title", title); try { System.out.println("Attempting to read the item..."); Item outcome = table.getItem(spec); System.out.println("GetItem succeeded: " + outcome); } catch (Exception e) { System.err.println("Unable to read item: " + year + " " + title); System.err.println(e.getMessage()); } } }
  2. 编译并运行程序。

步骤 3.3:更新项目

您可以使用 updateItem 方法修改现有项目。您可以更新现有属性的值、添加新属性或删除属性。

在此示例中,您可以执行以下更新:

  • 更改现有属性 (ratingplot) 的值。

  • 向现有 info 映射中添加新的列表属性 (actors)。

项目将在以下基础上进行更改:

Copy
{ year: 2015, title: "The Big New Movie", info: { plot: "Nothing happens at all.", rating: 0 } }

更改为以下项目:

Copy
{ year: 2015, title: "The Big New Movie", info: { plot: "Everything happens all at once.", rating: 5.5, actors: ["Larry", "Moe", "Curly"] } }
  1. 复制以下程序到您的 Java 开发环境中。

    Copy
    // Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. // Licensed under the Apache License, Version 2.0. package com.amazonaws.codesamples.gsg; import java.util.Arrays; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.Table; import com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome; import com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec; import com.amazonaws.services.dynamodbv2.document.utils.ValueMap; import com.amazonaws.services.dynamodbv2.model.ReturnValue; public class MoviesItemOps03 { public static void main(String[] args) throws Exception { AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard() .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")) .build(); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); int year = 2015; String title = "The Big New Movie"; UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("year", year, "title", title) .withUpdateExpression("set info.rating = :r, info.plot=:p, info.actors=:a") .withValueMap(new ValueMap().withNumber(":r", 5.5).withString(":p", "Everything happens all at once.") .withList(":a", Arrays.asList("Larry", "Moe", "Curly"))) .withReturnValues(ReturnValue.UPDATED_NEW); try { System.out.println("Updating the item..."); UpdateItemOutcome outcome = table.updateItem(updateItemSpec); System.out.println("UpdateItem succeeded:\n" + outcome.getItem().toJSONPretty()); } catch (Exception e) { System.err.println("Unable to update item: " + year + " " + title); System.err.println(e.getMessage()); } } }

    注意

    此程序使用 UpdateExpression 来描述您要对指定项目执行的所有更新。

    ReturnValues 参数用于指示 DynamoDB 仅返回更新后的属性 (UPDATED_NEW)。

  2. 编译并运行程序。

步骤 3.4:递增一个原子计数器

DynamoDB 支持原子计数器,以便您使用 updateItem 方法来递增或递减现有属性的值,而不干扰其他写入请求。 (所有写入请求的应用顺序跟接收顺序相同。)

以下程序演示了如何递增电影的 rating。每次运行此程序时,此属性都会递增 1。

  1. 复制以下程序到您的 Java 开发环境中。

    Copy
    // Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. // Licensed under the Apache License, Version 2.0. package com.amazonaws.codesamples.gsg; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.Table; import com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome; import com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec; import com.amazonaws.services.dynamodbv2.document.utils.ValueMap; import com.amazonaws.services.dynamodbv2.model.ReturnValue; public class MoviesItemOps04 { public static void main(String[] args) throws Exception { AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard() .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")) .build(); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); int year = 2015; String title = "The Big New Movie"; UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("year", year, "title", title) .withUpdateExpression("set info.rating = info.rating + :val") .withValueMap(new ValueMap().withNumber(":val", 1)).withReturnValues(ReturnValue.UPDATED_NEW); try { System.out.println("Incrementing an atomic counter..."); UpdateItemOutcome outcome = table.updateItem(updateItemSpec); System.out.println("UpdateItem succeeded:\n" + outcome.getItem().toJSONPretty()); } catch (Exception e) { System.err.println("Unable to update item: " + year + " " + title); System.err.println(e.getMessage()); } } }
  2. 编译并运行程序。

步骤 3.5:更新项目 (有条件)

以下程序演示了如何使用有条件的 UpdateItem。如果条件为真,则更新成功;否则,不执行更新。

在此示例中,仅当有超过 3 位男演员时,才会更新电影项目。

  1. 复制以下程序到您的 Java 开发环境中。

    Copy
    // Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. // Licensed under the Apache License, Version 2.0. package com.amazonaws.codesamples.gsg; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.PrimaryKey; import com.amazonaws.services.dynamodbv2.document.Table; import com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome; import com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec; import com.amazonaws.services.dynamodbv2.document.utils.ValueMap; import com.amazonaws.services.dynamodbv2.model.ReturnValue; public class MoviesItemOps05 { public static void main(String[] args) throws Exception { AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard() .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")) .build(); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); int year = 2015; String title = "The Big New Movie"; UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey(new PrimaryKey("year", year, "title", title)).withUpdateExpression("remove info.actors[0]") .withConditionExpression("size(info.actors) > :num").withValueMap(new ValueMap().withNumber(":num", 3)) .withReturnValues(ReturnValue.UPDATED_NEW); // Conditional update (we expect this to fail) try { System.out.println("Attempting a conditional update..."); UpdateItemOutcome outcome = table.updateItem(updateItemSpec); System.out.println("UpdateItem succeeded:\n" + outcome.getItem().toJSONPretty()); } catch (Exception e) { System.err.println("Unable to update item: " + year + " " + title); System.err.println(e.getMessage()); } } }
  2. 编译并运行程序。

    程序应该会失败并显示以下消息:

    The conditional request failed

    这是因为电影包含三位男演员,但此条件是检查男演员数量是否大于 3。

  3. 修改程序,以使 ConditionExpression 如下所示:

    Copy
    .withConditionExpression("size(info.actors) >= :num")

    此时,条件变成大于或等于 3,而不是大于 3

  4. 编译并运行程序。UpdateItem 操作这次应该会成功。

步骤 3.6:删除项目

您可以使用 deleteItem 方法,通过指定项目的主键来删除一个项目。您可以提供一个 ConditionExpression,避免在不符合条件时删除项目。

在以下示例中,您将尝试删除评分为 5 分或更低的特定电影项目。

  1. 复制以下程序到您的 Java 开发环境中。

    Copy
    // Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. // Licensed under the Apache License, Version 2.0. package com.amazonaws.codesamples.gsg; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.PrimaryKey; import com.amazonaws.services.dynamodbv2.document.Table; import com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec; import com.amazonaws.services.dynamodbv2.document.utils.ValueMap; public class MoviesItemOps06 { public static void main(String[] args) throws Exception { AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard() .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")) .build(); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); int year = 2015; String title = "The Big New Movie"; DeleteItemSpec deleteItemSpec = new DeleteItemSpec() .withPrimaryKey(new PrimaryKey("year", year, "title", title)).withConditionExpression("info.rating <= :val") .withValueMap(new ValueMap().withNumber(":val", 5.0)); // Conditional delete (we expect this to fail) try { System.out.println("Attempting a conditional delete..."); table.deleteItem(deleteItemSpec); System.out.println("DeleteItem succeeded"); } catch (Exception e) { System.err.println("Unable to delete item: " + year + " " + title); System.err.println(e.getMessage()); } } }
  2. 编译并运行程序。

    程序应该会失败并显示以下消息:

    The conditional request failed

    这是因为此特定电影的评分大于 5 分。

  3. 修改程序,以便移除 DeleteItemSpec 中的条件。

    Copy
    DeleteItemSpec deleteItemSpec = new DeleteItemSpec() .withPrimaryKey(new PrimaryKey("year", 2015, "title", "The Big New Movie"));
  4. 编译并运行程序。因为您已移除条件,所以此时删除操作将会成功。