@Override public Profile findOne(String profileName) { AmazonDynamoDB client = null; if (System.getProperty("useProxy") != null) { ClientConfiguration conf = new ClientConfiguration() .withProxyHost(System.getProperty("proxyHost")) .withProxyPort(Integer.valueOf(System.getProperty("proxyPort"))) .withProxyUsername(System.getProperty("proxyUsername")) .withProxyPassword(System.getProperty("proxyPassword")); client = new AmazonDynamoDBClient(new ClasspathPropertiesFileCredentialsProvider(), conf); } else { client = new AmazonDynamoDBClient(new ClasspathPropertiesFileCredentialsProvider()); } client.setRegion(Region.getRegion(Regions.AP_NORTHEAST_1)); client.setEndpoint("https://dynamodb.ap-northeast-1.amazonaws.com"); DynamoDBMapper mapper = new DynamoDBMapper(client); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); Map<String, Condition> scanFilter = new HashMap<String, Condition>(); Condition scanCondition = new Condition() .withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS(profileName)); scanFilter.put("profile_name", scanCondition); scanExpression.setScanFilter(scanFilter); for (Profile p : mapper.scan(Profile.class, scanExpression)) { return p; } return null; }
private BatchGetItemOutcome doBatchGetItem(BatchGetItemSpec spec) { final Collection<TableKeysAndAttributes> tableKeysAndAttributesCol = spec.getTableKeysAndAttributes(); // Unprocessed keys take precedence Map<String, KeysAndAttributes> requestItems = spec.getUnprocessedKeys(); if (requestItems == null || requestItems.size() == 0) { // handle new requests only if there is no unprocessed keys requestItems = new LinkedHashMap<String, KeysAndAttributes>(); } if (tableKeysAndAttributesCol != null) { for (TableKeysAndAttributes tableKeysAndAttributes : tableKeysAndAttributesCol) { // attributes against one table final Set<String> attrNames = tableKeysAndAttributes.getAttributeNames(); // primary keys against one table final List<PrimaryKey> pks = tableKeysAndAttributes.getPrimaryKeys(); final List<Map<String, AttributeValue>> keys = new ArrayList<Map<String, AttributeValue>>(pks.size()); for (PrimaryKey pk : pks) keys.add(InternalUtils.toAttributeValueMap(pk)); final KeysAndAttributes keysAndAttrs = new KeysAndAttributes() .withAttributesToGet(attrNames) .withConsistentRead(tableKeysAndAttributes.isConsistentRead()) .withKeys(keys) .withProjectionExpression(tableKeysAndAttributes.getProjectionExpression()) .withExpressionAttributeNames(tableKeysAndAttributes.getNameMap()); requestItems.put(tableKeysAndAttributes.getTableName(), keysAndAttrs); } } BatchGetItemRequest req = spec.getRequest().withRequestItems(requestItems); BatchGetItemResult result = client.batchGetItem(req); return new BatchGetItemOutcome(result); }
/** * Wait for the table to reach the desired status and returns the table description * * @param dynamo Dynamo client to use * @param tableName Table name to poll status of * @param desiredStatus Desired {@link TableStatus} to wait for. If null this method simply waits * until DescribeTable returns something non-null (i.e. any status) * @param timeout Timeout in milliseconds to continue to poll for desired status * @param interval Time to wait in milliseconds between poll attempts * @return Null if DescribeTables never returns a result, otherwise the result of the last poll * attempt (which may or may not have the desired state) * @throws InterruptedException * @throws {@link IllegalArgumentException} If timeout or interval is invalid */ private static TableDescription waitForTableDescription( final AmazonDynamoDB dynamo, final String tableName, TableStatus desiredStatus, final int timeout, final int interval) throws InterruptedException, IllegalArgumentException { if (timeout < 0) { throw new IllegalArgumentException("Timeout must be >= 0"); } if (interval <= 0 || interval >= timeout) { throw new IllegalArgumentException("Interval must be > 0 and < timeout"); } long startTime = System.currentTimeMillis(); long endTime = startTime + timeout; TableDescription table = null; while (System.currentTimeMillis() < endTime) { try { table = dynamo.describeTable(new DescribeTableRequest(tableName)).getTable(); if (desiredStatus == null || table.getTableStatus().equals(desiredStatus.toString())) { return table; } } catch (ResourceNotFoundException rnfe) { // ResourceNotFound means the table doesn't exist yet, // so ignore this error and just keep polling. } Thread.sleep(interval); } return table; }
@Test public void putItemWithExpected() { createGenericTable(tableName); AttributeValue value = new AttributeValue("test1"); Map<String, ExpectedAttributeValue> expectedMap = new HashMap<String, ExpectedAttributeValue>(); expectedMap.put("id", new ExpectedAttributeValue(false)); Map<String, AttributeValue> item = createGenericItem(value, null); AmazonDynamoDB client = getClient(); client.putItem(new PutItemRequest(tableName, item).withExpected(expectedMap)); try { client.putItem(new PutItemRequest(tableName, item).withExpected(expectedMap)); Assert.assertTrue(false); // Should have thrown a ConditionalCheckFailedException } catch (ConditionalCheckFailedException ccfe) { } }
/** * Deletes the table and ignores any errors if it doesn't exist. * * @param dynamo The Dynamo client to use. * @param deleteTableRequest The delete table request. * @return True if deleted, false otherwise. */ public static final boolean deleteTableIfExists( final AmazonDynamoDB dynamo, final DeleteTableRequest deleteTableRequest) { try { dynamo.deleteTable(deleteTableRequest); return true; } catch (final ResourceNotFoundException e) { if (LOG.isTraceEnabled()) { LOG.trace("Table " + deleteTableRequest.getTableName() + " does not exist", e); } } return false; }
/** * Creates the table and ignores any errors if it already exists. * * @param dynamo The Dynamo client to use. * @param createTableRequest The create table request. * @return True if created, false otherwise. */ public static final boolean createTableIfNotExists( final AmazonDynamoDB dynamo, final CreateTableRequest createTableRequest) { try { dynamo.createTable(createTableRequest); return true; } catch (final ResourceInUseException e) { if (LOG.isTraceEnabled()) { LOG.trace("Table " + createTableRequest.getTableName() + " already exists", e); } } return false; }
@Bean public AmazonDynamoDB amazonDynamoDB() { System.setProperty("java.library.path", "./DynamoDBLocal_lib"); try { Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths"); fieldSysPath.setAccessible(true); fieldSysPath.set(null, null); } catch (Exception e) { e.printStackTrace(); } AmazonDynamoDB amazonDynamoDB = DynamoDBEmbedded.create(); String tableName = "person"; List<AttributeDefinition> attributeDefinitions = new ArrayList<>(); attributeDefinitions.add( new AttributeDefinition().withAttributeName("id").withAttributeType("S")); List<KeySchemaElement> keySchema = new ArrayList<>(); keySchema.add(new KeySchemaElement().withAttributeName("id").withKeyType(KeyType.HASH)); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withKeySchema(keySchema) .withAttributeDefinitions(attributeDefinitions) .withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits(5L).withWriteCapacityUnits(5L)); amazonDynamoDB.createTable(request); log.debug("created dynamodb table[{}]", tableName); return amazonDynamoDB; }
protected ScanResult dbScanWithThroughputBackOff(ScanRequest scanRequest) { Long currentBackOffMs = minBackOffMs.get(); Long retryCount = 0L; while (true) { try { return dbClient.scan(scanRequest); } catch (ProvisionedThroughputExceededException e) { currentBackOffMs = Math.min(currentBackOffMs * 2, maxBackOffMs.get()); log.error( String.format( "Failed to poll Dynamo due to ProvisionedThroughputExceededException. Backing off for %d ms.", currentBackOffMs)); if (retryCount > maxRetryCount.get()) { throw e; } retryCount++; try { Thread.sleep(currentBackOffMs); } catch (InterruptedException ex) { } } } }
private void setEndpoint() { String endpoint = endpointName.get(); dbClient.setEndpoint(endpoint); log.info("Set Dynamo endpoint:" + endpoint); }