use of com.amazonaws.services.dynamodbv2.document.Item 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!");
}
use of com.amazonaws.services.dynamodbv2.document.Item 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!");
}
use of com.amazonaws.services.dynamodbv2.document.Item 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.document.Item 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);
}
}
use of com.amazonaws.services.dynamodbv2.document.Item in project nifi by apache.
the class PutDynamoDB method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
List<FlowFile> flowFiles = session.get(context.getProperty(BATCH_SIZE).evaluateAttributeExpressions().asInteger());
if (flowFiles == null || flowFiles.size() == 0) {
return;
}
Map<ItemKeys, FlowFile> keysToFlowFileMap = new HashMap<>();
final String table = context.getProperty(TABLE).evaluateAttributeExpressions().getValue();
final String hashKeyName = context.getProperty(HASH_KEY_NAME).evaluateAttributeExpressions().getValue();
final String hashKeyValueType = context.getProperty(HASH_KEY_VALUE_TYPE).getValue();
final String rangeKeyName = context.getProperty(RANGE_KEY_NAME).evaluateAttributeExpressions().getValue();
final String rangeKeyValueType = context.getProperty(RANGE_KEY_VALUE_TYPE).getValue();
final String jsonDocument = context.getProperty(JSON_DOCUMENT).evaluateAttributeExpressions().getValue();
final String charset = context.getProperty(DOCUMENT_CHARSET).evaluateAttributeExpressions().getValue();
TableWriteItems tableWriteItems = new TableWriteItems(table);
for (FlowFile flowFile : flowFiles) {
final Object hashKeyValue = getValue(context, HASH_KEY_VALUE_TYPE, HASH_KEY_VALUE, flowFile);
final Object rangeKeyValue = getValue(context, RANGE_KEY_VALUE_TYPE, RANGE_KEY_VALUE, flowFile);
if (!isHashKeyValueConsistent(hashKeyName, hashKeyValue, session, flowFile)) {
continue;
}
if (!isRangeKeyValueConsistent(rangeKeyName, rangeKeyValue, session, flowFile)) {
continue;
}
if (!isDataValid(flowFile, jsonDocument)) {
flowFile = session.putAttribute(flowFile, AWS_DYNAMO_DB_ITEM_SIZE_ERROR, "Max size of item + attribute should be 400kb but was " + flowFile.getSize() + jsonDocument.length());
session.transfer(flowFile, REL_FAILURE);
continue;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
session.exportTo(flowFile, baos);
try {
if (rangeKeyValue == null || StringUtils.isBlank(rangeKeyValue.toString())) {
tableWriteItems.addItemToPut(new Item().withKeyComponent(hashKeyName, hashKeyValue).withJSON(jsonDocument, IOUtils.toString(baos.toByteArray(), charset)));
} else {
tableWriteItems.addItemToPut(new Item().withKeyComponent(hashKeyName, hashKeyValue).withKeyComponent(rangeKeyName, rangeKeyValue).withJSON(jsonDocument, IOUtils.toString(baos.toByteArray(), charset)));
}
} catch (IOException ioe) {
getLogger().error("IOException while creating put item : " + ioe.getMessage());
flowFile = session.putAttribute(flowFile, DYNAMODB_ITEM_IO_ERROR, ioe.getMessage());
session.transfer(flowFile, REL_FAILURE);
}
keysToFlowFileMap.put(new ItemKeys(hashKeyValue, rangeKeyValue), flowFile);
}
if (keysToFlowFileMap.isEmpty()) {
return;
}
final DynamoDB dynamoDB = getDynamoDB();
try {
BatchWriteItemOutcome outcome = dynamoDB.batchWriteItem(tableWriteItems);
handleUnprocessedItems(session, keysToFlowFileMap, table, hashKeyName, hashKeyValueType, rangeKeyName, rangeKeyValueType, outcome);
// Handle any remaining flowfiles
for (FlowFile flowFile : keysToFlowFileMap.values()) {
getLogger().debug("Successful posted items to dynamodb : " + table);
session.transfer(flowFile, REL_SUCCESS);
}
} catch (AmazonServiceException exception) {
getLogger().error("Could not process flowFiles due to service exception : " + exception.getMessage());
List<FlowFile> failedFlowFiles = processServiceException(session, flowFiles, exception);
session.transfer(failedFlowFiles, REL_FAILURE);
} catch (AmazonClientException exception) {
getLogger().error("Could not process flowFiles due to client exception : " + exception.getMessage());
List<FlowFile> failedFlowFiles = processClientException(session, flowFiles, exception);
session.transfer(failedFlowFiles, REL_FAILURE);
} catch (Exception exception) {
getLogger().error("Could not process flowFiles due to exception : " + exception.getMessage());
List<FlowFile> failedFlowFiles = processException(session, flowFiles, exception);
session.transfer(failedFlowFiles, REL_FAILURE);
}
}
Aggregations