Example usage for com.amazonaws.services.dynamodbv2.model DescribeTableRequest DescribeTableRequest

List of usage examples for com.amazonaws.services.dynamodbv2.model DescribeTableRequest DescribeTableRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.model DescribeTableRequest DescribeTableRequest.

Prototype

public DescribeTableRequest(String tableName) 

Source Link

Document

Constructs a new DescribeTableRequest object.

Usage

From source file:com.cfelde.aws.ddb.management.TableThroughput.java

License:Open Source License

public void initCapacityValues() {
    synchronized (lock) {
        try {/*  w  ww  .  j  a  va 2  s.  c o m*/
            ProvisionedThroughputDescription ptd = ddb.describeTable(new DescribeTableRequest(tableName))
                    .getTable().getProvisionedThroughput();

            requestedReadCapacity.compareAndSet(0, ptd.getReadCapacityUnits());
            requestedWriteCapacity.compareAndSet(0, ptd.getWriteCapacityUnits());

            LOG.info("Initial capacity on " + tableName + ": reads: " + ptd.getReadCapacityUnits()
                    + ", writes: " + ptd.getWriteCapacityUnits());
        } catch (Throwable t) {
            LOG.error("Exception in initCapacityValues: " + t.getMessage(), t);
            exceptionCounter.incrementAndGet();
        }
    }
}

From source file:com.cfelde.aws.ddb.management.TableThroughput.java

License:Open Source License

public void updateDownscaleCounter() {
    synchronized (lock) {
        try {//from  w  w  w .  j ava2  s. c  om
            ProvisionedThroughputDescription ptd = ddb.describeTable(new DescribeTableRequest(tableName))
                    .getTable().getProvisionedThroughput();

            downscaleCounter.set(ptd.getNumberOfDecreasesToday());

            LOG.info("Current scale down counter value on " + tableName + ": "
                    + ptd.getNumberOfDecreasesToday());
        } catch (Throwable t) {
            LOG.error("Exception in updateDownscaleCounter: " + t.getMessage(), t);
            exceptionCounter.incrementAndGet();
        }
    }
}

From source file:com.cfelde.aws.ddb.management.TableThroughput.java

License:Open Source License

public void updateCapacity() {
    synchronized (lock) {
        try {/*  www  . ja  v  a  2s .  c  om*/
            if (lastTableChange.plusMinutes(3).isAfter(new DateTime()))
                return;

            long readCapacity = requestedReadCapacity.get();
            long writeCapacity = requestedWriteCapacity.get();

            if (readCapacity > maxReadLimit)
                readCapacity = maxReadLimit;
            if (readCapacity < minReadLimit)
                readCapacity = minReadLimit;

            if (writeCapacity > maxWriteLimit)
                writeCapacity = maxWriteLimit;
            if (writeCapacity < minWriteLimit)
                writeCapacity = minWriteLimit;

            ProvisionedThroughputDescription ptd = ddb.describeTable(new DescribeTableRequest(tableName))
                    .getTable().getProvisionedThroughput();

            downscaleCounter.set(ptd.getNumberOfDecreasesToday());

            final long currentReadCapacity = ptd.getReadCapacityUnits();
            final long currentWriteCapacity = ptd.getWriteCapacityUnits();

            // Make sure we don't try to scale up more than 100%
            if (readCapacity > currentReadCapacity * 2)
                readCapacity = currentReadCapacity * 2;

            if (writeCapacity > currentWriteCapacity * 2)
                writeCapacity = currentWriteCapacity * 2;

            if (!isDownscaleAllowed() && readCapacity < currentReadCapacity)
                readCapacity = currentReadCapacity;
            if (!isDownscaleAllowed() && writeCapacity < currentWriteCapacity)
                writeCapacity = currentWriteCapacity;

            // Check if no change
            if (readCapacity == currentReadCapacity && writeCapacity == currentWriteCapacity)
                return;

            /*
            if (readCapacity < currentReadCapacity || writeCapacity < currentWriteCapacity)
            downscaleAllowed.set(false);
            */

            ProvisionedThroughput throughput = new ProvisionedThroughput();
            throughput.withReadCapacityUnits(readCapacity);
            throughput.withWriteCapacityUnits(writeCapacity);

            UpdateTableRequest request = new UpdateTableRequest(tableName, throughput);

            LOG.info("Changing throughput on " + tableName + " to: reads: " + throughput.getReadCapacityUnits()
                    + ", writes: " + throughput.getWriteCapacityUnits());
            ddb.updateTable(request);

            lastTableChange = new DateTime();
        } catch (Throwable t) {
            LOG.error("Exception in updateCapacity: " + t.getMessage(), t);
            exceptionCounter.incrementAndGet();
        }
    }
}

