Search in sources :

Example 31 with Table

use of com.amazonaws.services.dynamodbv2.document.Table 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> <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" + "    Query 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 32 with Table

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

the class UpdateItem method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    UpdateItem <table> <name> <greeting>\n\n" + "Where:\n" + "    table    - the table to put the item in.\n" + "    name     - a name to update in the table. The name must exist,\n" + "               or an error will result.\n" + "Additional fields can be specified by appending them to the end of the\n" + "input.\n\n" + "Examples:\n" + "    UpdateItem SiteColors text default=000000 bold=b22222\n" + "    UpdateItem SiteColors background default=eeeeee code=d3d3d3\n\n";
    if (args.length < 3) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String table_name = args[0];
    String name = args[1];
    ArrayList<String[]> extra_fields = new ArrayList<String[]>();
    // any additional args (fields to add or update)?
    for (int x = 2; x < args.length; x++) {
        String[] fields = args[x].split("=", 2);
        if (fields.length == 2) {
            extra_fields.add(fields);
        } else {
            System.out.format("Invalid argument: %s\n", args[x]);
            System.out.println(USAGE);
            System.exit(1);
        }
    }
    System.out.format("Updating \"%s\" in %s\n", name, table_name);
    if (extra_fields.size() > 0) {
        System.out.println("Additional fields:");
        for (String[] field : extra_fields) {
            System.out.format("  %s: %s\n", field[0], field[1]);
        }
    }
    HashMap<String, AttributeValue> item_key = new HashMap<String, AttributeValue>();
    item_key.put("Name", new AttributeValue(name));
    HashMap<String, AttributeValueUpdate> updated_values = new HashMap<String, AttributeValueUpdate>();
    for (String[] field : extra_fields) {
        updated_values.put(field[0], new AttributeValueUpdate(new AttributeValue(field[1]), AttributeAction.PUT));
    }
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    try {
        ddb.updateItem(table_name, item_key, updated_values);
    } catch (ResourceNotFoundException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (AmazonServiceException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) AttributeValueUpdate(com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) AmazonServiceException(com.amazonaws.AmazonServiceException) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)

Example 33 with Table

use of com.amazonaws.services.dynamodbv2.document.Table 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 34 with Table

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

the class GetItem method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    GetItem <table> <name> [projection_expression]\n\n" + "Where:\n" + "    table - the table to get an item from.\n" + "    name  - the item to get.\n\n" + "You can add an optional projection expression (a quote-delimited,\n" + "comma-separated list of attributes to retrieve) to limit the\n" + "fields returned from the table.\n\n" + "Example:\n" + "    GetItem HelloTable World\n" + "    GetItem SiteColors text \"default, bold\"\n";
    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String table_name = args[0];
    String name = args[1];
    String projection_expression = null;
    // if a projection expression was included, set it.
    if (args.length == 3) {
        projection_expression = args[2];
    }
    System.out.format("Retrieving item \"%s\" from \"%s\"\n", name, table_name);
    HashMap<String, AttributeValue> key_to_get = new HashMap<String, AttributeValue>();
    key_to_get.put("Name", new AttributeValue(name));
    GetItemRequest request = null;
    if (projection_expression != null) {
        request = new GetItemRequest().withKey(key_to_get).withTableName(table_name).withProjectionExpression(projection_expression);
    } else {
        request = new GetItemRequest().withKey(key_to_get).withTableName(table_name);
    }
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    try {
        Map<String, AttributeValue> returned_item = ddb.getItem(request).getItem();
        if (returned_item != null) {
            Set<String> keys = returned_item.keySet();
            for (String key : keys) {
                System.out.format("%s: %s\n", key, returned_item.get(key).toString());
            }
        } else {
            System.out.format("No item found with the key %s!\n", name);
        }
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) HashMap(java.util.HashMap) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) GetItemRequest(com.amazonaws.services.dynamodbv2.model.GetItemRequest)

Example 35 with Table

use of com.amazonaws.services.dynamodbv2.document.Table in project cas by apereo.

the class DynamoDbCloudConfigBootstrapConfiguration method createSettingsTable.

@SneakyThrows
private static void createSettingsTable(final AmazonDynamoDB amazonDynamoDBClient, final boolean deleteTables) {
    final String name = ColumnNames.ID.getColumnName();
    final CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(new AttributeDefinition(name, ScalarAttributeType.S)).withKeySchema(new KeySchemaElement(name, KeyType.HASH)).withProvisionedThroughput(new ProvisionedThroughput(PROVISIONED_THROUGHPUT, PROVISIONED_THROUGHPUT)).withTableName(TABLE_NAME);
    if (deleteTables) {
        final DeleteTableRequest delete = new DeleteTableRequest(request.getTableName());
        LOGGER.debug("Sending delete request [{}] to remove table if necessary", delete);
        TableUtils.deleteTableIfExists(amazonDynamoDBClient, delete);
    }
    LOGGER.debug("Sending delete request [{}] to create table", request);
    TableUtils.createTableIfNotExists(amazonDynamoDBClient, request);
    LOGGER.debug("Waiting until table [{}] becomes active...", request.getTableName());
    TableUtils.waitUntilActive(amazonDynamoDBClient, request.getTableName());
    final DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(request.getTableName());
    LOGGER.debug("Sending request [{}] to obtain table description...", describeTableRequest);
    final TableDescription tableDescription = amazonDynamoDBClient.describeTable(describeTableRequest).getTable();
    LOGGER.debug("Located newly created table with description: [{}]", tableDescription);
}
Also used : DeleteTableRequest(com.amazonaws.services.dynamodbv2.model.DeleteTableRequest) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) SneakyThrows(lombok.SneakyThrows)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)16 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)12 ResourceNotFoundException (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)12 TableDescription (com.amazonaws.services.dynamodbv2.model.TableDescription)11 HashMap (java.util.HashMap)10 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)9 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)9 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)9 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)8 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)8 ScanRequest (com.amazonaws.services.dynamodbv2.model.ScanRequest)8 ScanResult (com.amazonaws.services.dynamodbv2.model.ScanResult)7 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 DeleteTableRequest (com.amazonaws.services.dynamodbv2.model.DeleteTableRequest)5 GoraException (org.apache.gora.util.GoraException)4 AmazonClientException (com.amazonaws.AmazonClientException)3 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)2 Table (com.amazonaws.services.dynamodbv2.document.Table)2