@Override public void update(Entry entry, Entry existingEntry) { readWriteLock.writeLock().lock(); try { Map<String, AttributeValue> keys = createKey(entry); Map<String, AttributeValueUpdate> attributes = createAttributes(entry); Map<String, ExpectedAttributeValue> expected = expectExists(existingEntry); try { executeUpdate(keys, attributes, expected); } catch (ConditionalCheckFailedException e) { throw new DoesNotExistException("Precondition to update entry in DynamoDB failed:" + keys.toString()); } } finally { readWriteLock.writeLock().unlock(); } }
@Override public void create(Entry entry) { readWriteLock.writeLock().lock(); try { Map<String, AttributeValue> keys = createKey(entry); Map<String, AttributeValueUpdate> attributes = createAttributes(entry); Map<String, ExpectedAttributeValue> expected = expectNotExists(); try { executeUpdate(keys, attributes, expected); } catch (ConditionalCheckFailedException e) { throw new AlreadyExistsException("DynamoDB store entry already exists:" + keys.toString()); } } finally { readWriteLock.writeLock().unlock(); } }
private Map<String, AttributeValueUpdate> createAttributes(Entry entry) { Map<String, AttributeValueUpdate> attributes = new HashMap<>(); attributes.put(SCHEMA_VERSION_FIELD_NAME, new AttributeValueUpdate() .withAction(AttributeAction.PUT) .withValue(new AttributeValue().withN(SCHEMA_VERSION))); attributes.put(OPTIMISTIC_LOCK_FIELD_NAME, new AttributeValueUpdate() .withAction(AttributeAction.PUT) .withValue(new AttributeValue().withS(sha(entry)))); for (Map.Entry<Integer, String> e : attributeMappings.entrySet()) { Object value = getValue(entry, e.getValue()); if (value != null) { attributes.put(e.getKey().toString(), new AttributeValueUpdate() .withAction(AttributeAction.PUT) .withValue(getAttribute(value))); } } return attributes; }
@Test public void createsOnlyThreeOpenLogs() throws Exception { final User user = new DyUser(new Dynamo(), "yegor256"); final Script script = user.script("test5"); final AttributeValueUpdate upd = new AttributeValueUpdate().withValue( new AttributeValue().withN( Long.toString(System.currentTimeMillis()) ) ).withAction(AttributeAction.PUT); // @checkstyle MagicNumber (1 line) for (int idx = 0; idx < 3; ++idx) { final Item item = script.open().iterator().next(); item.put("finish", upd); } MatcherAssert.assertThat( script.open(), Matchers.emptyIterable() ); }
public static void updateItem(AmazonDynamoDBClient client, String tableName, String id, String val) { java.util.Map<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("Id", new AttributeValue().withN(id)); Map<String, AttributeValueUpdate> attributeUpdates = new HashMap<String, AttributeValueUpdate>(); AttributeValueUpdate update = new AttributeValueUpdate() .withAction(AttributeAction.PUT) .withValue(new AttributeValue().withS(val)); attributeUpdates.put("attribute-2", update); UpdateItemRequest updateItemRequest = new UpdateItemRequest() .withTableName(tableName) .withKey(key) .withAttributeUpdates(attributeUpdates); client.updateItem(updateItemRequest); }
/** * Sends an update request to the service and returns true if the request is successful. */ public boolean sendUpdateRequest(Map<String, AttributeValue> primaryKey, Map<String, AttributeValueUpdate> updateItems, Map<String, ExpectedAttributeValue> expectedItems) throws Exception { if (updateItems.isEmpty()) { return false; // No update, return false } UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(tableName).withKey(primaryKey).withReturnValues(ReturnValue.UPDATED_NEW) .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withAttributeUpdates(updateItems); if (expectedItems != null) { updateItemRequest.withExpected(expectedItems); } UpdateItemResult result = dynamoDBClient.updateItem(updateItemRequest); if(!isRunningOnDDBLocal) { // DDB Local does not support rate limiting tableWriteRateLimiter.adjustRateWithConsumedCapacity(result.getConsumedCapacity()); } return true; }
protected Map<String, AttributeValueUpdate> genUpdateItemsForRecord() { Map<String, AttributeValueUpdate> updateItems = null; AttributeValue GSIHashKeyUpdateValue = tableWriter.genAttributeValueForGSIKey(options.getGsiHashKeyType(), getNextGsiHashKeyUpdateValue()); AttributeValue GSIRangeKeyUpdateValue = tableWriter.genAttributeValueForGSIKey(options.getGsiRangeKeyType(), getNextGsiRangeKeyUpdateValue()); // Find if gsi hash key/range key has violations. This will be needed when both hash and range violations // are to be found but only one has a violation. String gsiHashKeyName = getNextGsiHashKeyViolationType() == null ? null : options.getGsiHashKeyName(); String gsiRangeKeyName = getNextGsiRangeKeyViolationType() == null ? null : options.getGsiRangeKeyName(); boolean deleteBlank = getNextDeleteBlankAttribute(); if (deleteBlank) { updateItems = genUpdateItemsWithEmptyAttributeDeleted(gsiHashKeyName, GSIHashKeyUpdateValue, gsiRangeKeyName, GSIRangeKeyUpdateValue); } else { updateItems = genUpdateItemsWithEmptyAttributeKept(gsiHashKeyName, GSIHashKeyUpdateValue, gsiRangeKeyName, GSIRangeKeyUpdateValue); } return updateItems; }
/** * Do nothing to an attribute if update value for it is null. */ public Map<String, AttributeValueUpdate> genUpdateItemsWithEmptyAttributeKept(String GSIHashKeyName, AttributeValue GSIHashKeyUpdateValue, String GSIRangeKeyName, AttributeValue GSIRangeKeyUpdateValue) { Map<String, AttributeValueUpdate> updateItems = new HashMap<String, AttributeValueUpdate>(); boolean updateFound = false; if (GSIHashKeyName != null && GSIHashKeyUpdateValue != null) { updateItems.put(GSIHashKeyName, new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(GSIHashKeyUpdateValue)); updateFound = true; } if (GSIRangeKeyName != null && GSIRangeKeyUpdateValue != null) { updateItems.put(GSIRangeKeyName, new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(GSIRangeKeyUpdateValue)); updateFound = true; } if(updateFound) { violationUpdateRequests++; } return updateItems; }
@Test public void test_updateItem_WithAllParameters() throws Exception { createTable(); putItem(TEST_ATTRIBUTE, TEST_ATTRIBUTE_VALUE); String UPDATE_ATTRIBUTE_VALUE = "UpdateAttributeValue1"; Map<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put(TEST_ATTRIBUTE, new AttributeValue() .withS(TEST_ATTRIBUTE_VALUE)); Map<String, AttributeValueUpdate> attributeUpdates = new HashMap<String, AttributeValueUpdate>(); attributeUpdates.put(TEST_ATTRIBUTE, new AttributeValueUpdate() .withAction(AttributeAction.PUT) .withValue(new AttributeValue() .withS(UPDATE_ATTRIBUTE_VALUE))); String returnValues = ""; UpdateItemResult result = dynamoDb.updateItem(TEST_TABLE_NAME, key, attributeUpdates, returnValues); Double units = result.getConsumedCapacity().getCapacityUnits(); GetItemResult getItemResult = getItem(TEST_ATTRIBUTE, UPDATE_ATTRIBUTE_VALUE); String updatedValue = getItemResult.getItem().get(TEST_ATTRIBUTE).getS(); assertThat(units.doubleValue(), equalTo(1.0)); assertThat(updatedValue, equalTo(UPDATE_ATTRIBUTE_VALUE)); }
/** * Move or rename entity to other folder * * @param entity * - current entity want to move or rename * @param newParent * - parent of entity * @param newEntityName * - new name of entity * @param isRenamingAction * - TRUE is renaming file, otherwise FALSE * @return TRUE/FALSE */ @Override public boolean updateEntityByUniqueId(String tableName, Entity entity, Folder newParent, String newEntityName, boolean isRenamingAction) { HashMap<String, AttributeValue> primaryKey = new HashMap<String, AttributeValue>(); primaryKey.put(AttributeKey.UUID, new AttributeValue().withS(entity.getId().toString())); Map<String, AttributeValueUpdate> updateItems = new HashMap<String, AttributeValueUpdate>(); updateItems.put(AttributeKey.ENTITY_NAME, new AttributeValueUpdate() .withAction(AttributeAction.PUT).withValue(new AttributeValue().withS(newEntityName))); updateItems.put(AttributeKey.MODIFIED_DATE, new AttributeValueUpdate() .withAction(AttributeAction.PUT).withValue(new AttributeValue().withS(DateUtils.dateToString(new Date())))); if (!isRenamingAction) { updateItems.put(AttributeKey.PARENT_UUID, new AttributeValueUpdate() .withAction(AttributeAction.PUT).withValue(new AttributeValue() .withS(newParent.getId().toString()))); } UpdateItemResult updateStatus = dynamoDBService.updateItem(tableName, primaryKey, updateItems); if (updateStatus != null) { return true; } return false; }
private void updateRow(String key, String appid, Map<String, AttributeValue> row) { if (StringUtils.isBlank(key) || StringUtils.isBlank(appid) || row == null || row.isEmpty()) { return; } Map<String, AttributeValueUpdate> rou = new HashMap<>(); 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); } }
@Override public String toString() { final Collection<String> terms = new ArrayList<String>(this.attrs.size()); for (final Map.Entry<String, AttributeValueUpdate> attr : this.attrs.entrySet()) { terms.add( String.format( "%s=%s", attr.getKey(), attr.getValue() ) ); } return Joiner.on("; ").join(terms); }
/** * MkRegion can store and read items. * @throws Exception If some problem inside */ @Test public void storesAndReadsSingleAttribute() throws Exception { final String table = "ideas"; final String key = "number"; final String attr = "total"; final Region region = new MkRegion( new H2Data().with(table, new String[] {key}, attr) ); final Table tbl = region.table(table); tbl.put( new Attributes() .with(key, "32443") .with(attr, "0") ); final Item item = tbl.frame().iterator().next(); item.put( attr, new AttributeValueUpdate().withValue( new AttributeValue().withN("2") ).withAction(AttributeAction.PUT) ); MatcherAssert.assertThat(item.get(attr).getN(), Matchers.equalTo("2")); }
/** * AttributesUpdates can throw exception when put is called. */ @Test public void putThrowsException() { boolean passed; try { new AttributeUpdates().put( "key9", Mockito.mock(AttributeValueUpdate.class) ); passed = false; } catch (final UnsupportedOperationException ex) { passed = true; } if (!passed) { Assert.fail("#put should not be supported"); } }
private void executeUpdate(Map<String, AttributeValue> keys, Map<String, AttributeValueUpdate> attributes, Map<String, ExpectedAttributeValue> expected) { UpdateItemRequest updateEntry = new UpdateItemRequest() .withTableName(tableName) .withKey(keys) .withAttributeUpdates(attributes) .withExpected(expected); client.updateItem(updateEntry); }
@Override public void update(final String bash) throws IOException { this.item().put( "bash", new AttributeValueUpdate() .withValue(new AttributeValue().withS(bash)) .withAction(AttributeAction.PUT) ); }
@Override public void flush() throws IOException { this.item().put( "hour", new AttributeValueUpdate().withValue( new AttributeValue().withN("0") ).withAction(AttributeAction.PUT) ); }
@Override public void track(final long seconds) throws IOException { if (seconds > TimeUnit.MINUTES.toSeconds(2L)) { final Item item = this.item(); item.put( "used", new AttributeValueUpdate().withValue( new AttributeValue().withN(Long.toString(seconds)) ).withAction(AttributeAction.ADD) ); if (this.overdue() && item.has("stripe_customer")) { this.rebill(); } } }
@Override public void pay(final long cents, final String token, final String email) throws IOException { final String customer; try { customer = Customer.create( new StickyMap<String, Object>( new MapEntry<>("email", email), new MapEntry<>("source", token) ), new RequestOptions.RequestOptionsBuilder().setApiKey( Manifests.read("ThreeCopies-StripeSecret") ).build() ).getId(); } catch (final APIException | APIConnectionException | AuthenticationException | CardException | InvalidRequestException ex) { throw new IOException(ex); } this.item().put( new AttributeUpdates() .with( "stripe_cents", new AttributeValueUpdate().withValue( new AttributeValue().withN(Long.toString(cents)) ).withAction(AttributeAction.PUT) ) .with( "stripe_customer", new AttributeValueUpdate().withValue( new AttributeValue().withS(customer) ).withAction(AttributeAction.PUT) ) ); this.rebill(); }
/** * Charge him again. * @throws IOException If fails */ private void rebill() throws IOException { final Item item = this.item(); final Long cents = Long.parseLong(item.get("stripe_cents").getN()); final String customer = item.get("stripe_customer").getS(); try { Charge.create( new StickyMap<String, Object>( new MapEntry<>("amount", cents), new MapEntry<>("currency", "usd"), new MapEntry<>( "description", String.format("ThreeCopies: %s", this.name) ), new MapEntry<>("customer", customer) ), new RequestOptions.RequestOptionsBuilder().setApiKey( Manifests.read("ThreeCopies-StripeSecret") ).build() ); } catch (final APIException | APIConnectionException | AuthenticationException | CardException | InvalidRequestException ex) { throw new IOException(ex); } this.item().put( "paid", new AttributeValueUpdate().withValue( new AttributeValue().withN( Long.toString(cents * TimeUnit.HOURS.toSeconds(1L)) ) ).withAction(AttributeAction.ADD) ); }
/** * Make an update. * @param action The action * @return The update */ public AttributeValueUpdate update(final AttributeAction action) { return new AttributeValueUpdate() .withAction(action) .withValue( new AttributeValue().withN( Long.toString(this.longValue()) ) ); }
/** * Save XML. * @param xml The XML to save * @throws IOException If fails */ private void save(final String xml) throws IOException { this.item.put( "usage", new AttributeValueUpdate() .withValue(new AttributeValue().withS(xml)) .withAction(AttributeAction.PUT) ); }
/** * Save total. * @param total Total usage * @throws IOException If fails */ private void save(final long total) throws IOException { this.item.put( "total", new AttributeValueUpdate() .withValue(new AttributeValue().withN(Long.toString(total))) .withAction(AttributeAction.PUT) ); }
@Override public void status(final String text) throws IOException { this.item.put( "status", new AttributeValueUpdate() .withAction(AttributeAction.PUT) .withValue(new AttributeValue().withS(text)) ); }
@Override public void vote(final int points) throws IOException { this.item.put( "rank", new AttributeValueUpdate() .withAction(AttributeAction.ADD) .withValue(new AttributeValue().withN(Integer.toString(points))) ); }
public CompletableFuture<UpdateItemResult> updateItem(final String tableName, final Map<String, AttributeValue> key, final Map<String, AttributeValueUpdate> attributeUpdates) { return asyncExecutor.execute(new Callable<UpdateItemResult>() { @Override public UpdateItemResult call() throws Exception { return dbExecutor.updateItem(tableName, key, attributeUpdates); } }); }
public CompletableFuture<UpdateItemResult> updateItem(final String tableName, final Map<String, AttributeValue> key, final Map<String, AttributeValueUpdate> attributeUpdates, final String returnValues) { return asyncExecutor.execute(new Callable<UpdateItemResult>() { @Override public UpdateItemResult call() throws Exception { return dbExecutor.updateItem(tableName, key, attributeUpdates, returnValues); } }); }
@SafeVarargs public static Map<String, AttributeValueUpdate> asUpdateItem(Object... a) { if (0 != (a.length % 2)) { throw new IllegalArgumentException( "The parameters must be the pairs of property name and value, or Map, or an entity class with getter/setter methods."); } final Map<String, AttributeValueUpdate> item = new LinkedHashMap<>(N.initHashCapacity(a.length / 2)); for (int i = 0; i < a.length; i++) { item.put((String) a[i], attrValueUpdateOf(a[++i])); } return item; }
static List<Map<String, AttributeValueUpdate>> toUpdateItem(final Collection<?> entities, NamingPolicy namingPolicy) { final List<Map<String, AttributeValueUpdate>> attrsList = new ArrayList<>(entities.size()); for (Object entity : entities) { attrsList.add(toUpdateItem(entity, namingPolicy)); } return attrsList; }
@Test public void execute() { Map<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("1", new AttributeValue("Key_1")); exchange.getIn().setHeader(DdbConstants.KEY, key); Map<String, AttributeValueUpdate> attributeMap = new HashMap<String, AttributeValueUpdate>(); AttributeValueUpdate attributeValue = new AttributeValueUpdate( new AttributeValue("new value"), AttributeAction.ADD); attributeMap.put("name", attributeValue); exchange.getIn().setHeader(DdbConstants.UPDATE_VALUES, attributeMap); Map<String, ExpectedAttributeValue> expectedAttributeValueMap = new HashMap<String, ExpectedAttributeValue>(); expectedAttributeValueMap .put("name", new ExpectedAttributeValue(new AttributeValue("expected value"))); exchange.getIn().setHeader(DdbConstants.UPDATE_CONDITION, expectedAttributeValueMap); exchange.getIn().setHeader(DdbConstants.RETURN_VALUES, "ALL_OLD"); command.execute(); assertEquals("DOMAIN1", ddbClient.updateItemRequest.getTableName()); assertEquals(attributeMap, ddbClient.updateItemRequest.getAttributeUpdates()); assertEquals(key, ddbClient.updateItemRequest.getKey()); assertEquals(expectedAttributeValueMap, ddbClient.updateItemRequest.getExpected()); assertEquals("ALL_OLD", ddbClient.updateItemRequest.getReturnValues()); assertEquals(new AttributeValue("attrValue"), exchange.getIn().getHeader(DdbConstants.ATTRIBUTES, Map.class).get( "attrName")); }
public static int calculateItemUpdateSizeInBytes(final Map<String, AttributeValueUpdate> item) { int size = 0; if (item == null) { return size; } for (Map.Entry<String, AttributeValueUpdate> entry : item.entrySet()) { final String name = entry.getKey(); final AttributeValueUpdate update = entry.getValue(); size += name.getBytes(UTF8).length; size += calculateAttributeSizeInBytes(update.getValue()); } return size; }
@Override public Collection<MutateWorker> createMutationWorkers(final Map<StaticBuffer, KCVMutation> mutationMap, final DynamoDbStoreTransaction txh) { final List<MutateWorker> workers = Lists.newLinkedList(); for (Map.Entry<StaticBuffer, KCVMutation> entry : mutationMap.entrySet()) { final StaticBuffer hashKey = entry.getKey(); final KCVMutation mutation = entry.getValue(); final Map<String, AttributeValue> key = new ItemBuilder().hashKey(hashKey) .build(); // Using ExpectedAttributeValue map to handle large mutations in a single request // Large mutations would require multiple requests using expressions final Map<String, ExpectedAttributeValue> expected = new SingleExpectedAttributeValueBuilder(this, txh, hashKey).build(mutation); final Map<String, AttributeValueUpdate> attributeValueUpdates = new SingleUpdateBuilder().deletions(mutation.getDeletions()) .additions(mutation.getAdditions()) .build(); final UpdateItemRequest request = super.createUpdateItemRequest() .withKey(key) .withReturnValues(ReturnValue.ALL_NEW) .withAttributeUpdates(attributeValueUpdates) .withExpected(expected); final MutateWorker worker; if (mutation.hasDeletions() && !mutation.hasAdditions()) { worker = new SingleUpdateWithCleanupWorker(request, client.getDelegate()); } else { worker = new UpdateItemWorker(request, client.getDelegate()); } workers.add(worker); } return workers; }
public SingleUpdateBuilder put(final StaticBuffer column, final StaticBuffer value) { updates.put(encodeKeyBuffer(column), new AttributeValueUpdate() .withAction(AttributeAction.PUT) .withValue(encodeValue(value))); return this; }
@Override public Object handleRequest(Object input, Context context) { context.getLogger().log("input: " + input); if (input.toString().equals("{}") || input.toString().equals("")) { context.getLogger().log("input is empty: abort"); return "{\"status\":\"error\",\"message\":\"input at lambda function is empty\"}"; } dynamoDB = new AmazonDynamoDBClient().withRegion(Region .getRegion(Regions.EU_WEST_1)); HashMap<String, String> mapInput = (HashMap<String, String>) input; Map<String, AttributeValue> employeeKey = new HashMap<String, AttributeValue>(); String employeeId = mapInput.get("employee_id"); context.getLogger().log("employee_id: " + employeeId); employeeKey.put("employee_id", new AttributeValue().withS(employeeId)); Map<String, AttributeValueUpdate> attributeUpdates = new HashMap<String, AttributeValueUpdate>(); attributeUpdates.put("approval", new AttributeValueUpdate() .withValue(new AttributeValue().withS("approved"))); UpdateItemRequest updateItemRequest = new UpdateItemRequest() .withKey(employeeKey).withAttributeUpdates(attributeUpdates) .withTableName("lambda-reimbursment"); UpdateItemResult updateItemResult = dynamoDB .updateItem(updateItemRequest); context.getLogger().log("Result: " + updateItemResult); return "{'status':'done'}"; }
/** * Delete the attribute if update value for it is null. */ public Map<String, AttributeValueUpdate> genUpdateItemsWithEmptyAttributeDeleted(String GSIHashKeyName, AttributeValue GSIHashKeyUpdateValue, String GSIRangeKeyName, AttributeValue GSIRangeKeyUpdateValue) { Map<String, AttributeValueUpdate> updateItems = new HashMap<String, AttributeValueUpdate>(); boolean updateFound = false; if (GSIHashKeyName != null) { if (GSIHashKeyUpdateValue == null) { updateItems.put(GSIHashKeyName, new AttributeValueUpdate().withAction(AttributeAction.DELETE)); updateFound = true; } else { updateItems.put(GSIHashKeyName, new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(GSIHashKeyUpdateValue)); updateFound = true; } } if (GSIRangeKeyName != null) { if (GSIRangeKeyUpdateValue == null) { updateItems.put(GSIRangeKeyName, new AttributeValueUpdate().withAction(AttributeAction.DELETE)); updateFound = true; } else { updateItems.put(GSIRangeKeyName, new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(GSIRangeKeyUpdateValue)); updateFound = true; } } if(updateFound) { violationUpdateRequests++; } return updateItems; }
/** * Put expected items into the map only when it exists and the action is put */ protected Map<String, ExpectedAttributeValue> genExpectedItemsForRecord(Map<String, AttributeValueUpdate> updateItems) { Map<String, ExpectedAttributeValue> expectedItems = new HashMap<String, ExpectedAttributeValue>(); if (null != updateItems.get(options.getGsiHashKeyName())) { ExpectedAttributeValue gsiHashKeyExpectedValue = tableWriter.genExpectedAttributeValue(getNextGsiHashKey()); expectedItems.put(options.getGsiHashKeyName(), gsiHashKeyExpectedValue); } if (null != updateItems.get(options.getGsiRangeKeyName())) { ExpectedAttributeValue gsiRangeKeyExpectedValue = tableWriter.genExpectedAttributeValue(getNextGsiRangeKey()); expectedItems.put(options.getGsiRangeKeyName(), gsiRangeKeyExpectedValue); } return expectedItems; }
@Override public UpdateItemResult updateItem(String tableName, HashMap<String, AttributeValue> primaryKey, Map<String, AttributeValueUpdate> updateItems) { Map<String, AttributeValueUpdate> attributeValueUpdates = new HashMap<String, AttributeValueUpdate>(); attributeValueUpdates.putAll(updateItems); UpdateItemRequest updateItemRequest = new UpdateItemRequest() .withTableName(tableName) .withKey(primaryKey).withReturnValues(ReturnValue.UPDATED_NEW) .withAttributeUpdates(updateItems); UpdateItemResult updateItemResult = dynamoDBClient.updateItem(updateItemRequest); LOG.info("Successful by updating item from " + tableName + ": " + updateItemResult); return updateItemResult; }
/** * Update the Inventory table with the state of an Aggregator. * * @param streamName The Kinesis Stream being aggregated. * @param applicationName The application name running the aggregator. * @param workerId The worker ID which encapsulates an instance of an * Aggregator. * @param lastLowSeq The lowest sequence number observed in all records * which were flushed prior to this update. * @param lastHighSeq The highest sequence number for all records flushed in * this update. * @param lastWriteTime The write time of the data to Dynamo DB. * @param status The {@link STATE} of the Aggregator. * @throws Exception */ public void update(final String streamName, final String applicationName, final String namespace, final String shardId, final String lastLowSeq, final String lastHighSeq, final long lastWriteTime, final STATE status) throws Exception { // create the last write time value final String lastUpdateDateLabel = StreamAggregator.dateFormatter.format(new Date( lastWriteTime)); // generate the item update Map<String, AttributeValueUpdate> inventoryUpdate = new HashMap<String, AttributeValueUpdate>() { { put(InventoryModel.LAST_WRITE_TIME, new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue( new AttributeValue().withS(lastUpdateDateLabel))); if (lastLowSeq != null) put(InventoryModel.LAST_LOW_SEQ, new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue( new AttributeValue().withS(lastLowSeq))); if (lastHighSeq != null) put(InventoryModel.LAST_HIGH_SEQ, new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue( new AttributeValue().withS(lastHighSeq))); if (status != null) put(InventoryModel.STATUS, new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue( new AttributeValue().withS(status.name()))); } }; DynamoUtils.updateWithRetries( dynamoClient, new UpdateItemRequest().withTableName(InventoryModel.TABLE_NAME).withKey( getKey(streamName, applicationName, namespace, shardId)).withAttributeUpdates( inventoryUpdate)); }
void updateRow(String storeName, Map<String, AttributeValue> key, Map<String, AttributeValueUpdate> attributeUpdates) { String tableName = storeToTableName(storeName); m_logger.debug("Updating row in table {}, key={}", tableName, DynamoDBService.getDDBKey(key)); Timer timer = new Timer(); boolean bSuccess = false; for (int attempts = 1; !bSuccess; attempts++) { try { m_ddbClient.updateItem(tableName, key, attributeUpdates); if (attempts > 1) { m_logger.info("updateRow() succeeded on attempt #{}", attempts); } bSuccess = true; m_logger.debug("Time to update table {}, key={}: {}", new Object[]{tableName, DynamoDBService.getDDBKey(key), timer.toString()}); } catch (ProvisionedThroughputExceededException e) { if (attempts >= m_max_commit_attempts) { String errMsg = "All retries exceeded; abandoning updateRow() for table: " + tableName; m_logger.error(errMsg, e); throw new RuntimeException(errMsg, e); } m_logger.warn("updateRow() attempt #{} failed: {}", attempts, e); try { Thread.sleep(attempts * m_retry_wait_millis); } catch (InterruptedException ex2) { // ignore } } } }