Class DynamoDb
A DynamoDB client.
DynamoDb db = DynamoDb.forRegion(null); // AWS_REGION
Map item = new LinkedHashMap();
item.put("id", "42");
item.put("name", "Ada");
db.putItem("users", item);
Map back = db.getItem("users", Collections.singletonMap("id", "42"));
The typed methods take and return plain values (see Items). Anything they
do not cover -- a projection, a return-values option, a global secondary
index -- goes through call(String, Map), which takes DynamoDB's own request
document and returns its response document unchanged.
Thread safe: every call is an independent signed HTTPS request.
-
Method Summary
Modifier and TypeMethodDescriptionvoidWrites allitemstotable, in batches of 25, retrying what DynamoDB reports unprocessed.Sends one operation, such as"Query", and returns the response document.voidcreateTable(String table, String partitionKey, boolean numericPartition, String sortKey, boolean numericSort) voidcreateTable(String table, String partitionKey, String sortKey) Creates a table keyed by a string partition key and, whensortKeyis not null, a string sort key, billed on demand.voiddeleteItem(String table, Map key) Deletes the item with this key; nothing happens when there is none.voiddeleteTable(String table) Deletes a table; a missing one is not an error.static DynamoDbforEndpoint(Credentials credentials, String region, String endpoint) A DynamoDB-compatible endpoint, such as DynamoDB Local.static DynamoDbDynamoDB in one AWS region, with credentials resolved the usual way.The item with this key, or null when there is none.voidWrites an item, replacing any with the same key.voidWrites an item only ifconditionholds, for example"attribute_not_exists(id)"; a false condition throws aDynamoDbExceptionwhoseDynamoDbException.isConditionalCheckFailed()is true.query(String table, String index, String keyCondition, String filter, Map names, Map values, int limit) Likequery(String, String, String, Map, Map, int), with a filter expression (or null) applied to the items the key condition reads.Every item a key condition matches, following pagination to the end.Every item of a table, optionally filtered, following pagination.voidsetConsistentReads(boolean consistent) Whether reads of a table are strongly consistent; true by default.Applies an update expression such as"SET #n = :n ADD visits :one"and returns the item as it is afterwards.
-
Method Details
-
forRegion
DynamoDB in one AWS region, with credentials resolved the usual way.
A null region reads AWS_REGION, then AWS_DEFAULT_REGION.
- Throws:
IOException
-
forEndpoint
A DynamoDB-compatible endpoint, such as DynamoDB Local.
endpointis a host with an optional port and scheme, for examplehttp://localhost:8000. TLS is used unless it sayshttp://. -
call
Sends one operation, such as"Query", and returns the response document.- Throws:
IOException
-
getItem
The item with this key, or null when there is none.- Throws:
IOException
-
putItem
Writes an item, replacing any with the same key.- Throws:
IOException
-
putItem
Writes an item only ifconditionholds, for example"attribute_not_exists(id)"; a false condition throws aDynamoDbExceptionwhoseDynamoDbException.isConditionalCheckFailed()is true.valuesbinds the:nameplaceholders of the condition.- Throws:
IOException
-
updateItem
public Map updateItem(String table, Map key, String update, String condition, Map names, Map values) throws IOException Applies an update expression such as
"SET #n = :n ADD visits :one"and returns the item as it is afterwards.namesbinds the#nameplaceholders andvaluesthe:nameones; either may be null.conditionmay be null.- Throws:
IOException
-
deleteItem
Deletes the item with this key; nothing happens when there is none.- Throws:
IOException
-
query
public List query(String table, String index, String keyCondition, Map names, Map values, int limit) throws IOException Every item a key condition matches, following pagination to the end.
indexnames a secondary index, or null for the table itself.limitstops after that many items; zero reads them all.- Throws:
IOException
-
query
public List query(String table, String index, String keyCondition, String filter, Map names, Map values, int limit) throws IOException Likequery(String, String, String, Map, Map, int), with a filter expression (or null) applied to the items the key condition reads. DynamoDB filters after reading, so the filter narrows what is returned, not what is read and paid for.- Throws:
IOException
-
scan
Every item of a table, optionally filtered, following pagination.
A scan reads the whole table and is billed for it; prefer
query(String, String, String, Map, Map, int).- Throws:
IOException
-
setConsistentReads
public void setConsistentReads(boolean consistent) Whether reads of a table are strongly consistent; true by default.
DynamoDB's own default is eventually consistent, under which an item just written can be missing from the next read, and a version read for an optimistic update can be stale. Strong reads cost twice the read capacity. A secondary index read is always eventually consistent.
-
batchPut
Writes allitemstotable, in batches of 25, retrying what DynamoDB reports unprocessed.- Throws:
IOException
-
createTable
Creates a table keyed by a string partition key and, whensortKeyis not null, a string sort key, billed on demand. An existing table is left alone.- Throws:
IOException
-
createTable
public void createTable(String table, String partitionKey, boolean numericPartition, String sortKey, boolean numericSort) throws IOException LikecreateTable(String, String, String), declaring each key as a number (N) or a string (S). DynamoDB refuses a write whose key value is not of the declared type, so a numeric key needsN.- Throws:
IOException
-
deleteTable
Deletes a table; a missing one is not an error.- Throws:
IOException
-