Search in sources :

Example 61 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project aws-doc-sdk-examples by awsdocs.

the class Query method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    Query <table> <partitionkey> <partitionkeyvalue>\n\n" + "Where:\n" + "    table - the table to put the item in.\n" + "    partitionkey  - partition key name of the table.\n" + "    partitionkeyvalue - value of the partition key that should match.\n\n" + "Example:\n" + "    Query GreetingsTable Language eng \n";
    if (args.length < 3) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String table_name = args[0];
    String partition_key_name = args[1];
    String partition_key_val = args[2];
    String partition_alias = "#a";
    System.out.format("Querying %s", table_name);
    System.out.println("");
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    // set up an alias for the partition key name in case it's a reserved word
    HashMap<String, String> attrNameAlias = new HashMap<String, String>();
    attrNameAlias.put(partition_alias, partition_key_name);
    // set up mapping of the partition name with the value
    HashMap<String, AttributeValue> attrValues = new HashMap<String, AttributeValue>();
    attrValues.put(":" + partition_key_name, new AttributeValue().withS(partition_key_val));
    QueryRequest queryReq = new QueryRequest().withTableName(table_name).withKeyConditionExpression(partition_alias + " = :" + partition_key_name).withExpressionAttributeNames(attrNameAlias).withExpressionAttributeValues(attrValues);
    try {
        QueryResult response = ddb.query(queryReq);
        System.out.println(response.getCount());
    } catch (AmazonDynamoDBException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) AmazonDynamoDBException(com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) HashMap(java.util.HashMap) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 62 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project aws-doc-sdk-examples by awsdocs.

the class UpdateTable method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    UpdateTable <table> <read> <write>\n\n" + "Where:\n" + "    table - the table to put the item in.\n" + "    read  - the new read capacity of the table.\n" + "    write - the new write capacity of the table.\n\n" + "Example:\n" + "    UpdateTable HelloTable 16 10\n";
    if (args.length < 3) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String table_name = args[0];
    Long read_capacity = Long.parseLong(args[1]);
    Long write_capacity = Long.parseLong(args[2]);
    System.out.format("Updating %s with new provisioned throughput values\n", table_name);
    System.out.format("Read capacity : %d\n", read_capacity);
    System.out.format("Write capacity : %d\n", write_capacity);
    ProvisionedThroughput table_throughput = new ProvisionedThroughput(read_capacity, write_capacity);
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    try {
        ddb.updateTable(table_name, table_throughput);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 63 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project aws-doc-sdk-examples by awsdocs.

the class CreateTableCompositeKey method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    CreateTable <table>\n\n" + "Where:\n" + "    table - the table to create.\n\n" + "Example:\n" + "    CreateTable GreetingsTable\n";
    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    /* Read the name from command args */
    String table_name = args[0];
    System.out.format("Creating table %s\n with a composite primary key:\n");
    System.out.format("* Language - partition key\n");
    System.out.format("* Greeting - sort key\n");
    CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(new AttributeDefinition("Language", ScalarAttributeType.S), new AttributeDefinition("Greeting", ScalarAttributeType.S)).withKeySchema(new KeySchemaElement("Language", KeyType.HASH), new KeySchemaElement("Greeting", KeyType.RANGE)).withProvisionedThroughput(new ProvisionedThroughput(new Long(10), new Long(10))).withTableName(table_name);
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    try {
        CreateTableResult result = ddb.createTable(request);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) CreateTableResult(com.amazonaws.services.dynamodbv2.model.CreateTableResult)

Example 64 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project aws-doc-sdk-examples by awsdocs.

the class StreamsAdapterDemoHelper method deleteItem.

public static void deleteItem(AmazonDynamoDB dynamoDBClient, String tableName, String id) {
    java.util.Map<String, AttributeValue> key = new HashMap<String, AttributeValue>();
    key.put("Id", new AttributeValue().withN(id));
    DeleteItemRequest deleteItemRequest = new DeleteItemRequest().withTableName(tableName).withKey(key);
    dynamoDBClient.deleteItem(deleteItemRequest);
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) HashMap(java.util.HashMap) DeleteItemRequest(com.amazonaws.services.dynamodbv2.model.DeleteItemRequest)

Example 65 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project aws-doc-sdk-examples by awsdocs.

the class StreamsAdapterDemoHelper method updateItem.

public static void updateItem(AmazonDynamoDB dynamoDBClient, String tableName, String id, String val) {
    java.util.Map<String, AttributeValue> key = new HashMap<String, AttributeValue>();
    key.put("Id", new AttributeValue().withN(id));
    Map<String, AttributeValueUpdate> attributeUpdates = new HashMap<String, AttributeValueUpdate>();
    AttributeValueUpdate update = new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue().withS(val));
    attributeUpdates.put("attribute-2", update);
    UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(tableName).withKey(key).withAttributeUpdates(attributeUpdates);
    dynamoDBClient.updateItem(updateItemRequest);
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) UpdateItemRequest(com.amazonaws.services.dynamodbv2.model.UpdateItemRequest) AttributeValueUpdate(com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate) HashMap(java.util.HashMap)

Aggregations

AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)70 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)16 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)14 Test (org.junit.Test)13 Table (com.amazonaws.services.dynamodbv2.document.Table)12 IOException (java.io.IOException)12 AmazonServiceException (com.amazonaws.AmazonServiceException)11 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)11 HashMap (java.util.HashMap)11 AmazonClientException (com.amazonaws.AmazonClientException)10 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)10 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)9 ScanRequest (com.amazonaws.services.dynamodbv2.model.ScanRequest)9 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)8 ToString (lombok.ToString)7 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)6 ArrayList (java.util.ArrayList)6 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)5 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)5 DescribeTableResult (com.amazonaws.services.dynamodbv2.model.DescribeTableResult)5