Example usage for com.amazonaws.services.dynamodbv2 AmazonDynamoDBClient AmazonDynamoDBClient

List of usage examples for com.amazonaws.services.dynamodbv2 AmazonDynamoDBClient AmazonDynamoDBClient

Introduction

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

Prototype

@Deprecated
public AmazonDynamoDBClient() 

Source Link

Document

Constructs a new client to invoke service methods on DynamoDB.

Usage

From source file:aws.example.dynamodb.CreateTable.java

License:Open Source License

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);//from ww w .ja v a2  s  .c o  m
    }

    /* 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 AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();

    try {
        CreateTableResult result = ddb.createTable(request);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}

From source file:aws.example.dynamodb.CreateTableCompositeKey.java

License:Open Source License

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);/*from   ww w.j av  a2 s  . c o  m*/
    }

    /* 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 AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();

    try {
        CreateTableResult result = ddb.createTable(request);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}

From source file:aws.example.dynamodb.DeleteItem.java

License:Open Source License

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);/*from w  w w.ja  v a  2s.  c o  m*/
    }

    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 AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();

    try {
        ddb.deleteItem(table_name, key_to_get);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }

    System.out.println("Done!");
}

From source file:aws.example.dynamodb.DeleteTable.java

License:Open Source License

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);//  ww w.  j  a  va  2  s. c  o m
    }

    String table_name = args[0];

    System.out.format("Deleting table %s...\n", table_name);

    final AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();

    try {
        ddb.deleteTable(table_name);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}

From source file:aws.example.dynamodb.DescribeTable.java

License:Open Source License

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);//from w w w  .  jav  a  2s  .  c  o  m
    }

    String table_name = args[0];
    System.out.format("Getting description for %s\n\n", table_name);

    final AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();

    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!");
}

From source file:aws.example.dynamodb.GetItem.java

License:Open Source License

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);//from w w w.jav a 2  s .c  o m
    }

    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 AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();

    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);
    }
}

From source file:aws.example.dynamodb.ListTables.java

License:Open Source License

public static void main(String[] args) {
    System.out.println("Your DynamoDB tables:\n");

    final AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();

    boolean more_tables = true;
    while (more_tables) {
        String last_name = null;/*from   w  ww . j a  v  a2 s .  com*/
        try {
            ListTablesResult table_list = null;
            if (last_name == null) {
                table_list = ddb.listTables();
            } else {
                table_list = ddb.listTables(last_name);
            }

            List<String> table_names = table_list.getTableNames();

            if (table_names.size() > 0) {
                for (String cur_name : table_names) {
                    System.out.format("* %s\n", cur_name);
                }
            } else {
                System.out.println("No tables found!");
                System.exit(0);
            }

            last_name = table_list.getLastEvaluatedTableName();
            if (last_name == null) {
                more_tables = false;
            }
        } catch (AmazonServiceException e) {
            System.err.println(e.getErrorMessage());
            System.exit(1);
        }
    }
    System.out.println("\nDone!");
}

From source file:aws.example.dynamodb.PutItem.java

License:Open Source License

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);//from   w  w w . j  av a 2  s .  c o  m
    }

    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 AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();

    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!");
}

From source file:aws.example.dynamodb.Query.java

License:Open Source License

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);/*from w  w  w .j  av a  2 s  .  co  m*/
    }

    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 AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();

    try {
        ddb.updateTable(table_name, table_throughput);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}

From source file:aws.example.dynamodb.UpdateItem.java

License:Open Source License

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);//  w w w  .  java2  s  . c  om
    }

    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 AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();

    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!");
}