java:无法删除dynamodb项值

3phpmpom  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(560)

我试图从表动物上的所有项目中删除一个名为“dog”的字段,或者使用以下代码:

  1. Table table= dynamoDB.getTable("animals");
  2. ScanRequest scanRequest = new ScanRequest().withTableName("animals");
  3. ScanResult result = client.scan(scanRequest);
  4. for (Map<String, AttributeValue> item : result.getItems()){
  5. table.updateItem(new PrimaryKey("<Primary partition key name>", item.get("<Primary partition key name>"),"<Primary sort key name>", item.get("<Primary sort key name>")),
  6. new AttributeUpdate("dog").delete());
  7. }

但我得到了:

  1. Exception in thread "main" java.lang.RuntimeException: value type: class com.amazonaws.services.dynamodbv2.model.AttributeValue
  2. .
  3. .
  4. .
  5. Caused by: java.lang.UnsupportedOperationException: value type: class com.amazonaws.services.dynamodbv2.model.AttributeValue

我做错了什么?
顺便说一下,我也试过:

  1. UpdateItemSpec updateItemSpec = new UpdateItemSpec()
  2. .withPrimaryKey("<Primary partition key name>", item.get("<Primary partition key name>"),"<Primary sort key name>", item.get("<Primary sort key name>"))
  3. .withUpdateExpression("REMOVE dog");
  4. table.updateItem(updateItemSpec);

但也有同样的例外(还尝试了删除而不是删除)

56lgkhnf

56lgkhnf1#

您使用的是旧的v1 dynamodbapi,这不是最佳实践。使用AmazonDynamodbv2JavaAPI是更好的实践。
AWSSDKforJava2.x是对Version1.x代码库的主要重写。它构建在Java8+之上,并添加了几个经常请求的特性。其中包括对非阻塞i/o的支持,以及在运行时插入不同http实现的能力。
如果您不熟悉aws sdk for java v2,请参阅:
开始使用aws sdk for java 2.x
对于这个用例(修改一个项),解决方案是使用增强型客户机,它允许您将java对象Map到表。有关详细信息,请参阅:
dynamodb表中的Map项
使用增强型clint,您可以使用以下代码修改项目:

  1. package com.example.dynamodb;
  2. import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
  3. import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
  4. import software.amazon.awssdk.enhanced.dynamodb.Key;
  5. import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
  6. import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
  7. import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;
  8. import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey;
  9. import software.amazon.awssdk.regions.Region;
  10. import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
  11. import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
  12. import java.time.Instant;
  13. /*
  14. * Prior to running this code example, create an Amazon DynamoDB table named Customer with these columns:
  15. * - id - the id of the record that is the key
  16. * - custName - the customer name
  17. * - email - the email value
  18. * - registrationDate - an instant value when the item was added to the table
  19. * Also, ensure that you have setup your development environment, including your credentials.
  20. *
  21. * For information, see this documentation topic:
  22. *
  23. * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
  24. */
  25. public class EnhancedModifyItem {
  26. public static void main(String[] args) {
  27. String usage = "Usage:\n" +
  28. " UpdateItem <key> <email> \n\n" +
  29. "Where:\n" +
  30. " key - the name of the key in the table (id120).\n" +
  31. " email - the value of the modified email column.\n" ;
  32. if (args.length != 2) {
  33. System.out.println(usage);
  34. System.exit(1);
  35. }
  36. String key = args[0];
  37. String email = args[1];
  38. Region region = Region.US_EAST_1;
  39. DynamoDbClient ddb = DynamoDbClient.builder()
  40. .region(region)
  41. .build();
  42. DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder()
  43. .dynamoDbClient(ddb)
  44. .build();
  45. String updatedValue = modifyItem(enhancedClient,key,email);
  46. System.out.println("The updated name value is "+updatedValue);
  47. ddb.close();
  48. }
  49. public static String modifyItem(DynamoDbEnhancedClient enhancedClient, String keyVal, String email) {
  50. try {
  51. //Create a DynamoDbTable object
  52. DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
  53. //Create a KEY object
  54. Key key = Key.builder()
  55. .partitionValue(keyVal)
  56. .build();
  57. // Get the item by using the key and update the email value.
  58. Customer customerRec = mappedTable.getItem(r->r.key(key));
  59. customerRec.setEmail(email);
  60. mappedTable.updateItem(customerRec);
  61. return customerRec.getEmail();
  62. } catch (DynamoDbException e) {
  63. System.err.println(e.getMessage());
  64. System.exit(1);
  65. }
  66. return "";
  67. }
  68. }

您可以在此处找到此v2示例和amazon dynamodb的其他示例:
https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/example_code/dynamodb

展开查看全部

相关问题