Search in sources :

Example 11 with Table

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

the class DeleteItem method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    DeleteItem <table> <name>\n\n" + "Where:\n" + "    table - the table to delete the item from.\n" + "    name  - the item to delete from the table,\n" + "            using the primary key \"Name\"\n\n" + "Example:\n" + "    DeleteItem HelloTable World\n\n" + "**Warning** This program will actually delete the item\n" + "            that you specify!\n";
    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String table_name = args[0];
    String name = args[1];
    System.out.format("Deleting 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));
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    try {
        ddb.deleteItem(table_name, key_to_get);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) HashMap(java.util.HashMap) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 12 with Table

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

the class DeleteTable method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    DeleteTable <table>\n\n" + "Where:\n" + "    table - the table to delete.\n\n" + "Example:\n" + "    DeleteTable Greetings\n\n" + "**Warning** This program will actually delete the table\n" + "            that you specify!\n";
    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String table_name = args[0];
    System.out.format("Deleting table %s...\n", table_name);
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    try {
        ddb.deleteTable(table_name);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 13 with Table

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

the class DescribeTable method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    DescribeTable <table>\n\n" + "Where:\n" + "    table - the table to get information about.\n\n" + "Example:\n" + "    DescribeTable HelloTable\n";
    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String table_name = args[0];
    System.out.format("Getting description for %s\n\n", table_name);
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    try {
        TableDescription table_info = ddb.describeTable(table_name).getTable();
        if (table_info != null) {
            System.out.format("Table name  : %s\n", table_info.getTableName());
            System.out.format("Table ARN   : %s\n", table_info.getTableArn());
            System.out.format("Status      : %s\n", table_info.getTableStatus());
            System.out.format("Item count  : %d\n", table_info.getItemCount().longValue());
            System.out.format("Size (bytes): %d\n", table_info.getTableSizeBytes().longValue());
            ProvisionedThroughputDescription throughput_info = table_info.getProvisionedThroughput();
            System.out.println("Throughput");
            System.out.format("  Read Capacity : %d\n", throughput_info.getReadCapacityUnits().longValue());
            System.out.format("  Write Capacity: %d\n", throughput_info.getWriteCapacityUnits().longValue());
            List<AttributeDefinition> attributes = table_info.getAttributeDefinitions();
            System.out.println("Attributes");
            for (AttributeDefinition a : attributes) {
                System.out.format("  %s (%s)\n", a.getAttributeName(), a.getAttributeType());
            }
        }
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("\nDone!");
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) ProvisionedThroughputDescription(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughputDescription) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription)

Example 14 with Table

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

the class PutItem method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    PutItem <table> <name> [field=value ...]\n\n" + "Where:\n" + "    table    - the table to put the item in.\n" + "    name     - a name to add to the table. If the name already\n" + "               exists, its entry will be updated.\n" + "Additional fields can be added by appending them to the end of the\n" + "input.\n\n" + "Example:\n" + "    PutItem Cellists Pau Language=ca Born=1876\n";
    if (args.length < 2) {
        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 to database)?
    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("Adding \"%s\" to \"%s\"", 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_values = new HashMap<String, AttributeValue>();
    item_values.put("Name", new AttributeValue(name));
    for (String[] field : extra_fields) {
        item_values.put(field[0], new AttributeValue(field[1]));
    }
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    try {
        ddb.putItem(table_name, item_values);
    } catch (ResourceNotFoundException e) {
        System.err.format("Error: The table \"%s\" can't be found.\n", table_name);
        System.err.println("Be sure that it exists and that you've typed its name correctly!");
        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) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)

Example 15 with Table

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

the class CreateTable 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 HelloTable\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\" with a simple primary key: \"Name\".\n", table_name);
    CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(new AttributeDefinition("Name", ScalarAttributeType.S)).withKeySchema(new KeySchemaElement("Name", KeyType.HASH)).withProvisionedThroughput(new ProvisionedThroughput(new Long(10), new Long(10))).withTableName(table_name);
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    try {
        CreateTableResult result = ddb.createTable(request);
        System.out.println(result.getTableDescription().getTableName());
    } 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)

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