See: Description
Class | Description |
---|---|
AddAction |
Represents an ADD action in the ADD section of an update expression.
|
AndCondition |
Represents an AND condition in a condition expression.
|
B |
A path operand that refers to a binary attribute in DynamoDB; used for building expressions.
|
BetweenCondition |
Represents a BETWEEN
condition in a condition expression.
|
BOOL |
A path operand that refers to a boolean attribute in DynamoDB; used for building expressions.
|
BS |
A path operand that refers to a binary set attribute in DynamoDB; used for building expressions.
|
ComparatorCondition |
Represents a Comparator condition in building condition expression.
|
Condition |
Represents a condition for building condition expression.
|
DeleteAction |
Represents a DELETE action in the DELETE section of an update expression.
|
DeleteItemExpressionSpec |
Expression specification for making DeleteItem request to Amazon
DynamoDB.
|
ExpressionSpecBuilder |
A request-centric Expression Specification Builder that can be used to
construct valid expressions, and the respective name maps and value maps, for
various DynamoDB requests in a typeful manner.
|
FunctionCondition |
Represents a Function condition in building condition expression.
|
FunctionOperand |
Represents a function call in building expression.
|
GetItemExpressionSpec |
Expression specification for making GetItem request to Amazon DynamoDB.
|
IfNotExistsFunction<T> |
Represents an if_not_exists(path, operand) function in building expressions.
|
InCondition |
Represents a IN condition in building condition expression.
|
L |
A path operand that refers to a list attribute in DynamoDB; used for building expressions.
|
ListAppendFunction |
Represents the list_append(operand, operand) function in building expression.
|
M |
A path operand that refers to a map attribute in DynamoDB; used for building expressions.
|
MinusOperation |
Represents a minus binary operation in building expressions that involve
number attributes.
|
N |
A path operand that refers to a number attribute in DynamoDB; used for building expressions.
|
NegationCondition |
Represents a negation
condition in building condition expressions.
|
NS |
A path operand that refers to a number set attribute in DynamoDB; used for building expressions.
|
NULL |
A path operand that refers to a NULL attribute in DynamoDB; used for building expressions.
|
Operand |
Represents an operand for building expressions.
|
OrCondition |
Represents an OR condition in building condition expressions.
|
ParenthesizedCondition |
An explicitly parenthesized condition, ie '(' condition ')', used in building
condition expressions.
|
PathOperand |
A path operand used in building DynamooDB expressions such as update
expressions and condition (aka filter) expressions.
|
PlusOperation |
Represents a plus binary operation in building expressions that involve
number attributes.
|
PutItemExpressionSpec |
Expression specification for making PutItem request to Amazon DynamoDB.
|
QueryExpressionSpec |
Expression specification for making query request to Amazon DynamoDB.
|
RemoveAction |
Represents a REMOVE action in the REMOVE section of an update expression.
|
S |
A path operand that refers to a string attribute in DynamoDB; used for building expressions.
|
ScanExpressionSpec |
Expression specification for making scan request to Amazon DynamoDB.
|
SetAction |
Represents a SET action in the SET section of an update expression.
|
SS |
A path operand that refers to a string set attribute in DynamoDB; used for building expressions.
|
UpdateAction |
Represents an update action for building update expression.
|
UpdateItemExpressionSpec |
Expression specification for making UpdateItem request to Amazon DynamoDB.
|
ExpressionSpecBuilder
is the API entry point to this library.
import static com.amazonaws.services.dynamodbv2.xspec.ExpressionSpecBuilder.*; ... Table table = dynamo.getTable(TABLE_NAME); UpdateItemExpressionSpec xspec = new ExpressionSpecBuilder() // SET num1 = num1 + 20 .addUpdate( N("num1").set(N("num1").plus(20))) // SET string-attr = "string-value" .addUpdate( S("string-attr").set("string-value") ) // num2 BETWEEN 0 AND 100 .withCondition( N("num2").between(0, 100) ).buildForUpdate(); table.updateItem(HASH_KEY_NAME, "hashKeyValue", RANGE_KEY_NAME, 0, xspec);
Let's say you want to include a complex condition expression such as:
(attribute_not_exists(item_version) AND attribute_not_exists(config_id) AND attribute_not_exists(config_version)) OR (item_version < 123) OR (item_version = 123 AND config_id < 456) OR (item_version = 123 AND config_id = 456 AND config_version < 999)Here is how:
import static com.amazonaws.services.dynamodbv2.xspec.ExpressionSpecBuilder.*; ... Table table = dynamo.getTable(TABLE_NAME); UpdateItemExpressionSpec xspec = new ExpressionSpecBuilder() // SET num1 = num1 + 20 .addUpdate( N("num1").set(N("num1").plus(20))) // SET string-attr = "string-value" .addUpdate( S("string-attr").set("string-value") ) // a complex condition expression (as shown above) .withCondition( // add explicit parenthesis parenthesize( attribute_not_exists("item_version") .and( attribute_not_exists("config_id") ) .and( attribute_not_exists("config_version") ) ).or( N("item_version").lt(123) ) .or( N("item_version").eq(123) .and( N("config_id").lt(456) ) ) .or( N("item_version").eq(123) .and( N("config_id").eq(456) ) .and( N("config_version").lt(999) )) ).buildForUpdate(); table.updateItem(HASH_KEY_NAME, "hashKeyValue", RANGE_KEY_NAME, 0, xspec);
Without ExpressionSpecBuilder, the code (using the DynamoDB Document API) could be something like:
ItemCollection<?> col = table.scan( "(#hk = :hashkeyAttrValue) AND (#rk BETWEEN :lo AND :hi)", new NameMap().with("#hk", HASH_KEY_NAME).with("#rk", RANGE_KEY_NAME), new ValueMap().withString(":hashkeyAttrValue", "allDataTypes") .withInt(":lo", 1).withInt(":hi", 10));In contrast, using ExpressionSpecBuilder:
import static com.amazonaws.services.dynamodbv2.xspec.ExpressionSpecBuilder.*; ... ScanExpressionSpec xspec = new ExpressionSpecBuilder() .withCondition( S(HASH_KEY_NAME).eq("allDataTypes") .and(N(RANGE_KEY_NAME).between(1, 10)) ).buildForScan(); ItemCollection> col = table.scan(xspec);
import static com.amazonaws.services.dynamodbv2.xspec.ExpressionSpecBuilder.*; ... Table table = dynamo.getTable(TABLE_NAME); UpdateItemExpressionSpec xspec = new ExpressionSpecBuilder() .addUpdate(S("mapAttr.colors[0]").set("red")) .addUpdate(S("mapAttr.colors[1]").set("blue")) .addUpdate(L("mapAttr.members").set( L("mapAttr.members").listAppend("marry", "liza"))) .addUpdate(SS("mapAttr.countries").append("cn", "uk")) .addUpdate(SS("mapAttr.brands").delete("Facebook", "LinkedIn")) .addUpdate(attribute("mapAttr.foo").remove()) .buildForUpdate(); assertEquals("SET #0.#1[0] = :0, #0.#1[1] = :1, #0.#2 = list_append(#0.#2, :2) ADD #0.#3 :3 DELETE #0.#4 :4 REMOVE #0.#5", xspec.getUpdateExpression()); final String hashkey = "addRemoveDeleteColors"; table.updateItem(HASH_KEY_NAME, hashkey, RANGE_KEY_NAME, 0, xspec);
Copyright © 2020. All rights reserved.