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