Class DynamoDb

java.lang.Object
com.codename1.backend.dynamo.DynamoDb

public final class DynamoDb extends Object

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 Details

    • forRegion

      public static DynamoDb forRegion(String region) throws IOException

      DynamoDB in one AWS region, with credentials resolved the usual way.

      A null region reads AWS_REGION, then AWS_DEFAULT_REGION.

      Throws:
      IOException
    • forEndpoint

      public static DynamoDb forEndpoint(Credentials credentials, String region, String endpoint)

      A DynamoDB-compatible endpoint, such as DynamoDB Local.

      endpoint is a host with an optional port and scheme, for example http://localhost:8000. TLS is used unless it says http://.

    • call

      public Map call(String operation, Map request) throws IOException
      Sends one operation, such as "Query", and returns the response document.
      Throws:
      IOException
    • getItem

      public Map getItem(String table, Map key) throws IOException
      The item with this key, or null when there is none.
      Throws:
      IOException
    • putItem

      public void putItem(String table, Map item) throws IOException
      Writes an item, replacing any with the same key.
      Throws:
      IOException
    • putItem

      public void putItem(String table, Map item, String condition, Map values) throws IOException
      Writes an item only if condition holds, for example "attribute_not_exists(id)"; a false condition throws a DynamoDbException whose DynamoDbException.isConditionalCheckFailed() is true. values binds the :name placeholders 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.

      names binds the #name placeholders and values the :name ones; either may be null. condition may be null.

      Throws:
      IOException
    • deleteItem

      public void deleteItem(String table, Map key) throws IOException
      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.

      index names a secondary index, or null for the table itself. limit stops 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
      Like query(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

      public List scan(String table, String filter, Map names, Map values, int limit) throws IOException

      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

      public void batchPut(String table, List items) throws IOException
      Writes all items to table, in batches of 25, retrying what DynamoDB reports unprocessed.
      Throws:
      IOException
    • createTable

      public void createTable(String table, String partitionKey, String sortKey) throws IOException
      Creates a table keyed by a string partition key and, when sortKey is 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
      Like createTable(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 needs N.
      Throws:
      IOException
    • deleteTable

      public void deleteTable(String table) throws IOException
      Deletes a table; a missing one is not an error.
      Throws:
      IOException