List of usage examples for com.amazonaws.services.dynamodbv2.model AttributeAction PUT
AttributeAction PUT
To view the source code for com.amazonaws.services.dynamodbv2.model AttributeAction PUT.
Click Source Link
From source file:aws.example.dynamodb.UpdateItem.java
License:Open Source License
public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " UpdateItem <table> <name> <greeting>\n\n" + "Where:\n" + " table - the table to put the item in.\n" + " name - a name to update in the table. The name must exist,\n" + " or an error will result.\n" + "Additional fields can be specified by appending them to the end of the\n" + "input.\n\n" + "Examples:\n" + " UpdateItem SiteColors text default=000000 bold=b22222\n" + " UpdateItem SiteColors background default=eeeeee code=d3d3d3\n\n"; if (args.length < 3) { System.out.println(USAGE); System.exit(1);//from www.j a v a 2s . co m } String table_name = args[0]; String name = args[1]; ArrayList<String[]> extra_fields = new ArrayList<String[]>(); // any additional args (fields to add or update)? for (int x = 2; x < args.length; x++) { String[] fields = args[x].split("=", 2); if (fields.length == 2) { extra_fields.add(fields); } else { System.out.format("Invalid argument: %s\n", args[x]); System.out.println(USAGE); System.exit(1); } } System.out.format("Updating \"%s\" in %s\n", name, table_name); if (extra_fields.size() > 0) { System.out.println("Additional fields:"); for (String[] field : extra_fields) { System.out.format(" %s: %s\n", field[0], field[1]); } } HashMap<String, AttributeValue> item_key = new HashMap<String, AttributeValue>(); item_key.put("Name", new AttributeValue(name)); HashMap<String, AttributeValueUpdate> updated_values = new HashMap<String, AttributeValueUpdate>(); for (String[] field : extra_fields) { updated_values.put(field[0], new AttributeValueUpdate(new AttributeValue(field[1]), AttributeAction.PUT)); } final AmazonDynamoDBClient ddb = new AmazonDynamoDBClient(); try { ddb.updateItem(table_name, item_key, updated_values); } catch (ResourceNotFoundException e) { System.err.println(e.getMessage()); System.exit(1); } catch (AmazonServiceException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("Done!"); }
From source file:com.amazon.janusgraph.diskstorage.dynamodb.builder.SingleUpdateBuilder.java
License:Open Source License
public SingleUpdateBuilder put(final StaticBuffer column, final StaticBuffer value) { updates.put(encodeKeyBuffer(column), new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(encodeValue(value))); return this; }
From source file:com.clicktravel.infrastructure.persistence.aws.dynamodb.DynamoDbTemplate.java
License:Apache License
private Map<String, AttributeValueUpdate> getAttributeUpdateMap(final Item item, final ItemConfiguration itemConfiguration, final Long version) { final Map<String, AttributeValueUpdate> attributeMap = new HashMap<>(); for (final PropertyDescriptor propertyDescriptor : itemConfiguration.propertyDescriptors()) { final String propertyName = propertyDescriptor.getName(); if (propertyName.equals(VERSION_ATTRIBUTE)) { attributeMap.put(propertyName, new AttributeValueUpdate().withAction(AttributeAction.PUT) .withValue(new AttributeValue().withN(String.valueOf(version)))); } else if (propertyDescriptor.getWriteMethod() != null) { final AttributeValue attributeValue = DynamoDbPropertyMarshaller.getValue(item, propertyDescriptor); if (attributeMap != null) { // TODO Only add to attribute map if there is a difference if (attributeValue != null) { attributeMap.put(propertyName, new AttributeValueUpdate().withAction(AttributeAction.PUT) .withValue(attributeValue)); } else { attributeMap.put(propertyName, new AttributeValueUpdate().withAction(AttributeAction.DELETE)); }/*from ww w. j a va 2s . c o m*/ } } } if (VariantItemConfiguration.class.isAssignableFrom(itemConfiguration.getClass())) { final VariantItemConfiguration variantItemConfiguration = (VariantItemConfiguration) itemConfiguration; attributeMap.put(variantItemConfiguration.parentItemConfiguration().discriminator(), new AttributeValueUpdate().withAction(AttributeAction.PUT) .withValue(new AttributeValue(variantItemConfiguration.discriminatorValue()))); } return attributeMap; }
From source file:com.dell.doradus.db.dynamodb.DDBTransaction.java
License:Apache License
private void updateRowColumnUpdates(String storeName, Map<String, AttributeValue> key, List<DColumn> colList) { Map<String, AttributeValueUpdate> attributeUpdates = new HashMap<>(); for (DColumn col : colList) { AttributeValue attrValue = mapColumnValue(storeName, col); attributeUpdates.put(col.getName(), new AttributeValueUpdate(attrValue, AttributeAction.PUT)); }/*from w w w .j a v a2 s. c o m*/ m_service.updateRow(storeName, key, attributeUpdates); }
From source file:com.erudika.para.persistence.AWSDynamoDAO.java
License:Apache License
private void updateRow(String key, String appid, Map<String, AttributeValue> row) { if (StringUtils.isBlank(key) || StringUtils.isBlank(appid) || row == null || row.isEmpty()) { return;//from w ww .j ava2s.c om } Map<String, AttributeValueUpdate> rou = new HashMap<String, AttributeValueUpdate>(); try { for (Entry<String, AttributeValue> attr : row.entrySet()) { rou.put(attr.getKey(), new AttributeValueUpdate(attr.getValue(), AttributeAction.PUT)); } UpdateItemRequest updateItemRequest = new UpdateItemRequest(getTableNameForAppid(appid), Collections.singletonMap(Config._KEY, new AttributeValue(getKeyForAppid(key, appid))), rou); client().updateItem(updateItemRequest); } catch (Exception e) { logger.error("Could not update row in DB - appid={}, key={}", appid, key, e); } }
From source file:com.lvl6.mobsters.dynamo.setup.TransactionExamples.java
License:Open Source License
/** * This example shows that reads can be performed in a transaction, and read locks can be upgraded to write locks. *///from w w w . jav a 2s . com public void readThenWrite() { print("\n*** readThenWrite() ***\n"); Transaction t1 = txManager.newTransaction(); // Perform a GetItem request on the transaction print("Reading Item1"); Map<String, AttributeValue> key1 = new HashMap<String, AttributeValue>(); key1.put(EXAMPLE_TABLE_HASH_KEY, new AttributeValue("Item1")); Map<String, AttributeValue> item1 = t1 .getItem(new GetItemRequest().withKey(key1).withTableName(EXAMPLE_TABLE_NAME)).getItem(); print("Item1: " + item1); // Now call UpdateItem to add a new attribute. // Notice that the library supports ReturnValues in writes print("Updating Item1"); Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(); updates.put("Color", new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue("Green"))); item1 = t1.updateItem(new UpdateItemRequest().withKey(key1).withTableName(EXAMPLE_TABLE_NAME) .withAttributeUpdates(updates).withReturnValues(ReturnValue.ALL_NEW)).getAttributes(); print("Item1 is now: " + item1); t1.commit(); t1.delete(); }
From source file:com.lvl6.mobsters.dynamo.setup.TransactionExamples.java
License:Open Source License
/** * Demonstrates the 3 levels of supported read isolation: Uncommitted, Committed, Locked *///from w ww. j av a 2 s . com public void reading() { print("\n*** reading() ***\n"); // First, create a new transaction and update Item1, but don't commit yet. print("Starting a transaction to modify Item1"); Transaction t1 = txManager.newTransaction(); Map<String, AttributeValue> key1 = new HashMap<String, AttributeValue>(); key1.put(EXAMPLE_TABLE_HASH_KEY, new AttributeValue("Item1")); // Show the current value before any changes print("Getting the current value of Item1 within the transaction. This is the strongest form of read isolation."); print(" However, you can't trust the value you get back until your transaction commits!"); Map<String, AttributeValue> item1 = t1 .getItem(new GetItemRequest().withKey(key1).withTableName(EXAMPLE_TABLE_NAME)).getItem(); print("Before any changes, Item1 is: " + item1); // Show the current value before any changes print("Changing the Color of Item1, but not committing yet"); Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(); updates.put("Color", new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue("Purple"))); item1 = t1.updateItem(new UpdateItemRequest().withKey(key1).withTableName(EXAMPLE_TABLE_NAME) .withAttributeUpdates(updates).withReturnValues(ReturnValue.ALL_NEW)).getAttributes(); print("Item1 is not yet committed, but if committed, will be: " + item1); // Perform an Uncommitted read print("The weakest (but cheapest) form of read is Uncommitted, where you can get back changes that aren't yet committed"); print(" And might be rolled back!"); item1 = txManager.getItem(new GetItemRequest() // Uncommitted reads happen on the transaction manager, not on a transaction. .withKey(key1).withTableName(EXAMPLE_TABLE_NAME), IsolationLevel.UNCOMMITTED).getItem(); // Note the isloationLevel print("The read, which could return changes that will be rolled back, returned: " + item1); // Perform a Committed read print("A strong read is Committed. This means that you are guaranteed to read only committed changes,"); print(" but not necessarily the *latest* committed change!"); item1 = txManager.getItem(new GetItemRequest() // Uncommitted reads happen on the transaction manager, not on a transaction. .withKey(key1).withTableName(EXAMPLE_TABLE_NAME), IsolationLevel.COMMITTED).getItem(); // Note the isloationLevel print("The read, which should return the same value of the original read, returned: " + item1); // Now start a new transaction and do a read, write, and read in it Transaction t2 = txManager.newTransaction(); print("Getting Item1, but this time under a new transaction t2"); item1 = t2.getItem(new GetItemRequest().withKey(key1).withTableName(EXAMPLE_TABLE_NAME)).getItem(); print("Before any changes, in t2, Item1 is: " + item1); print(" This just rolled back transaction t1! Notice that this is the same value as *before* t1!"); updates = new HashMap<String, AttributeValueUpdate>(); updates.put("Color", new AttributeValueUpdate().withAction(AttributeAction.PUT) .withValue(new AttributeValue("Magenta"))); print("Updating item1 again, but now under t2"); item1 = t2.updateItem(new UpdateItemRequest().withKey(key1).withTableName(EXAMPLE_TABLE_NAME) .withAttributeUpdates(updates).withReturnValues(ReturnValue.ALL_NEW)).getAttributes(); print("Item1 is not yet committed, but if committed, will now be: " + item1); print("Getting Item1, again, under lock in t2. Notice that the transaction library makes your write during this transaction visible to future reads."); item1 = t2.getItem(new GetItemRequest().withKey(key1).withTableName(EXAMPLE_TABLE_NAME)).getItem(); print("Under transaction t2, Item1 is going to be: " + item1); print("Committing t2"); t2.commit(); try { print("Committing t1 (this will fail because it was rolled back)"); t1.commit(); throw new RuntimeException("Should have been rolled back"); } catch (TransactionRolledBackException e) { print("t1 was rolled back as expected. I hope you didn't act on the GetItem you did under the lock in t1!"); } }
From source file:com.netflix.config.sources.DynamoDbIntegrationTestHelper.java
License:Apache License
static void updateValues(AmazonDynamoDB dbClient, String tableName) { Map<String, AttributeValue> key1 = new HashMap<String, AttributeValue>(1); key1.put("test1", new AttributeValue().withS("HASH")); Map<String, AttributeValueUpdate> item1 = new HashMap<String, AttributeValueUpdate>(1); item1.put(DynamoDbConfigurationSource.defaultValueAttribute, new AttributeValueUpdate() .withAction(AttributeAction.PUT).withValue(new AttributeValue().withS("vala"))); dbClient.updateItem(//from ww w . j a va 2 s .co m new UpdateItemRequest().withTableName(tableName).withKey(key1).withAttributeUpdates(item1)); Map<String, AttributeValue> key2 = new HashMap<String, AttributeValue>(1); key2.put("test2", new AttributeValue().withS("HASH")); HashMap<String, AttributeValueUpdate> item2 = new HashMap<String, AttributeValueUpdate>(1); item2.put(DynamoDbConfigurationSource.defaultValueAttribute, new AttributeValueUpdate() .withAction(AttributeAction.PUT).withValue(new AttributeValue().withS("valb"))); dbClient.updateItem( new UpdateItemRequest().withTableName(tableName).withKey(key2).withAttributeUpdates(item2)); Map<String, AttributeValue> key3 = new HashMap<String, AttributeValue>(1); key3.put("test3", new AttributeValue().withS("HASH")); HashMap<String, AttributeValueUpdate> item3 = new HashMap<String, AttributeValueUpdate>(1); item3.put(DynamoDbConfigurationSource.defaultValueAttribute, new AttributeValueUpdate() .withAction(AttributeAction.PUT).withValue(new AttributeValue().withS("valc"))); dbClient.updateItem( new UpdateItemRequest().withTableName(tableName).withKey(key3).withAttributeUpdates(item3)); }
From source file:com.rapid7.diskstorage.dynamodb.builder.SingleUpdateBuilder.java
License:Open Source License
public SingleUpdateBuilder put(StaticBuffer column, StaticBuffer value) { updates.put(encodeKeyBuffer(column), new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(encodeValue(value))); return this; }
From source file:com.vitembp.embedded.configuration.CloudConfigSync.java
License:Open Source License
/** * Checks if the configuration has changed. *//*from w ww . j a v a 2 s. c om*/ private static void checkConfiguration() { // register the device if it is not in the table if (!CloudConfigSync.deviceRegistered) { try { CloudConfigSync.registerDevice(); } catch (IOException ex) { // log the error LOGGER.error("Could not register device.", ex); // do not continue we cannot update config if it is not registered return; } } // check if configuration has changed boolean hasChanged; String remoteConfig; // perform query Map<String, AttributeValue> reqkey = new HashMap<>(); reqkey.put("ID", new AttributeValue().withS(SystemConfig.getConfig().getSystemUUID().toString())); GetItemRequest request = new GetItemRequest().withTableName("DEVICES").withKey(reqkey) .withProjectionExpression("CONFIG,UPDATED"); // try to get the data GetItemResult result = client.getItem(request); if (result != null && result.getItem() != null) { // parse data from response if the table has the attributes Map<String, AttributeValue> attributes = result.getItem(); // can not continue if the UPDATED and CONFIG attributes are not // present in the table row if (!attributes.containsKey("UPDATED") || !attributes.containsKey("CONFIG")) { LOGGER.error("Configuration is not available in database."); return; } // attributes are present, get their values hasChanged = Boolean.parseBoolean(result.getItem().get("UPDATED").getS()); remoteConfig = result.getItem().get("CONFIG").getS(); } else { // query failed so do not continue LOGGER.error("Could not query configuration in databse."); return; } // update if tagged as changed, prefering the remote config over the local if (hasChanged) { try { loadConfigFromRemote(remoteConfig); } catch (XMLStreamException ex) { LOGGER.error("Could not update configuration.", ex); return; } // change updated to false Map<String, AttributeValue> keyAttribs = new HashMap<>(); keyAttribs.put("ID", new AttributeValue().withS(SystemConfig.getConfig().getSystemUUID().toString())); Map<String, AttributeValueUpdate> updateAttribs = new HashMap<>(); updateAttribs.put("UPDATED", new AttributeValueUpdate().withValue(new AttributeValue().withS(Boolean.toString(false))) .withAction(AttributeAction.PUT)); client.updateItem("DEVICES", keyAttribs, updateAttribs); LOGGER.info("Config updated flag set to false."); } else { // build local config to check if the local has changes not // in the remote config String localConfig; try { localConfig = SystemConfig.getConfig().writeToString(); } catch (XMLStreamException ex) { LOGGER.error("Could not create configuration string.", ex); return; } // update remote as needed if (!remoteConfig.equals(localConfig)) { // updated configuration with local Map<String, AttributeValue> keyAttribs = new HashMap<>(); keyAttribs.put("ID", new AttributeValue().withS(SystemConfig.getConfig().getSystemUUID().toString())); Map<String, AttributeValueUpdate> updateAttribs = new HashMap<>(); updateAttribs.put("CONFIG", new AttributeValueUpdate() .withValue(new AttributeValue().withS(localConfig)).withAction(AttributeAction.PUT)); client.updateItem("DEVICES", keyAttribs, updateAttribs); LOGGER.info("Remote config updated."); } } }