From source file:com.clicktravel.infrastructure.persistence.aws.dynamodb.manager.DynamoDbTemplateInfrastructureManager.java

License:Apache License

private boolean isTableCreated(final String tableName) {
    try {//from   w  w  w  . jav  a  2 s .  c  o  m
        final DescribeTableResult result = amazonDynamoDbClient
                .describeTable(new DescribeTableRequest(tableName));
        final TableDescription tableDescription = result.getTable();
        final String tableStatus = tableDescription.getTableStatus();
        final String returnedTableName = tableDescription.getTableName();
        return tableName.equals(returnedTableName) && TableStatus.ACTIVE.toString().equals(tableStatus);
    } catch (final ResourceNotFoundException e) {
        return false;
    }
}

From source file:com.github.sdmcraft.slingdynamo.impl.DynamoDBResourceProvider.java

License:Open Source License

/**
 * Returns a resource from this resource provider or null if the resource provider cannot find it.
 * The table-name, id and child-ids are parsed out from the path and queried against dynamodb to fetch the specified resource.
 *
 * @param resolver the ResourceResolver to which the returned Resource is attached.
 * @param req the HttpServletRequest made to get this resource
 * @param path the path of the resource. The path is of the format &lt;table-name&gt;/&lt;id&gt;/[&lt;child-id1&gt;/.../&lt;child-idn&gt;]
 * @return the resource at the specified path if it exists else returns null
 *///from   www  .  j  av a2s.c om
public Resource getResource(ResourceResolver resolver, HttpServletRequest req, String path) {
    Resource resource = null;

    try {
        Map<String, Object> resourceProps = new HashMap<String, Object>();
        ResourceMetadata resourceMetadata = new ResourceMetadata();
        resourceMetadata.setResolutionPath(path);

        if (!path.contains(".")) {
            if (path.length() > root.length()) {
                String subPath = path.substring(root.length() + 1);
                String[] subPathSplits = subPath.split("/");
                String table = subPathSplits[0];
                resourceMetadata.put("table", table);

                Table dbtable = dynamoDB.getTable(table);

                if (subPathSplits.length == 1) {
                    DescribeTableRequest describeTableRequest = new DescribeTableRequest(table);
                    DescribeTableResult describeTableResult = null;

                    describeTableResult = dynamoDBClient.describeTable(describeTableRequest);

                    Date creationDate = describeTableResult.getTable().getCreationDateTime();
                    long itemCount = describeTableResult.getTable().getItemCount();

                    resourceProps.put("creation-date", creationDate);
                    resourceProps.put("record-count", itemCount);
                    resourceProps.put("table-name", table);
                } else if (subPathSplits.length == 2) {
                    int id = Integer.parseInt(subPathSplits[1]);
                    ScanFilter idFilter = new ScanFilter("id").eq(id);
                    ItemCollection<ScanOutcome> items = dbtable.scan(idFilter);
                    Iterator<Item> itemItr = items.iterator();

                    Item item = itemItr.next();
                    resourceProps = itemToMap(item);
                } else if (subPathSplits.length > 2) {
                    int parent = Integer.parseInt(subPathSplits[1]);
                    Item item = null;

                    for (int i = 2; i < subPathSplits.length; i++) {
                        int child = Integer.parseInt(subPathSplits[i]);
                        ScanFilter parentFilter = new ScanFilter("parent").eq(parent);
                        ScanFilter childFilter = new ScanFilter("child_id").eq(child);
                        ItemCollection<ScanOutcome> items = dbtable.scan(parentFilter, childFilter);
                        Iterator<Item> itemItr = items.iterator();
                        item = itemItr.next();
                        parent = item.getInt("id");
                    }

                    resourceProps = itemToMap(item);
                }
            }

            resourceProps.put("hello", "world");

            ModifiableValueMapDecorator valueMap = new ModifiableValueMapDecorator(resourceProps);

            resource = new DynamoDBResource(resolver, resourceMetadata, valueMap, resourceType);
        }
    } catch (Throwable ex) {
        LOGGER.error(ex.getMessage(), ex);
        throw new RuntimeException(ex);
    }

    return resource;
}

