Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. /**
  2. * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * This file is licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License. A copy of
  6. * the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0/
  9. *
  10. * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  11. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  12. * specific language governing permissions and limitations under the License.
  13. */
  14.  
  15.  
  16.  
  17. package com.amazonaws.codesamples.gsg;
  18.  
  19. import com.amazonaws.client.builder.AwsClientBuilder;
  20. import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
  21. import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
  22. import com.amazonaws.services.dynamodbv2.document.DynamoDB;
  23. import com.amazonaws.services.dynamodbv2.document.PrimaryKey;
  24. import com.amazonaws.services.dynamodbv2.document.Table;
  25. import com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome;
  26. import com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec;
  27. import com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
  28. import com.amazonaws.services.dynamodbv2.model.ReturnValue;
  29.  
  30. public class MoviesItemOps05 {
  31.  
  32. public static void main(String[] args) throws Exception {
  33.  
  34. AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
  35. .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
  36. .build();
  37.  
  38. DynamoDB dynamoDB = new DynamoDB(client);
  39.  
  40. Table table = dynamoDB.getTable("Movies");
  41.  
  42. int year = 2015;
  43. String title = "The Big New Movie";
  44.  
  45. UpdateItemSpec updateItemSpec = new UpdateItemSpec()
  46. .withPrimaryKey(new PrimaryKey("year", year, "title", title)).withUpdateExpression("remove info.actors[0]")
  47. .withConditionExpression("size(info.actors) > :num").withValueMap(new ValueMap().withNumber(":num", 3))
  48. .withReturnValues(ReturnValue.UPDATED_NEW);
  49.  
  50. // Conditional update (we expect this to fail)
  51. try {
  52. System.out.println("Attempting a conditional update...");
  53. UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
  54. System.out.println("UpdateItem succeeded:\n" + outcome.getItem().toJSONPretty());
  55.  
  56. }
  57. catch (Exception e) {
  58. System.err.println("Unable to update item: " + year + " " + title);
  59. System.err.println(e.getMessage());
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement