/** * Save the item using a PutItem request. The handler will call this * method if * <ul> * <li> CLOBBER configuration is being used; * <li> OR the item contains auto-generated key value; * <li> OR an UpdateItem request has silently failed (200 response with * no affected attribute), which indicates the key-only-put scenario * that we used to handle by the keyOnlyPut(...) hack. * </ul> */ protected PutItemResult doPutItem() { Map<String, AttributeValue> attributeValues = convertToItem(getAttributeValueUpdates()); attributeValues = transformAttributes( toParameters(attributeValues, this.clazz, getTableName(), saveConfig)); PutItemRequest req = new PutItemRequest() .withTableName(getTableName()) .withItem(attributeValues) .withExpected(mergeExpectedAttributeValueConditions()) .withConditionalOperator(userProvidedConditionOperator) .withRequestMetricCollector(saveConfig.getRequestMetricCollector()); return db.putItem(applyUserAgent(req)); }
private PutItemOutcome doPutItem(PutItemSpec spec) { // set the table name String tableName = getTable().getTableName(); PutItemRequest req = spec.getRequest().withTableName(tableName); // set up the item Item item = spec.getItem(); final Map<String,AttributeValue> attributes = InternalUtils.toAttributeValues(item); // set up the expected attribute map, if any final Map<String, ExpectedAttributeValue> expectedMap = InternalUtils.toExpectedAttributeValueMap(spec.getExpected()); // set up the value map, if any (when expression API is used) final Map<String,AttributeValue> attrValMap = InternalUtils.fromSimpleMap(spec.getValueMap()); // set up the request req.withItem(attributes) .withExpected(expectedMap) .withExpressionAttributeNames(spec.getNameMap()) .withExpressionAttributeValues(attrValMap) ; PutItemResult result = getClient().putItem(req); return new PutItemOutcome(result); } }
@Override public void saveAttributes(RequestEnvelope envelope, Map<String, Object> attributes) throws PersistenceException { String partitionKey = partitionKeyGenerator.apply(envelope); PutItemRequest request = new PutItemRequest() .withTableName(tableName) .withItem(getItem(partitionKey, attributes)); try { dynamoDb.putItem(request); } catch (ResourceNotFoundException e) { throw new PersistenceException(String.format("Table %s does not exist or is in the process of being created", tableName), e); } catch (AmazonDynamoDBException e) { throw new PersistenceException("Failed to save attributes to DynamoDB", e); } }
/** * Save the item using a PutItem request. The handler will call this * method if * <ul> * <li>CLOBBER configuration is being used; * <li>OR the item contains auto-generated key value; * <li>OR an UpdateItem request has silently failed (200 response with * no affected attribute), which indicates the key-only-put scenario * that we used to handle by the keyOnlyPut(...) hack. * </ul> */ protected PutItemResult doPutItem() { Map<String, AttributeValue> attributeValues = convertToItem(getAttributeValueUpdates()); attributeValues = transformAttributes( toParameters(attributeValues, this.clazz, getTableName(), saveConfig)); final PutItemRequest req = new PutItemRequest() .withTableName(getTableName()) .withItem(attributeValues) .withExpected(mergeExpectedAttributeValueConditions()) .withConditionalOperator(userProvidedConditionOperator) .withRequestMetricCollector(saveConfig.getRequestMetricCollector()); return db.putItem(applyUserAgent(req)); }
/** * Puts a document into DynamoDB. * * @param document the {@link Document}. * @param config {@link PutItemOperationConfig}. * @return the dynamodb {@link Document}. */ public Document putItem(Document document, PutItemOperationConfig config) { final PutItemRequest request = new PutItemRequest(); request.setTableName(this.tableName); request.setItem(toAttributeMap(document)); if (config != null && config.getReturnValue().compareTo(ReturnValue.ALL_OLD) == 0) { request.setReturnValues(ReturnValue.ALL_OLD); } Table.appendDynamoDBDocumentUserAgentString(request); final PutItemResult result = client.putItem(request); Document ret = null; if (config != null && config.getReturnValue().compareTo(ReturnValue.ALL_OLD) == 0) { final Map<String, AttributeValue> values = result.getAttributes(); ret = this.fromAttributeMap(values); ret.commit(); } return ret; }
@Override public PutItemResult putItem(String tableName, Map<String, AttributeValue> item) throws AmazonServiceException, AmazonClientException { return getBackend().putItem(tableName, item); }
@Override public PutItemResult putItem(String tableName, Map<String, AttributeValue> item, String returnValues) throws AmazonServiceException, AmazonClientException { return getBackend().putItem(tableName, item, returnValues); }
@Override public PutItemResult putItem(PutItemRequest request) throws AmazonServiceException, AmazonClientException { return getBackend().putItem(request); }
private Map<String, AttributeValue> conditionalPut(final Map<String, AttributeValue> item) { try { final PutItemRequest put = new PutItemRequest().withTableName(tableName).withItem(item) .withExpected(doesNotExist); ddb.putItem(put); return item; } catch (final ConditionalCheckFailedException ex) { final Map<String, AttributeValue> ddbKey = new HashMap<String, AttributeValue>(); ddbKey.put(DEFAULT_HASH_KEY, item.get(DEFAULT_HASH_KEY)); ddbKey.put(DEFAULT_RANGE_KEY, item.get(DEFAULT_RANGE_KEY)); return ddbGet(ddbKey); } }
private Map<String, AttributeValue> conditionalPut(final Map<String, AttributeValue> item) { try { final PutItemRequest put = new PutItemRequest().withTableName(tableName).withItem(item) .withExpected(doesNotExist); ddb.putItem(put); return item; } catch (final ConditionalCheckFailedException ex) { final Map<String, AttributeValue> ddbKey = new HashMap<>(); ddbKey.put(DEFAULT_HASH_KEY, item.get(DEFAULT_HASH_KEY)); ddbKey.put(DEFAULT_RANGE_KEY, item.get(DEFAULT_RANGE_KEY)); return ddbGet(ddbKey); } }
private String createRow(String key, String appid, Map<String, AttributeValue> row) { if (StringUtils.isBlank(key) || StringUtils.isBlank(appid) || row == null || row.isEmpty()) { return null; } try { key = getKeyForAppid(key, appid); setRowKey(key, row); PutItemRequest putItemRequest = new PutItemRequest(getTableNameForAppid(appid), row); client().putItem(putItemRequest); } catch (Exception e) { logger.error("Could not write row to DB - appid={}, key={}", appid, key, e); throwIfNecessary(e); } return key; }
private String createRow(String key, String appid, Map<String, AttributeValue> row) { if (StringUtils.isBlank(key) || StringUtils.isBlank(appid) || row == null || row.isEmpty()) { return null; } try { key = getKeyForAppid(key, appid); setRowKey(key, row); PutItemRequest putItemRequest = new PutItemRequest(getTableNameForAppid(appid), row); client().putItem(putItemRequest); } catch (Exception e) { logger.error("Could not write row to DB - appid={}, key={}", appid, key, e); throwIfNecessary(e); } return key; }
@Override public void saveAttributes(RequestEnvelope envelope, Map<String, Object> attributes) throws PersistenceException { String partitionKey = partitionKeyGenerator.apply(envelope); PutItemRequest request = new PutItemRequest() .withTableName(tableName) .withItem(getItem(partitionKey, attributes)); try { dynamoDb.putItem(request); } catch (ResourceNotFoundException e) { throw new PersistenceException(String.format("Table %s does not exist or is in the process of being created", tableName), e); } catch (AmazonDynamoDBException e) { throw new PersistenceException("Failed to save attributes to DynamoDB", e); } }
@Override protected void append(E event) { if (this.id == -1) { this.id = getLastId(outputTableName, instanceName, dynamoClient); if (this.id == -1) { addWarn("Could not initialize " + DynamoDBLogbackAppender.class.getCanonicalName() + " ( will try to initialize again later ): "); return; } } Map<String, AttributeValue> data = new HashMap<String, AttributeValue>(4); data.put("instance", new AttributeValue().withS(instanceName)); data.put("id", new AttributeValue().withN(String.valueOf(++id))); data.put("msg", new AttributeValue().withS(new String(encoder.encode(event)))); PutItemRequest itemRequest = new PutItemRequest().withTableName(outputTableName).withItem(data); dynamoClient.putItem(itemRequest); }
@Override public String apply(String key) { PutItemRequest request = new PutItemRequest() .withTableName(tableName) .withReturnConsumedCapacity(returnConsumedCapacity) .addItemEntry(partitionKeyName, new AttributeValue().withS(key)) .addItemEntry(ATTRIBUTE_NAME, new AttributeValue().withS(dataGenerator.getRandomValue())); try { // Write the item to the table return Optional.ofNullable(dynamoDB.putItem(request)) .map(this::measureConsumedCapacity) .map(PutItemResult::toString) .orElse(null); } catch (AmazonServiceException ase) { throw amazonServiceException(ase); } catch (AmazonClientException ace) { throw amazonClientException(ace); } }
@Override public void execute() throws MetaModelException { final Map<String, AttributeValue> itemValues = new HashMap<>(); final Column[] columns = getColumns(); final Object[] values = getValues(); for (int i = 0; i < columns.length; i++) { final Column column = columns[i]; final Object value = values[i]; if (column.isPrimaryKey() && value == null) { throw new IllegalArgumentException("Value for '" + column.getName() + "' cannot be null"); } final AttributeValue attributeValue = DynamoDbUtils.toAttributeValue(value); itemValues.put(column.getName(), attributeValue); } final AmazonDynamoDB dynamoDb = getUpdateCallback().getDataContext().getDynamoDb(); dynamoDb.putItem(getTable().getName(), itemValues); } }
/** * This API retrieves the intermediate keys from the source region and replicates it in the target region. * * @param materialName material name of the encryption material. * @param version version of the encryption material. * @param targetMetaStore target MetaStore where the encryption material to be stored. */ public void replicate(final String materialName, final long version, final MetaStore targetMetaStore) { try { Map<String, AttributeValue> item = getMaterialItem(materialName, version); final Map<String, AttributeValue> plainText = getPlainText(item); final Map<String, AttributeValue> encryptedText = targetMetaStore.getEncryptedText(plainText); final PutItemRequest put = new PutItemRequest().withTableName(targetMetaStore.tableName).withItem(encryptedText) .withExpected(doesNotExist); targetMetaStore.ddb.putItem(put); } catch (ConditionalCheckFailedException e) { //Item already present. } }
public PutItemResult putItem(final PutItemRequest request) throws BackendException { setUserAgent(request); PutItemResult result; final int bytes = calculateItemSizeInBytes(request.getItem()); getBytesHistogram(PUT_ITEM, request.getTableName()).update(bytes); final int wcu = computeWcu(bytes); timedWriteThrottle(PUT_ITEM, request.getTableName(), wcu); final Timer.Context apiTimerContext = getTimerContext(PUT_ITEM, request.getTableName()); try { result = client.putItem(request); } catch (Exception e) { throw processDynamoDbApiException(e, PUT_ITEM, request.getTableName()); } finally { apiTimerContext.stop(); } meterConsumedCapacity(PUT_ITEM, result.getConsumedCapacity()); return result; }
/** * Save the item using a PutItem request. The handler will call this * method if * <ul> * <li> CLOBBER configuration is being used; * <li> OR the item contains auto-generated key value; * <li> OR an UpdateItem request has silently failed (200 response with * no affected attribute), which indicates the key-only-put scenario * that we used to handle by the keyOnlyPut(...) hack. * </ul> */ protected PutItemResult doPutItem() { Map<String, AttributeValue> attributeValues = convertToItem(getAttributeValueUpdates()); attributeValues = transformAttributes( toParameters(attributeValues, this.clazz, getTableName(), saveConfig)); PutItemRequest req = new PutItemRequest() .withTableName(getTableName()) .withItem(attributeValues) .withExpected(mergeExpectedAttributeValueConditions()) .withConditionalOperator(userProvidedConditionOperator) .withRequestMetricCollector(saveConfig.getRequestMetricCollector()); return db.putItem(applyUserAgent(req)); }
private PutItemOutcome doPutItem(PutItemSpec spec) { // set the table name String tableName = getTable().getTableName(); PutItemRequest req = spec.getRequest().withTableName(tableName); // set up the item Item item = spec.getItem(); final Map<String,AttributeValue> attributes = InternalUtils.toAttributeValues(item); // set up the expected attribute map, if any final Map<String, ExpectedAttributeValue> expectedMap = InternalUtils.toExpectedAttributeValueMap(spec.getExpected()); // set up the value map, if any (when expression API is used) final Map<String,AttributeValue> attrValMap = InternalUtils.fromSimpleMap(spec.getValueMap()); // set up the request req.withItem(attributes) .withExpected(expectedMap) .withExpressionAttributeNames(spec.getNameMap()) .withExpressionAttributeValues(attrValMap) ; PutItemResult result = getClient().putItem(req); return new PutItemOutcome(result); } }