From source file:io.klerch.alexa.state.handler.AWSDynamoStateHandler.java

License:Open Source License

private boolean tableExists() {
    // if custom table is set assume it is present
    // this is how you can bypass table existence checks and table creation as you maybe do not want
    // to authorize your AWS client with broader permission sets
    if (tableExistenceApproved)
        return true;
    try {//from   www . j a v  a 2  s.co m
        // due to absence of existence check in AWS SDK we check the table descriptor for its state
        final TableDescription table = awsClient.describeTable(new DescribeTableRequest(tableName)).getTable();
        // save result to a local variable to not let this handler check for table existence again
        tableExistenceApproved = TableStatus.ACTIVE.toString().equals(table.getTableStatus());
        return tableExistenceApproved;
    } catch (ResourceNotFoundException e) {
        final String error = String.format("Could not find table '%1$s'", tableName);
        log.warn(error);
        return false;
    }
}

From source file:io.venable.amazonaws.dynamo.table.TableHelper.java

License:Apache License

private static boolean doesTableExist(AmazonDynamoDB dynamo, String tableName) {
    try {//from  w  w w. java 2s.c o  m
        TableDescription table = dynamo.describeTable(new DescribeTableRequest(tableName)).getTable();
        return TableStatus.ACTIVE.toString().equals(table.getTableStatus());
    } catch (ResourceNotFoundException ex) {
        return false;
    }
}

From source file:io.venable.amazonaws.dynamo.test.TableTestUtils.java

License:Apache License

public static boolean doesTableExist(AmazonDynamoDB dynamo, String tableName) {
    try {/* w ww .ja v  a  2 s .c o m*/
        TableDescription table = dynamo.describeTable(new DescribeTableRequest(tableName)).getTable();
        return TableStatus.ACTIVE.toString().equals(table.getTableStatus());
    } catch (ResourceNotFoundException ex) {
        return false;
    }
}

From source file:org.springframework.integration.aws.metadata.DynamoDbMetaDataStore.java

License:Apache License

@Override
public void afterPropertiesSet() throws Exception {
    try {//w w w .  jav a 2s . c o m
        this.table.describe();
        createTableLatch.countDown();
        return;
    } catch (ResourceNotFoundException e) {
        if (logger.isInfoEnabled()) {
            logger.info("No table '" + this.table.getTableName() + "'. Creating one...");
        }
    }

    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(this.table.getTableName())
            .withKeySchema(new KeySchemaElement(KEY, KeyType.HASH))
            .withAttributeDefinitions(new AttributeDefinition(KEY, ScalarAttributeType.S))
            .withProvisionedThroughput(new ProvisionedThroughput(this.readCapacity, this.writeCapacity));

    this.dynamoDB.createTableAsync(createTableRequest,
            new AsyncHandler<CreateTableRequest, CreateTableResult>() {

                @Override
                public void onError(Exception e) {
                    logger.error(
                            "Cannot create DynamoDb table: " + DynamoDbMetaDataStore.this.table.getTableName(),
                            e);
                    DynamoDbMetaDataStore.this.createTableLatch.countDown();
                }

                @Override
                public void onSuccess(CreateTableRequest request, CreateTableResult createTableResult) {
                    Waiter<DescribeTableRequest> waiter = DynamoDbMetaDataStore.this.dynamoDB.waiters()
                            .tableExists();

                    WaiterParameters<DescribeTableRequest> waiterParameters = new WaiterParameters<>(
                            new DescribeTableRequest(DynamoDbMetaDataStore.this.table.getTableName()))
                                    .withPollingStrategy(new PollingStrategy(new MaxAttemptsRetryStrategy(25),
                                            new FixedDelayStrategy(1)));

                    waiter.runAsync(waiterParameters, new WaiterHandler<DescribeTableRequest>() {

                        @Override
                        public void onWaitSuccess(DescribeTableRequest request) {
                            DynamoDbMetaDataStore.this.createTableLatch.countDown();
                            DynamoDbMetaDataStore.this.table.describe();
                        }

                        @Override
                        public void onWaitFailure(Exception e) {
                            logger.error("Cannot describe DynamoDb table: "
                                    + DynamoDbMetaDataStore.this.table.getTableName(), e);
                            DynamoDbMetaDataStore.this.createTableLatch.countDown();
                        }

                    });
                }

            });
}