Java Code Examples for com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder#defaultClient()

The following examples show how to use com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder#defaultClient() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: APIDemoHandler.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {

    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    JSONObject responseJson = new JSONObject();

    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient();
    DynamoDB dynamoDb = new DynamoDB(client);

    try {
        JSONObject event = (JSONObject) parser.parse(reader);

        if (event.get("body") != null) {

            Person person = new Person((String) event.get("body"));

            dynamoDb.getTable(DYNAMODB_TABLE_NAME)
                .putItem(new PutItemSpec().withItem(new Item().withNumber("id", person.getId())
                    .withString("name", person.getName())));
        }

        JSONObject responseBody = new JSONObject();
        responseBody.put("message", "New item created");

        JSONObject headerJson = new JSONObject();
        headerJson.put("x-custom-header", "my custom header value");

        responseJson.put("statusCode", 200);
        responseJson.put("headers", headerJson);
        responseJson.put("body", responseBody.toString());

    } catch (ParseException pex) {
        responseJson.put("statusCode", 400);
        responseJson.put("exception", pex);
    }

    OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
    writer.write(responseJson.toString());
    writer.close();
}
 
Example 2
Source File: DeleteItem.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
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!");
}
 
Example 3
Source File: DeleteTable.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
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!");
}
 
Example 4
Source File: UpdateTable.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
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!");
}
 
Example 5
Source File: AppConfiguration.java    From smartapp-sdk-java with Apache License 2.0 5 votes vote down vote up
@Bean
public DefaultInstalledAppContextStore installedAppContextStore(
        TokenRefreshService tokenRefreshService) {
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient();
    DynamoDB dynamoDB = new DynamoDB(client);
    return new DynamoDBInstalledAppContextStore(dynamoDB, tokenRefreshService);
}
 
Example 6
Source File: Query.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
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!");
}
 
Example 7
Source File: CreateTableCompositeKey.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
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!");
}
 
Example 8
Source File: DescribeTable.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
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!");
}
 
Example 9
Source File: ListTables.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args)
{
    System.out.println("Your DynamoDB tables:\n");

    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();

    ListTablesRequest request;

    boolean more_tables = true;
    String last_name = null;

    while(more_tables) {
        try {
            if (last_name == null) {
            	request = new ListTablesRequest().withLimit(10);
            }
            else {
            	request = new ListTablesRequest()
            			.withLimit(10)
            			.withExclusiveStartTableName(last_name);
            }

            ListTablesResult table_list = ddb.listTables(request);
            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!");
}
 
Example 10
Source File: GetItem.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
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("DATABASE_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);
    }
}
 
Example 11
Source File: CreateTable.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
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!");
}
 
Example 12
Source File: DynamoDBServiceImpl1.java    From Serverless-Programming-Cookbook with MIT License 4 votes vote down vote up
public DynamoDBServiceImpl1() {
    final AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder.defaultClient();
    this.dynamoDB = new DynamoDB(dynamoDBClient);

}
 
Example 13
Source File: DynamoDBServiceImpl2.java    From Serverless-Programming-Cookbook with MIT License 4 votes vote down vote up
public DynamoDBServiceImpl2() {
    this.dynamoDBClient = AmazonDynamoDBClientBuilder.defaultClient();
}
 
Example 14
Source File: DynamoDBServiceImpl1.java    From Serverless-Programming-Cookbook with MIT License 4 votes vote down vote up
public DynamoDBServiceImpl1() {
    final AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder.defaultClient();
    this.dynamoDB = new DynamoDB(dynamoDBClient);

}
 
Example 15
Source File: DynamoDBServiceImpl2.java    From Serverless-Programming-Cookbook with MIT License 4 votes vote down vote up
public DynamoDBServiceImpl2() {
    this.dynamoDBClient = AmazonDynamoDBClientBuilder.defaultClient();
}
 
Example 16
Source File: DynamoDBServiceImpl1.java    From Serverless-Programming-Cookbook with MIT License 4 votes vote down vote up
public DynamoDBServiceImpl1() {
    final AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder.defaultClient();
    this.dynamoDB = new DynamoDB(dynamoDBClient);

}
 
Example 17
Source File: DynamoDBServiceImpl2.java    From Serverless-Programming-Cookbook with MIT License 4 votes vote down vote up
public DynamoDBServiceImpl2() {
    this.dynamoDBClient = AmazonDynamoDBClientBuilder.defaultClient();
}