下面列出了怎么用com.amazonaws.services.dynamodbv2.model.ResourceInUseException的API类实例代码及写法,或者点击链接到github查看源代码。
@Before
public void setup() {
try {
repository = new ProductInfoRepository();
repository.setMapper(dynamoDBMapper);
CreateTableRequest tableRequest = dynamoDBMapper.generateCreateTableRequest(ProductInfo.class);
tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
amazonDynamoDB.createTable(tableRequest);
} catch (ResourceInUseException e) {
// Do nothing, table already created
}
// TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfo on table
dynamoDBMapper.batchDelete((List<ProductInfo>) repository.findAll());
}
private static void createTable() {
List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition().withAttributeName("name").withAttributeType("S"));
List<KeySchemaElement> ks = new ArrayList<KeySchemaElement>();
ks.add(new KeySchemaElement().withAttributeName("name").withKeyType(KeyType.HASH)); // Partition
// key
ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(10L)
.withWriteCapacityUnits(10L);
CreateTableRequest request = new CreateTableRequest().withTableName(TABLENAME)
.withAttributeDefinitions(attributeDefinitions).withKeySchema(ks)
.withProvisionedThroughput(provisionedThroughput);
try {
CreateTableResult createdTableDescription = dynamoDBClient.createTable(request);
logger.info("Created Table: " + createdTableDescription);
// Wait for it to become active
waitForTableToBecomeAvailable(TABLENAME);
}
catch (ResourceInUseException e) {
logger.warn("Table already existed", e);
}
}
@Before
public void setup() throws Exception {
try {
dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB);
CreateTableRequest tableRequest = dynamoDBMapper.generateCreateTableRequest(ProductInfo.class);
tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
amazonDynamoDB.createTable(tableRequest);
} catch (ResourceInUseException e) {
// Do nothing, table already created
}
// TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfo on table
dynamoDBMapper.batchDelete((List<ProductInfo>) repository.findAll());
}
public void waitForTableActive(String tableName, long waitTimeSeconds) throws InterruptedException {
if(waitTimeSeconds < 0) {
throw new IllegalArgumentException("Invalid waitTimeSeconds " + waitTimeSeconds);
}
long startTimeMs = System.currentTimeMillis();
long elapsedMs = 0;
do {
DescribeTableResult describe = client.describeTable(new DescribeTableRequest().withTableName(tableName));
String status = describe.getTable().getTableStatus();
if(TableStatus.ACTIVE.toString().equals(status)) {
return;
}
if(TableStatus.DELETING.toString().equals(status)) {
throw new ResourceInUseException("Table " + tableName + " is " + status + ", and waiting for it to become ACTIVE is not useful.");
}
Thread.sleep(10 * 1000);
elapsedMs = System.currentTimeMillis() - startTimeMs;
} while(elapsedMs / 1000.0 < waitTimeSeconds);
throw new ResourceInUseException("Table " + tableName + " did not become ACTIVE after " + waitTimeSeconds + " seconds.");
}
public void waitForTableActive(String tableName,
List<AttributeDefinition> definitions,
List<KeySchemaElement> keySchema,
List<LocalSecondaryIndex> localIndexes,
long waitTimeSeconds) throws InterruptedException {
if(waitTimeSeconds < 0) {
throw new IllegalArgumentException("Invalid waitTimeSeconds " + waitTimeSeconds);
}
long startTimeMs = System.currentTimeMillis();
long elapsedMs = 0;
do {
String status = verifyTableExists(tableName, definitions, keySchema, localIndexes);
if(TableStatus.ACTIVE.toString().equals(status)) {
return;
}
if(TableStatus.DELETING.toString().equals(status)) {
throw new ResourceInUseException("Table " + tableName + " is " + status + ", and waiting for it to become ACTIVE is not useful.");
}
Thread.sleep(10 * 1000);
elapsedMs = System.currentTimeMillis() - startTimeMs;
} while(elapsedMs / 1000.0 < waitTimeSeconds);
throw new ResourceInUseException("Table " + tableName + " did not become ACTIVE after " + waitTimeSeconds + " seconds.");
}
public void waitForTableDeleted(String tableName, long waitTimeSeconds) throws InterruptedException {
if(waitTimeSeconds < 0) {
throw new IllegalArgumentException("Invalid waitTimeSeconds " + waitTimeSeconds);
}
long startTimeMs = System.currentTimeMillis();
long elapsedMs = 0;
do {
try {
DescribeTableResult describe = client.describeTable(new DescribeTableRequest().withTableName(tableName));
String status = describe.getTable().getTableStatus();
if(! TableStatus.DELETING.toString().equals(status)) {
throw new ResourceInUseException("Table " + tableName + " is " + status + ", and waiting for it to not exist is only useful if it is DELETING.");
}
} catch (ResourceNotFoundException e) {
return;
}
Thread.sleep(10 * 1000);
elapsedMs = System.currentTimeMillis() - startTimeMs;
} while(elapsedMs / 1000.0 < waitTimeSeconds);
throw new ResourceInUseException("Table " + tableName + " was not deleted after " + waitTimeSeconds + " seconds.");
}
@PostConstruct
public void init() {
try {
dynamoDB.createTable(tableName, Arrays.asList(new KeySchemaElement("installedAppId", KeyType.HASH)),
Arrays.asList(new AttributeDefinition("installedAppId", "S")), new ProvisionedThroughput(10L, 10L));
} catch (ResourceInUseException e) {
log.info("InstalledAppContext table already exists");
}
}
@Test
public void initIsFineWithTableAlreadyExisting() {
when(dynamoDB.createTable(eq("installedAppContext"),
eq(Arrays.asList(new KeySchemaElement("installedAppId", KeyType.HASH))),
eq(Arrays.asList(new AttributeDefinition("installedAppId", "S"))), any()))
.thenThrow(new ResourceInUseException("table already exists"));
tester.init();
verify(dynamoDB).createTable(eq("installedAppContext"),
eq(Arrays.asList(new KeySchemaElement("installedAppId", KeyType.HASH))),
eq(Arrays.asList(new AttributeDefinition("installedAppId", "S"))), any());
}
@Override
public String create() {
readWriteLock.writeLock().lock();
try {
CreateTableResult result = client.createTable(constructCreateTableRequest());
waitForTableToBecomeActive();
return result.getTableDescription().getTableArn();
} catch (ResourceInUseException e) {
throw new AlreadyExistsException(String.format("There is already a DynamoDB table called '%s'", tableName), e);
} finally {
readWriteLock.writeLock().unlock();
}
}
/**
* @return StreamArn
*/
public static String createTable(AmazonDynamoDB client, String tableName) {
java.util.List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N"));
java.util.List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
keySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH)); // Partition
// key
ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(2L)
.withWriteCapacityUnits(2L);
StreamSpecification streamSpecification = new StreamSpecification();
streamSpecification.setStreamEnabled(true);
streamSpecification.setStreamViewType(StreamViewType.NEW_IMAGE);
CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
.withAttributeDefinitions(attributeDefinitions).withKeySchema(keySchema)
.withProvisionedThroughput(provisionedThroughput).withStreamSpecification(streamSpecification);
try {
System.out.println("Creating table " + tableName);
CreateTableResult result = client.createTable(createTableRequest);
return result.getTableDescription().getLatestStreamArn();
}
catch (ResourceInUseException e) {
System.out.println("Table already exists.");
return describeTable(client, tableName).getTable().getLatestStreamArn();
}
}
private void open(boolean batchLoading)
{
if(type == GraphDatabaseType.TITAN_DYNAMODB && config.getDynamodbPrecreateTables()) {
List<CreateTableRequest> requests = new LinkedList<>();
long wcu = config.getDynamodbTps();
long rcu = Math.max(1, config.dynamodbConsistentRead() ? wcu : (wcu / 2));
for(String store : Constants.REQUIRED_BACKEND_STORES) {
final String tableName = config.getDynamodbTablePrefix() + "_" + store;
if(BackendDataModel.MULTI == config.getDynamodbDataModel()) {
requests.add(DynamoDBStore.createTableRequest(tableName,
rcu, wcu));
} else if(BackendDataModel.SINGLE == config.getDynamodbDataModel()) {
requests.add(DynamoDBSingleRowStore.createTableRequest(tableName, rcu, wcu));
}
}
//TODO is this autocloseable?
final AmazonDynamoDB client =
new AmazonDynamoDBClient(Client.createAWSCredentialsProvider(config.getDynamodbCredentialsFqClassName(),
config.getDynamodbCredentialsCtorArguments() == null ? null : config.getDynamodbCredentialsCtorArguments().split(",")));
client.setEndpoint(config.getDynamodbEndpoint());
for(CreateTableRequest request : requests) {
try {
client.createTable(request);
} catch(ResourceInUseException ignore) {
//already created, good
}
}
client.shutdown();
}
titanGraph = buildTitanGraph(type, dbStorageDirectory, config, batchLoading);
}
/**
* @return StreamArn
*/
public static String createTable(AmazonDynamoDBClient client, String tableName) {
java.util.List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N"));
java.util.List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
keySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH));
ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput()
.withReadCapacityUnits(2L).withWriteCapacityUnits(2L);
StreamSpecification streamSpecification = new StreamSpecification();
streamSpecification.setStreamEnabled(true);
streamSpecification.setStreamViewType(StreamViewType.NEW_IMAGE);
CreateTableRequest createTableRequest = new CreateTableRequest()
.withTableName(tableName)
.withAttributeDefinitions(attributeDefinitions)
.withKeySchema(keySchema)
.withProvisionedThroughput(provisionedThroughput)
.withStreamSpecification(streamSpecification);
try {
System.out.println("Creating table " + tableName);
CreateTableResult result = client.createTable(createTableRequest);
return result.getTableDescription().getLatestStreamArn();
} catch(ResourceInUseException e) {
System.out.println("Table already exists.");
return describeTable(client, tableName).getTable().getLatestStreamArn();
}
}
private static void createTable()
{
List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition().withAttributeName("name").withAttributeType("S"));
List<KeySchemaElement> ks = new ArrayList<KeySchemaElement>();
ks.add(new KeySchemaElement().withAttributeName("name").withKeyType(KeyType.HASH));
ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(10L)
.withWriteCapacityUnits(10L);
CreateTableRequest request = new CreateTableRequest().withTableName(TABLENAME)
.withAttributeDefinitions(attributeDefinitions).withKeySchema(ks)
.withProvisionedThroughput(provisionedThroughput);
try
{
CreateTableResult createdTableDescription = dynamoDBClient.createTable(request);
logger.info("Created Table: " + createdTableDescription);
// Wait for it to become active
waitForTableToBecomeAvailable(TABLENAME);
}
catch (ResourceInUseException e)
{
logger.warn("Table already existed", e);
}
}
/**
* Ensures that the transaction table exists and has the correct schema.
*
* @param client
* @param transactionTableName
* @param transactionImagesTableName
* @throws ResourceInUseException if the table exists but has the wrong schema
* @throws ResourceNotFoundException if the table does not exist
*/
public static void verifyTransactionTablesExist(AmazonDynamoDB client, String transactionTableName, String transactionImagesTableName) {
String state = new TableHelper(client).verifyTableExists(transactionTableName, TRANSACTIONS_TABLE_ATTRIBUTES, TRANSACTIONS_TABLE_KEY_SCHEMA, null/*localIndexes*/);
if(! "ACTIVE".equals(state)) {
throw new ResourceInUseException("Table " + transactionTableName + " is not ACTIVE");
}
state = new TableHelper(client).verifyTableExists(transactionImagesTableName, TRANSACTION_IMAGES_TABLE_ATTRIBUTES, TRANSACTION_IMAGES_TABLE_KEY_SCHEMA, null/*localIndexes*/);
if(! "ACTIVE".equals(state)) {
throw new ResourceInUseException("Table " + transactionImagesTableName + " is not ACTIVE");
}
}
public String verifyTableExists(
String tableName,
List<AttributeDefinition> definitions,
List<KeySchemaElement> keySchema,
List<LocalSecondaryIndex> localIndexes) {
DescribeTableResult describe = client.describeTable(new DescribeTableRequest().withTableName(tableName));
if(! new HashSet<AttributeDefinition>(definitions).equals(new HashSet<AttributeDefinition>(describe.getTable().getAttributeDefinitions()))) {
throw new ResourceInUseException("Table " + tableName + " had the wrong AttributesToGet."
+ " Expected: " + definitions + " "
+ " Was: " + describe.getTable().getAttributeDefinitions());
}
if(! keySchema.equals(describe.getTable().getKeySchema())) {
throw new ResourceInUseException("Table " + tableName + " had the wrong KeySchema."
+ " Expected: " + keySchema + " "
+ " Was: " + describe.getTable().getKeySchema());
}
List<LocalSecondaryIndex> theirLSIs = null;
if(describe.getTable().getLocalSecondaryIndexes() != null) {
theirLSIs = new ArrayList<LocalSecondaryIndex>();
for(LocalSecondaryIndexDescription description : describe.getTable().getLocalSecondaryIndexes()) {
LocalSecondaryIndex lsi = new LocalSecondaryIndex()
.withIndexName(description.getIndexName())
.withKeySchema(description.getKeySchema())
.withProjection(description.getProjection());
theirLSIs.add(lsi);
}
}
if(localIndexes != null) {
if(! new HashSet<LocalSecondaryIndex>(localIndexes).equals(new HashSet<LocalSecondaryIndex>(theirLSIs))) {
throw new ResourceInUseException("Table " + tableName + " did not have the expected LocalSecondaryIndexes."
+ " Expected: " + localIndexes
+ " Was: " + theirLSIs);
}
} else {
if(theirLSIs != null) {
throw new ResourceInUseException("Table " + tableName + " had local secondary indexes, but expected none."
+ " Indexes: " + theirLSIs);
}
}
return describe.getTable().getTableStatus();
}