while (System.currentTimeMillis() < endTime) { try { table = dynamo.describeTable(new DescribeTableRequest(tableName)).getTable(); if (desiredStatus == null || table.getTableStatus().equals(desiredStatus.toString())) { return table;
public DescribeTableResult unmarshall(JsonUnmarshallerContext context) throws Exception { DescribeTableResult describeTableResult = new DescribeTableResult(); int originalDepth = context.getCurrentDepth(); String currentParentElement = context.getCurrentParentElement(); int targetDepth = originalDepth + 1; JsonToken token = context.getCurrentToken(); if (token == null) token = context.nextToken(); if (token == VALUE_NULL) { return describeTableResult; } while (true) { if (token == null) break; if (token == FIELD_NAME || token == START_OBJECT) { if (context.testExpression("Table", targetDepth)) { context.nextToken(); describeTableResult.setTable(TableDescriptionJsonUnmarshaller.getInstance().unmarshall(context)); } } else if (token == END_ARRAY || token == END_OBJECT) { if (context.getLastParsedParentElement() == null || context.getLastParsedParentElement().equals(currentParentElement)) { if (context.getCurrentDepth() <= originalDepth) break; } } token = context.nextToken(); } return describeTableResult; }
private static void waitForTableToBecomeAvailable(String tableName) { System.out.println("Waiting for " + tableName + " to become ACTIVE..."); long startTime = System.currentTimeMillis(); long endTime = startTime + (10 * 60 * 1000); while (System.currentTimeMillis() < endTime) { DescribeTableRequest request = new DescribeTableRequest() .withTableName(tableName); TableDescription tableDescription = client.describeTable( request).getTable(); String tableStatus = tableDescription.getTableStatus(); System.out.println(" - current state: " + tableStatus); if (tableStatus.equals(TableStatus.ACTIVE.toString())) return; try { Thread.sleep(1000 * 20); } catch (Exception e) { } } throw new RuntimeException("Table " + tableName + " never went active"); }
/** * Gives basic information about a DynamoDB table (status, creation date, size). * @param appid name of the {@link com.erudika.para.core.App} * @return a map */ public static Map<String, Object> getTableStatus(final String appid) { if (StringUtils.isBlank(appid)) { return Collections.emptyMap(); } try { final TableDescription td = getClient().describeTable(getTableNameForAppid(appid)).getTable(); HashMap<String, Object> dbStatus = new HashMap<>(); dbStatus.put("id", appid); dbStatus.put("status", td.getTableStatus()); dbStatus.put("created", td.getCreationDateTime().getTime()); dbStatus.put("sizeBytes", td.getTableSizeBytes()); dbStatus.put("itemCount", td.getItemCount()); dbStatus.put("readCapacityUnits", td.getProvisionedThroughput().getReadCapacityUnits()); dbStatus.put("writeCapacityUnits", td.getProvisionedThroughput().getWriteCapacityUnits()); return dbStatus; } catch (Exception e) { logger.error(null, e); } return Collections.emptyMap(); }
@PostConstruct public void setup() { try { DescribeTableResult table = dynamoDBClient.describeTable(tableConfig.getTableName()); if (!table.getTable().getKeySchema().equals(PROJECT_KEYSCHEMA)) { throw new IllegalStateException("Dynamodb table for query metadata store has invalid key schema"); } if (!ImmutableSet.copyOf(table.getTable().getAttributeDefinitions()).equals(ATTRIBUTES)) { throw new IllegalStateException("Dynamodb table for query metadata store has invalid attribute schema"); } } catch (ResourceNotFoundException e) { createTable(); } }
private TableStatus tableStatus(String tableName) { DescribeTableRequest request = new DescribeTableRequest(); request.setTableName(tableName); try { DescribeTableResult result = amazonDynamoDB.describeTable(request); TableStatus tableStatus = TableStatus.fromValue(result.getTable().getTableStatus()); logger.debug("table status {} ", tableStatus); return tableStatus; } catch (ResourceNotFoundException e) { logger.debug("ResourceNotFound is TableName {}", tableName); return null; } catch (AmazonClientException e) { logger.error("Unknown error ", e); throw new IllegalStateException("Unknown Exception", e); } }
/** * Interface which will block until a dynamo table reaches a specified * state. Also returns immediately if the object doesn't exist * * @param dynamoClient * Dynamo DB Client to use for connection to Dynamo DB. * @param dynamoTable * The table name to check. * @param status * The status to wait for * @throws Exception */ private static void waitForTableState(final AmazonDynamoDB dynamoClient, final String dynamoTable, TableStatus status) throws Exception { DescribeTableResult tableRequest = null; while (true) { try { tableRequest = dynamoClient.describeTable(dynamoTable); if (tableRequest.getTable().getTableStatus() .equals(status.name())) break; Thread.sleep(1000); } catch (InterruptedException e) { return; } } }
private void runTest(AmazonDynamoDB client) { CreateTableRequest request = new CreateTableRequest() .withAttributeDefinitions(new AttributeDefinition( "Name", ScalarAttributeType.S)) .withKeySchema(new KeySchemaElement("Name", KeyType.HASH)) .withProvisionedThroughput(new ProvisionedThroughput( new Long(10), new Long(10))) .withTableName("foo"); client.createTable(request); final TableDescription tableDescription = client.describeTable("foo").getTable(); assertNotNull("the description is not null", tableDescription); assertEquals("the table has the right name", "foo", tableDescription.getTableName()); assertEquals("the name has the right primary key", "Name", tableDescription.getKeySchema().get(0).getAttributeName()); } }
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()); if(describe.getTable().getLocalSecondaryIndexes() != null) { theirLSIs = new ArrayList<LocalSecondaryIndex>(); for(LocalSecondaryIndexDescription description : describe.getTable().getLocalSecondaryIndexes()) { LocalSecondaryIndex lsi = new LocalSecondaryIndex() .withIndexName(description.getIndexName()) return describe.getTable().getTableStatus();
/** * @param entityInformation * The entity to check for it's table * @throws IllegalStateException * is thrown if the existing table doesn't match the entity's * annotation */ private DescribeTableResult performValidate(DynamoDBEntityInformation<T, ID> entityInformation) throws IllegalStateException { Class<T> domainType = entityInformation.getJavaType(); CreateTableRequest expected = mapper.generateCreateTableRequest(domainType); DescribeTableResult result = amazonDynamoDB.describeTable(expected.getTableName()); TableDescription actual = result.getTable(); if (!expected.getKeySchema().equals(actual.getKeySchema())) { throw new IllegalStateException("KeySchema is not as expected. Expected: <" + expected.getKeySchema() + "> but found <" + actual.getKeySchema() + ">"); } LOGGER.debug("KeySchema is valid"); if (expected.getGlobalSecondaryIndexes() != null) { if (!Arrays.deepEquals(expected.getGlobalSecondaryIndexes().toArray(), actual.getGlobalSecondaryIndexes().toArray())) { throw new IllegalStateException("Global Secondary Indexes are not as expected. Expected: <" + expected.getGlobalSecondaryIndexes() + "> but found <" + actual.getGlobalSecondaryIndexes() + ">"); } } LOGGER.debug("Global Secondary Indexes are valid"); LOGGER.info("Validated table {} for entity{}", expected.getTableName(), domainType); return result; }
final AmazonDynamoDB dynamoClient = new AmazonDynamoDBClient( credentials); if (region != null) dynamoClient.setRegion(region); final AmazonDynamoDBAsyncClient deleteCli = new AmazonDynamoDBAsyncClient( credentials); .describeTable(dynamoTable).getTable().getKeySchema(); for (KeySchemaElement element : keySchema) { if (element.getKeyType().equals(KeyType.HASH.name())) .withExclusiveStartKey(lastKey); ScanResult results = dynamoClient.scan(scan);
.appendDynamoDBDocumentUserAgentString(new DescribeTableRequest(this.tableName)); .describeTable(request); this.tableDescription = result.getTable(); for (final KeySchemaElement element : this.tableDescription.getKeySchema()) { final String keyName = element.getAttributeName(); for (final AttributeDefinition ad : this.tableDescription.getAttributeDefinitions()) { if (ad.getAttributeName().equals(keyName)) { final KeyDescription kd = new KeyDescription(); if (this.tableDescription.getLocalSecondaryIndexes() != null) { for (final LocalSecondaryIndexDescription index : this.tableDescription .getLocalSecondaryIndexes()) {
/** * Generate a list of attribute names found in the Aggregator's dynamo * table. Assumes that all Items in the Aggregator table are of the same * structure. * * @param dynamoClient Dynamo DB Client to use for connection to Dynamo DB. * @param dynamoTable The Dynamo Table for the Aggregator * @return A list of attribute names from the Dynamo table * @throws Exception */ protected List<String> getDictionaryEntry() throws Exception { // get a list of all columns in the table, with keys first List<String> columns = new ArrayList<>(); List<KeySchemaElement> keys = dynamoClient.describeTable(this.tableName).getTable().getKeySchema(); for (KeySchemaElement key : keys) { columns.add(key.getAttributeName()); } ScanResult scan = dynamoClient.scan(new ScanRequest().withTableName(this.tableName).withSelect( Select.ALL_ATTRIBUTES).withLimit(1)); List<Map<String, AttributeValue>> scannedItems = scan.getItems(); for (Map<String, AttributeValue> map : scannedItems) { for (String s : map.keySet()) { if (!columns.contains(s)) columns.add(s); } } return columns; }
static void getTableInformation() { TableDescription tableDescription = client.describeTable( new DescribeTableRequest().withTableName(tableName)).getTable(); System.out.format("Name: %s:\n" + "Status: %s \n" + "Provisioned Throughput (read capacity units/sec): %d \n" + "Provisioned Throughput (write capacity units/sec): %d \n", tableDescription.getTableName(), tableDescription.getTableStatus(), tableDescription.getProvisionedThroughput().getReadCapacityUnits(), tableDescription.getProvisionedThroughput().getWriteCapacityUnits()); }
public static void main(String args[]) { dynamoDBClient.setEndpoint("DYNAMODB_ENDPOINT_GOES_HERE"); dynamoDBClient.createTable(createTableRequest); DescribeTableResult describeTableResult = dynamoDBClient.describeTable(tableName); String myStreamArn = describeTableResult.getTable().getLatestStreamArn(); StreamSpecification myStreamSpec = describeTableResult.getTable().getStreamSpecification();
final ListTablesResult tables = _dynamoDb.listTables(); final List<String> tableNames = tables.getTableNames(); for (final String tableName : tableNames) { schema.addTable(table); final DescribeTableResult descripeTableResult = _dynamoDb.describeTable(tableName); final TableDescription tableDescription = descripeTableResult.getTable(); addColumnFromKeySchema("Primary index", tableDescription.getKeySchema(), table, true); .getGlobalSecondaryIndexes(); if (globalSecondaryIndexes != null) { for (final GlobalSecondaryIndexDescription globalSecondaryIndex : globalSecondaryIndexes) { .getLocalSecondaryIndexes(); if (localSecondaryIndexes != null) { for (final LocalSecondaryIndexDescription localSecondaryIndex : localSecondaryIndexes) {
/** * Retrieves the table description from DynamoDB. Involves network calls. * Meant to be called as infrequently as possible to avoid throttling * exception from the server side. * * @return a non-null table description * * @throws ResourceNotFoundException if the table doesn't exist */ public TableDescription describe() { DescribeTableResult result = client.describeTable( InternalUtils.applyUserAgent(new DescribeTableRequest(tableName))); return tableDescription = result.getTable(); }
/** * Gives basic information about a DynamoDB table (status, creation date, size). * @param appid name of the {@link com.erudika.para.core.App} * @return a map */ public static Map<String, Object> getTableStatus(String appid) { if (StringUtils.isBlank(appid)) { return Collections.emptyMap(); } try { Map<String, Object> map = new HashMap<String, Object>(); final TableDescription td = getClient().describeTable(getTablNameForAppid(appid)).getTable(); return new HashMap<String, Object>() { { put("status", td.getTableStatus()); put("created", td.getCreationDateTime().getTime()); put("sizeBytes", td.getTableSizeBytes()); put("itemCount", td.getItemCount()); put("readCapacityUnits", td.getProvisionedThroughput().getReadCapacityUnits()); put("writeCapacityUnits", td.getProvisionedThroughput().getWriteCapacityUnits()); } }; } catch (Exception e) { logger.error(null, e); } return Collections.emptyMap(); }
private TableDescription describeTable(final String tableName) throws BackendException { return describeTable(new DescribeTableRequest().withTableName(tableName)).getTable(); }
public static void assertNoStaleTables(AmazonDynamoDBClient client, String when) { /* Get the list of tables without the ones in the deleting state */ List<String> tables = client.listTables().getTableNames().stream() .filter(t -> System.currentTimeMillis() - AWSUtils.toEpochMillis(t) > AWSUtils.HOUR || !"DELETING".equals(client.describeTable(t).getTable().getTableStatus())) .collect(Collectors.toList()); Assert.assertEquals(String.format("Found stale DynamoDB tables %s running the test: %s", when, tables), 0, tables.size()); }