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; }
/** * Put given item into the table. If the item exists, it replaces the entire * item. Instead of replacing the entire item, if you want to update only * specific attributes, you can use the updateItem method. * * @param item */ @Override public PutItemResult putItem(String tableName, Map<String, AttributeValue> item) { if (item == null || item.isEmpty()) { LOG.warn("Does not support store null or empty entity in table " + tableName); return null; } LOG.info("Successfully putted item " + item.toString() + " into " + tableName); try { PutItemRequest putItemRequest = new PutItemRequest(tableName, item); PutItemResult putItemResult = dynamoDBClient.putItem(putItemRequest); LOG.info("Putted status: " + putItemResult); return putItemResult; } catch (AmazonServiceException ase) { LOG.error("Failed to put given item into the " + tableName, ase); } catch (AmazonClientException ace) { LOG.error("Failed to put given item into the " + tableName, ace); } return null; }
@Override public boolean insert(LandUnit lu) { boolean retval = false; Map<String, AttributeValue> item = new HashMap<String, AttributeValue>(); item.put(LandUnit.ID_ATTR_NAME, new AttributeValue(Long.toString(lu.getUnitId()))); item.put(LandUnit.USER_ID_ATTR_NAME, new AttributeValue(Long.toString(lu.getUserId()))); item.put(LandUnit.NAME_ATTR_NAME, new AttributeValue(lu.getName())); item.put(LandUnit.FARM_NAME_ATTR_NAME, new AttributeValue(lu.getFarmName())); item.put(LandUnit.CLIENT_NAME_ATTR_NAME, new AttributeValue(lu.getClientName())); item.put(LandUnit.ACRES_ATTR_NAME, new AttributeValue(Float.toString(lu.getAcres()))); item.put(LandUnit.SOURCE_ATTR_NAME, new AttributeValue(lu.getSource())); item.put(LandUnit.OTHER_PROPS_ATTR_NAME, new AttributeValue(lu.getOtherProps())); item.put(LandUnit.GEOM_ATTR_NAME, new AttributeValue(lu.getWktBoundary())); PutItemRequest putItemRequest = new PutItemRequest(LANDUNIT_DYNAMO_DB_TABLE_NAME, item); try { PutItemResult putItemResult = dynamoDB.putItem(putItemRequest); LOG.debug("DDB Insert Result: " + putItemResult); retval = true; } catch (Exception e) { LOG.error("DDB Insert failed " + e.getMessage()); } return retval; }
public PutPointResult putPoint(PutPointRequest putPointRequest) { long geohash = S2Manager.generateGeohash(putPointRequest.getGeoPoint()); long hashKey = S2Manager.generateHashKey(geohash, config.getHashKeyLength()); String geoJson = GeoJsonMapper.stringFromGeoObject(putPointRequest.getGeoPoint()); PutItemRequest putItemRequest = putPointRequest.getPutItemRequest(); putItemRequest.setTableName(config.getTableName()); AttributeValue hashKeyValue = new AttributeValue().withN(String.valueOf(hashKey)); putItemRequest.getItem().put(config.getHashKeyAttributeName(), hashKeyValue); putItemRequest.getItem().put(config.getRangeKeyAttributeName(), putPointRequest.getRangeKeyValue()); AttributeValue geohashValue = new AttributeValue().withN(Long.toString(geohash)); putItemRequest.getItem().put(config.getGeohashAttributeName(), geohashValue); AttributeValue geoJsonValue = new AttributeValue().withS(geoJson); putItemRequest.getItem().put(config.getGeoJsonAttributeName(), geoJsonValue); PutItemResult putItemResult = config.getDynamoDBClient().putItem(putItemRequest); PutPointResult putPointResult = new PutPointResult(putItemResult); return putPointResult; }
/** * Put ticket. * * @param ticket the ticket * @param encodedTicket the encoded ticket */ public void put(final Ticket ticket, final Ticket encodedTicket) { final TicketDefinition metadata = this.ticketCatalog.find(ticket); final Map<String, AttributeValue> values = buildTableAttributeValuesMapFromTicket(ticket, encodedTicket); LOGGER.debug("Adding ticket id [{}] with attribute values [{}]", encodedTicket.getId(), values); final PutItemRequest putItemRequest = new PutItemRequest(metadata.getProperties().getStorageName(), values); LOGGER.debug("Submitting put request [{}] for ticket id [{}]", putItemRequest, encodedTicket.getId()); final PutItemResult putItemResult = amazonDynamoDBClient.putItem(putItemRequest); LOGGER.debug("Ticket added with result [{}]", putItemResult); getAll(); }
/** * Put. * * @param service the service */ public void put(final RegisteredService service) { final Map<String, AttributeValue> values = buildTableAttributeValuesMapFromService(service); final PutItemRequest putItemRequest = new PutItemRequest(TABLE_NAME, values); LOGGER.debug("Submitting put request [{}] for service id [{}]", putItemRequest, service.getServiceId()); final PutItemResult putItemResult = amazonDynamoDBClient.putItem(putItemRequest); LOGGER.debug("Service added with result [{}]", putItemResult); }
public CompletableFuture<PutItemResult> putItem(final String tableName, final Map<String, AttributeValue> item) { return asyncExecutor.execute(new Callable<PutItemResult>() { @Override public PutItemResult call() throws Exception { return dbExecutor.putItem(tableName, item); } }); }
public CompletableFuture<PutItemResult> putItem(final String tableName, final Map<String, AttributeValue> item, final String returnValues) { return asyncExecutor.execute(new Callable<PutItemResult>() { @Override public PutItemResult call() throws Exception { return dbExecutor.putItem(tableName, item, returnValues); } }); }
public CompletableFuture<PutItemResult> putItem(final PutItemRequest putItemRequest) { return asyncExecutor.execute(new Callable<PutItemResult>() { @Override public PutItemResult call() throws Exception { return dbExecutor.putItem(putItemRequest); } }); }
CompletableFuture<PutItemResult> putItem(final String tableName, final Object entity) { return asyncExecutor.execute(new Callable<PutItemResult>() { @Override public PutItemResult call() throws Exception { return dbExecutor.putItem(tableName, entity); } }); }
CompletableFuture<PutItemResult> putItem(final String tableName, final Object entity, final String returnValues) { return asyncExecutor.execute(new Callable<PutItemResult>() { @Override public PutItemResult call() throws Exception { return dbExecutor.putItem(tableName, entity, returnValues); } }); }
@Override public void execute() { PutItemResult result = ddbClient.putItem(new PutItemRequest() .withTableName(determineTableName()) .withItem(determineItem()) .withExpected(determineUpdateCondition()) .withReturnValues(determineReturnValues())); addAttributesToResult(result.getAttributes()); }
@Test public void test_putItem_WithAllParameters() throws Exception { createTable(); PutItemResult result = putItem(TEST_ATTRIBUTE, TEST_ATTRIBUTE_VALUE); Double units = result.getConsumedCapacity().getCapacityUnits(); assertThat(units.doubleValue(), equalTo(1.0)); }
private PutItemResult putItem(String attributeName, String attributeValue) throws Exception { Map<String, AttributeValue> item = new HashMap<String, AttributeValue>(); item.put(attributeName, new AttributeValue() .withS(attributeValue)); String returnValues = ""; PutItemResult result = dynamoDb.putItem(TEST_TABLE_NAME, item, returnValues); return result; }
/** * The putEntity method stores an item in a table * * @param entity * @return */ @Override public boolean putEntity(String tableName, Entity entity) { Map<String, AttributeValue> newItem = dynamoDBService.newItem(entity); PutItemResult putItemResult = dynamoDBService.putItem(tableName, newItem); if (putItemResult != null) { return true; } return false; }
public void put(String user, long timestamp, double x, double y) { if (tableName != null) { Map<String, AttributeValue> item = new HashMap<String, AttributeValue>(); item.put("user", new AttributeValue().withS(user)); item.put("timestamp", new AttributeValue().withS(Long.toString(timestamp))); item.put("latitude", new AttributeValue().withS(Double.toString(x))); item.put("longitude", new AttributeValue().withS(Double.toString(y))); PutItemRequest putItemRequest = new PutItemRequest() .withTableName(tableName) .withItem(item); PutItemResult result = client.putItem(putItemRequest); } }
@Override protected void handle(final ILoggingEvent event, final String encoded) throws Exception { Item item = Item.fromJSON(encoded).withPrimaryKey(createEventId(event)); Map<String, AttributeValue> attributes = InternalUtils.toAttributeValues(item); PutItemRequest request = new PutItemRequest(table, attributes); String errorMessage = format("Appender '%s' failed to send logging event '%s' to DynamoDB table '%s'", getName(), event, table); CountDownLatch latch = new CountDownLatch(isAsyncParent() ? 0 : 1); dynamoDb.putItemAsync(request, new LoggingEventHandler<PutItemRequest, PutItemResult>(this, latch, errorMessage)); AppenderExecutors.awaitLatch(this, latch, getMaxFlushTime()); }
private static void putItem(Map<String, AttributeValue> item) { try { PutItemRequest putItemRequest = new PutItemRequest(TABLENAME, item); PutItemResult putItemResult = dynamoDBClient.putItem(putItemRequest); logger.info("Result: " + putItemResult); } catch (Exception e) { // TODO: handle exception } }
@Override public Item put(final Map<String, AttributeValue> attributes) throws IOException { final AmazonDynamoDB aws = this.credentials.aws(); try { final PutItemRequest request = new PutItemRequest(); request.setTableName(this.self); request.setItem(attributes); request.setReturnValues(ReturnValue.NONE); request.setReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL); final PutItemResult result = aws.putItem(request); final long start = System.currentTimeMillis(); Logger.info( this, "#put('%[text]s'): created item in '%s', %s, in %[ms]s", attributes, this.self, new PrintableConsumedCapacity( result.getConsumedCapacity() ).print(), System.currentTimeMillis() - start ); return new AwsItem( this.credentials, this.frame(), this.self, new Attributes(attributes).only(this.keys()), new Array<String>(this.keys()) ); } catch (final AmazonClientException ex) { throw new IOException( String.format( "failed to put into \"%s\" with %s", this.self, attributes ), ex ); } finally { aws.shutdown(); } }
public PutItemResult putItem(final String tableName, final Map<String, AttributeValue> item) { return dynamoDB.putItem(tableName, item); }
public PutItemResult putItem(final String tableName, final Map<String, AttributeValue> item, final String returnValues) { return dynamoDB.putItem(tableName, item, returnValues); }
public PutItemResult putItem(final PutItemRequest putItemRequest) { return dynamoDB.putItem(putItemRequest); }
PutItemResult putItem(final String tableName, final Object entity) { return putItem(tableName, toItem(entity)); }
PutItemResult putItem(final String tableName, final Object entity, final String returnValues) { return putItem(tableName, toItem(entity), returnValues); }
@Override public PutItemResult putItem(PutItemRequest putItemRequest) { this.putItemRequest = putItemRequest; return new PutItemResult().withAttributes(getAttributes()); }
private void writeToDynamo() throws RetryFailedException, InterruptedException { Map<String, AttributeValue> attributeValueMap = indexInput.getAttributes().entrySet() .stream() .filter((entry) -> entry.getValue() != null) .filter((entry) -> StringUtils.isNotBlank(entry.getValue().toString())) .collect(Collectors.toMap( e -> e.getKey().toString(), e -> { try { switch (e.getKey().getType()) { case LONG: case DOUBLE: return new AttributeValue().withN(e.getValue().toString()); default: String serializedData = null; // use objectMapper for everything but strings because it // adds quotes to raw strings if (e.getValue().getClass() != String.class) { serializedData = objectMapper.writeValueAsString(e.getValue()); } else { serializedData = (String) e.getValue(); } return new AttributeValue().withS(serializedData); } } catch (Exception ex) { logger.error("Couldn't serialize data to index.", ex); return null; } } )); String tableName = config.getString(TABLE_NAME_CONFIG_KEY); PutItemRequest putItemRequest = new PutItemRequest() .withTableName(tableName) .withItem(attributeValueMap) .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL); final PutItemResult putItemResult = retry(() -> dynamoDBClient.putItem(putItemRequest)); logger.info("Consumed table capacity: " + putItemResult.getConsumedCapacity().getCapacityUnits()); }
/** * AwsTable can save an item. * @throws Exception If some problem inside */ @Test public void savesItemToDynamo() throws Exception { final Credentials credentials = Mockito.mock(Credentials.class); final AmazonDynamoDB aws = Mockito.mock(AmazonDynamoDB.class); Mockito.doReturn(aws).when(credentials).aws(); Mockito.doReturn( new PutItemResult().withConsumedCapacity( new ConsumedCapacity().withCapacityUnits(1.0d) ) ).when(aws).putItem(Mockito.any(PutItemRequest.class)); Mockito.doReturn( new DescribeTableResult().withTable( new TableDescription().withKeySchema( new KeySchemaElement().withAttributeName(AwsTableTest.KEY) ) ) ).when(aws).describeTable(Mockito.any(DescribeTableRequest.class)); final String attr = "attribute-1"; final AttributeValue value = new AttributeValue("value-1"); final String name = "table-name"; final Table table = new AwsTable( credentials, Mockito.mock(Region.class), name ); table.put(new Attributes().with(attr, value)); Mockito.verify(aws).putItem( PutItemRequest.class.cast( MockitoHamcrest.argThat( Matchers.allOf( Matchers.hasProperty( AwsTableTest.TABLE_NAME, Matchers.equalTo(name) ), Matchers.hasProperty( "item", Matchers.hasEntry( Matchers.equalTo(attr), Matchers.equalTo(value) ) ) ) ) ) ); }
public PutPointResult(PutItemResult putItemResult) { this.putItemResult = putItemResult; }
public PutItemResult getPutItemResult() { return putItemResult; }
PutItemResult putItem(String tableName, Map<String, AttributeValue> item);