Example usage for com.amazonaws.services.dynamodbv2.document DynamoDB getTable

List of usage examples for com.amazonaws.services.dynamodbv2.document DynamoDB getTable

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.document DynamoDB getTable.

Prototype

public Table getTable(String tableName) 

Source Link

Document

Returns the specified DynamoDB table.

Usage

From source file:com.eho.dynamodb.DynamoDBConnection.java

public static String upload_resource(BaseResource resource,
        String primary_key /* if no primary key in case of post, send null*/ ) throws Exception {
    String id = add_primary_as_extension(resource, primary_key);
    String resource_string = DynamoDBConnection.fCtx.newJsonParser().setPrettyPrint(true)
            .encodeResourceToString(resource);
    ;// w  ww  .  ja  v a 2  s. c om
    DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
    Table table = dynamoDB.getTable(PATIENT_TABLE);

    //lets retreive based on the key. if key invalid (not assigned yet) nullis returned.
    Item retreived_item = table.getItem(PRIMARY_KEY, id);
    if (retreived_item == null)//if null instantiate it
    {
        retreived_item = new Item();
        retreived_item.withPrimaryKey(PRIMARY_KEY, id);
        retreived_item.withInt("version", -1);
    }

    Integer new_version = retreived_item.getInt("version") + 1;
    retreived_item.withInt("version", new_version);

    Item item_to_upload = retreived_item//Item.fromJSON(retreived_item.toJSONPretty())
            .withString("text" + new_version.toString(), resource_string)
            .withMap("json-document", new ObjectMapper().readValue(resource_string, LinkedHashMap.class));
    PutItemSpec putItemSpec = new PutItemSpec().withItem(item_to_upload);
    table.putItem(putItemSpec);
    return id;
}

From source file:com.eho.dynamodb.DynamoDBConnection.java

public static ItemCollection<QueryOutcome> query_dynamodb(QuerySpec spec) {
    DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
    Table table = dynamoDB.getTable(PATIENT_TABLE);
    ItemCollection<QueryOutcome> items = table.query(spec);
    return items;
}

From source file:com.eho.dynamodb.DynamoDBConnection.java

public static UpdateItemOutcome update_resource(String resource) throws Exception {
    String id;//from  w  w  w  . j a v a2  s .com
    JSONObject json_resource = new JSONObject(resource);
    //does the resource have a primary key?
    if (json_resource.has(PRIMARY_KEY))//if it does not have a primary key, create one using uuid
        id = json_resource.getString(PRIMARY_KEY);
    else
        id = UUID.randomUUID().toString();

    DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
    Table table = dynamoDB.getTable(PATIENT_TABLE);

    //lets retreive based on the key. if key invalid (not assigned yet) nullis returned.
    Item retreived_item = table.getItem(PRIMARY_KEY, id);
    if (retreived_item == null)//if null instantiate it
    {
        retreived_item = new Item();
        retreived_item.withPrimaryKey(PRIMARY_KEY, id);
    }

    Integer new_version = retreived_item.getInt("version") + 1;
    retreived_item.withInt("version", new_version);
    String new_version_str = new_version.toString();

    UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey(PRIMARY_KEY, id)
            .withUpdateExpression("SET " + new_version_str + "= :newval")
            .withValueMap(new ValueMap().withString(":newval", resource)).withReturnValues(ReturnValue.ALL_NEW);

    return table.updateItem(updateItemSpec);
}

From source file:com.envirover.spl.stream.MAVLinkMessagesTable.java

License:Open Source License

public MAVLinkMessagesTable() {
    if (System.getenv(SPL_DYNAMODB_TABLE) != null) {
        tableName = System.getenv(SPL_DYNAMODB_TABLE);
    }//w w w. ja va 2  s.  co  m

    AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder.defaultClient();

    DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);

    table = dynamoDB.getTable(tableName);
}

From source file:com.intuit.tank.persistence.databases.AmazonDynamoDatabaseDocApi.java

License:Open Source License

private void addItemsToTable(String tableName, final BatchWriteItemRequest request) {

    boolean shouldRetry;
    int retries = 0;

    do {/*from   w ww. j a v a 2  s  . c om*/
        shouldRetry = false;
        try {
            BatchWriteItemResult result = dynamoDb.batchWriteItem(request);
            if (result != null) {
                try {
                    List<ConsumedCapacity> consumedCapacity = result.getConsumedCapacity();
                    for (ConsumedCapacity cap : consumedCapacity) {
                        logger.info(cap.getCapacityUnits());
                    }
                } catch (Exception e) {
                    // ignore this
                }
            }
        } catch (AmazonServiceException e) {
            if (e instanceof ProvisionedThroughputExceededException) {
                try {
                    DynamoDB db = new DynamoDB(dynamoDb);
                    Table table = db.getTable(tableName);
                    ProvisionedThroughputDescription oldThroughput = table.getDescription()
                            .getProvisionedThroughput();
                    logger.info("ProvisionedThroughputExceeded throughput = " + oldThroughput);
                    ProvisionedThroughput newThroughput = new ProvisionedThroughput()
                            .withReadCapacityUnits(
                                    table.getDescription().getProvisionedThroughput().getReadCapacityUnits())
                            .withWriteCapacityUnits(getIncreasedThroughput(
                                    table.getDescription().getProvisionedThroughput().getReadCapacityUnits()));
                    if (!oldThroughput.equals(newThroughput)) {
                        logger.info("Updating throughput to " + newThroughput);
                        table.updateTable(newThroughput);
                        table.waitForActive();
                    }
                } catch (Exception e1) {
                    logger.error("Error increasing capacity: " + e, e);
                }
            }
            int status = e.getStatusCode();
            if (status == HttpStatus.SC_INTERNAL_SERVER_ERROR || status == HttpStatus.SC_SERVICE_UNAVAILABLE) {
                shouldRetry = true;
                long delay = (long) (Math.random() * (Math.pow(4, retries++) * 100L));
                try {
                    Thread.sleep(delay);
                } catch (InterruptedException iex) {
                    logger.error("Caught InterruptedException exception", iex);
                }
            } else {
                logger.error("Error writing to DB: " + e.getMessage());
                throw new RuntimeException(e);
            }
        }
    } while (shouldRetry && retries < MAX_NUMBER_OF_RETRIES);

}

