下面列出了怎么用com.amazonaws.services.dynamodbv2.model.DescribeTableRequest的API类实例代码及写法,或者点击链接到github查看源代码。
public TableDescription describeTable(String tableName) {
final DescribeTableRequest describeTablesRequest = new DescribeTableRequest()
.withTableName(tableName);
try {
RetryResult<DescribeTableResult> describeResult = getRetryDriver().runWithRetry(
new Callable<DescribeTableResult>() {
@Override
public DescribeTableResult call() {
DescribeTableResult result = dynamoDB.describeTable(describeTablesRequest);
log.info("Describe table output: " + result);
return result;
}
}, null, null);
return describeResult.result.getTable();
} catch (Exception e) {
throw new RuntimeException("Could not lookup table " + tableName + " in DynamoDB.", e);
}
}
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");
}
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");
}
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) {
e.printStackTrace();
}
}
throw new RuntimeException("Table " + tableName + " never went active");
}
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");
}
public void setupTable() {
setupGeoDataManager();
GeoDataManagerConfiguration config = geoDataManager.getGeoDataManagerConfiguration();
DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(config.getTableName());
try {
config.getDynamoDBClient().describeTable(describeTableRequest);
if (status == Status.NOT_STARTED) {
status = Status.READY;
}
} catch (ResourceNotFoundException e) {
PhotoLocationsTable photoLocationsTable = new PhotoLocationsTable();
photoLocationsTable.start();
}
}
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");
}
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");
}
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) {
e.printStackTrace();
}
}
throw new RuntimeException("Table " + tableName + " never went active");
}
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");
}
public void setupTable() {
setupGeoDataManager();
GeoDataManagerConfiguration config = geoDataManager.getGeoDataManagerConfiguration();
DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(config.getTableName());
try {
config.getDynamoDBClient().describeTable(describeTableRequest);
if (status == Status.NOT_STARTED) {
status = Status.READY;
}
} catch (ResourceNotFoundException e) {
SchoolDataLoader schoolDataLoader = new SchoolDataLoader();
schoolDataLoader.start();
}
}
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 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.");
}
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());
}
private static void waitForTableToBecomeAvailable(String tableName) {
logger.info("Waiting for " + tableName + " to become ACTIVE...");
long startTime = System.currentTimeMillis();
long endTime = startTime + (10 * 60 * 1000);
while (System.currentTimeMillis() < endTime) {
try {
Thread.sleep(1000 * 20);
}
catch (Exception e) {
}
try {
DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
TableDescription tableDescription = dynamoDBClient.describeTable(request).getTable();
String tableStatus = tableDescription.getTableStatus();
logger.info(" - current state: " + tableStatus);
if (tableStatus.equals(TableStatus.ACTIVE.toString()))
return;
}
catch (AmazonServiceException ase) {
if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == false)
throw ase;
}
}
throw new RuntimeException("Table " + tableName + " never went active");
}
private DescribeTableResult describeTable(final DescribeTableRequest request) throws BackendException {
controlPlaneRateLimiter.acquire();
final Timer.Context apiTimerContext = getTimerContext(DESCRIBE_TABLE, request.getTableName());
DescribeTableResult result;
try {
result = client.describeTable(request);
} catch (final Exception e) {
throw processDynamoDbApiException(e, DESCRIBE_TABLE, request.getTableName());
} finally {
apiTimerContext.stop();
}
return result;
}
private boolean leaseTableExists() {
DescribeTableRequest request = new DescribeTableRequest();
request.setTableName(conf.applicationName);
DescribeTableResult result;
try {
result = dynamoDBClient.describeTable(request);
} catch (ResourceNotFoundException e) {
LOG.debug("Lease table '{}' does not exist", conf.applicationName);
return false;
}
TableStatus tableStatus = TableStatus.fromValue(result.getTable().getTableStatus());
LOG.debug("Lease table exists and is in '{}' state", tableStatus);
return tableStatus == TableStatus.ACTIVE;
}
protected boolean doesTableExist(String tableName) {
try {
DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
DescribeTableResult result = ddb.describeTable(request);
return (result != null && "ACTIVE".equals(result.getTable().getTableStatus()));
} catch (ResourceNotFoundException e) {
return false;
}
}
protected boolean doesTableExist(String tableName) {
try {
DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
DescribeTableResult result = ddb.describeTable(request);
return (result != null && "ACTIVE".equals(result.getTable().getTableStatus()));
} catch (ResourceNotFoundException e) {
return false;
}
}
private void waitForTableToBeReady() {
GeoDataManagerConfiguration config = geoDataManager.getGeoDataManagerConfiguration();
DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(config.getTableName());
DescribeTableResult describeTableResult = config.getDynamoDBClient().describeTable(describeTableRequest);
while (!describeTableResult.getTable().getTableStatus().equalsIgnoreCase("ACTIVE")) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
describeTableResult = config.getDynamoDBClient().describeTable(describeTableRequest);
}
}
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());
}
private static void waitForTableToBecomeAvailable(String tableName)
{
logger.info("Waiting for " + tableName + " to become ACTIVE...");
long startTime = System.currentTimeMillis();
long endTime = startTime + (10 * 60 * 1000);
while (System.currentTimeMillis() < endTime)
{
try
{
Thread.sleep(1000 * 20);
}
catch (Exception e)
{
}
try
{
DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
TableDescription tableDescription = dynamoDBClient.describeTable(request).getTable();
String tableStatus = tableDescription.getTableStatus();
logger.info(" - current state: " + tableStatus);
if (tableStatus.equals(TableStatus.ACTIVE.toString()))
return;
}
catch (AmazonServiceException ase)
{
if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == false)
throw ase;
}
}
throw new RuntimeException("Table " + tableName + " never went active");
}
private void waitForTableToBeReady() {
GeoDataManagerConfiguration config = geoDataManager.getGeoDataManagerConfiguration();
DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(config.getTableName());
DescribeTableResult describeTableResult = config.getDynamoDBClient().describeTable(describeTableRequest);
while (!describeTableResult.getTable().getTableStatus().equalsIgnoreCase("ACTIVE")) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
describeTableResult = config.getDynamoDBClient().describeTable(describeTableRequest);
}
}
protected List<KeySchemaElement> getTableSchema(String tableName) throws ResourceNotFoundException {
List<KeySchemaElement> schema = tableSchemaCache.get(tableName);
if(schema == null) {
DescribeTableResult result = client.describeTable(new DescribeTableRequest().withTableName(tableName));
schema = Collections.unmodifiableList(result.getTable().getKeySchema());
tableSchemaCache.put(tableName, schema);
}
return schema;
}
/**
* Verifies if the table has the expected schema.
*
* @param client
* The {@link AmazonDynamoDBClient} with Amazon DynamoDB read privileges
* @param tableName
* The Amazon DynamoDB table to check
* @param key
* The expected hashkey for the Amazon DynamoDB table
* @return true if the Amazon DynamoDB table exists and the expected hashkey matches the table schema,
* otherwise return false
*/
private static boolean tableHasCorrectSchema(AmazonDynamoDBClient client, String tableName, String key) {
DescribeTableRequest describeTableRequest = new DescribeTableRequest();
describeTableRequest.setTableName(tableName);
DescribeTableResult describeTableResult = client.describeTable(describeTableRequest);
TableDescription tableDescription = describeTableResult.getTable();
if (tableDescription.getAttributeDefinitions().size() != 1) {
LOG.error("The number of attribute definitions does not match the existing table.");
return false;
}
AttributeDefinition attributeDefinition = tableDescription.getAttributeDefinitions().get(0);
if (!attributeDefinition.getAttributeName().equals(key)
|| !attributeDefinition.getAttributeType().equals(ScalarAttributeType.S.toString())) {
LOG.error("Attribute name or type does not match existing table.");
return false;
}
List<KeySchemaElement> KSEs = tableDescription.getKeySchema();
if (KSEs.size() != 1) {
LOG.error("The number of key schema elements does not match the existing table.");
return false;
}
KeySchemaElement kse = KSEs.get(0);
if (!kse.getAttributeName().equals(key) || !kse.getKeyType().equals(KeyType.HASH.toString())) {
LOG.error("The hash key does not match the existing table.");
return false;
}
return true;
}
/**
* Helper method to determine if an Amazon DynamoDB table exists.
*
* @param client
* The {@link AmazonDynamoDBClient} with Amazon DynamoDB read privileges
* @param tableName
* The Amazon DynamoDB table to check for
* @return true if the Amazon DynamoDB table exists, otherwise return false
*/
private static boolean tableExists(AmazonDynamoDBClient client, String tableName) {
DescribeTableRequest describeTableRequest = new DescribeTableRequest();
describeTableRequest.setTableName(tableName);
try {
client.describeTable(describeTableRequest);
return true;
} catch (ResourceNotFoundException e) {
return false;
}
}
@Override
public DescribeTableResult describeTable(DescribeTableRequest describeTableRequest) {
throw new UnsupportedOperationException();
}
public static DescribeTableResult describeTable(AmazonDynamoDB client, String tableName) {
return client.describeTable(new DescribeTableRequest().withTableName(tableName));
}
private static void describeTable() {
DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(TABLENAME);
TableDescription tableDescription = dynamoDBClient.describeTable(describeTableRequest).getTable();
logger.info("Table Description: " + tableDescription);
}
private TableDescription describeTable(final String tableName) throws BackendException {
return describeTable(new DescribeTableRequest().withTableName(tableName)).getTable();
}