From source file:com.yahoo.athenz.zts.cert.impl.DynamoDBCertRecordStoreConnection.java

License:Apache License

public DynamoDBCertRecordStoreConnection(DynamoDB dynamoDB, final String tableName) {
    table = dynamoDB.getTable(tableName);
}

From source file:org.diksha.common.dyutils.DyDBUtils.java

License:Apache License

public static ArrayList<String> listOpenExecutionsFromDynamo(String config) {
    ArrayList<String> retValue = new ArrayList<String>();

    DynamoDB dynamoDB = getDynamoDB();
    Table table = dynamoDB.getTable("SchedulerWorkflowState");
    Index index = table.getIndex("loopStateIndex");

    ItemCollection<QueryOutcome> items = null;
    QuerySpec querySpec = new QuerySpec();

    querySpec.withKeyConditionExpression("loopState = :v_state")
            .withValueMap(new ValueMap().withString(":v_state", "PROCESSING"));

    items = index.query(querySpec);/*w ww. j  a  va 2 s.  c o  m*/

    Iterator<Item> iterator = items.iterator();
    int totalActualCount = 0;
    Item item;

    while (iterator.hasNext()) {

        item = iterator.next();

        retValue.add(item.getString("clientId"));

    }

    return retValue;

}

From source file:org.diksha.common.dyutils.DyDBUtils.java

License:Apache License

public static int countActiveJobs() {
    DynamoDB dynamoDB = getDynamoDB();

    Table table = dynamoDB.getTable("SchedulerWorkflowState");
    Index index = table.getIndex("loopStateIndex");

    ItemCollection<QueryOutcome> items = null;
    QuerySpec querySpec = new QuerySpec();

    int count = 0;

    querySpec.withKeyConditionExpression("loopState = :v_state")
            .withValueMap(new ValueMap().withString(":v_state", "PROCESSING"));
    items = index.query(querySpec);/*from  ww w.  j  a v  a 2s. c  o m*/
    Iterator<Item> iterator = items.iterator();

    while (iterator.hasNext()) {

        iterator.next();
        count++;
    }

    return count;

}

From source file:org.diksha.common.dyutils.DyDBUtils.java

License:Apache License

public static void listActiveJobs(String optionalExecutionId) {
    DynamoDB dynamoDB = getDynamoDB();

    Table table = dynamoDB.getTable("SchedulerWorkflowState");
    Index index = table.getIndex("loopStateIndex");

    ItemCollection<QueryOutcome> items = null;
    QuerySpec querySpec = new QuerySpec();

    if ((optionalExecutionId != null) && !optionalExecutionId.isEmpty()) {
        querySpec.withKeyConditionExpression("loopState = :v_state and begins_with(clientId, :v_eid)")
                .withValueMap(new ValueMap().withString(":v_state", "PROCESSING").withString(":v_eid",
                        optionalExecutionId));

    } else {/*from   w ww .ja  va2s  .  c  o  m*/

        querySpec.withKeyConditionExpression("loopState = :v_state")
                .withValueMap(new ValueMap().withString(":v_state", "PROCESSING"));
    }

    items = index.query(querySpec);
    Iterator<Item> iterator = items.iterator();

    System.out.format("%20s %7s %28s %40s\n", "CronExpression", "Loop Count", "Next Scheduled Time       ",
            " ExecutionId");
    while (iterator.hasNext()) {

        Item item = iterator.next();

        System.out.format("%20s %7s %28s %40s\n", item.get("cronExpression"), item.get("loopCount"),
                item.get("lastProposedTimeDate"), item.get("clientId"));

    }

}

From source file:org.diksha.common.dyutils.SchedulerDynamoTable.java

License:Apache License

public void deleteDynamoTable(DynamoDB dynamoDB) {
    Table table = dynamoDB.getTable(tableName);
    try {//from  w  w w.j  a v a2 s . co m
        System.out.println("Issuing DeleteTable request for " + tableName);
        table.delete();
        System.out.println("Waiting for " + tableName + " to be deleted...this may take a while...");
        table.waitForDelete();

    } catch (Exception e) {
        System.err.println("DeleteTable request failed for " + tableName);
        System.err.println(e.getMessage());
    }
}