DynamoDB “OR” conditional Range query -
let's assume table looks like:
code |startdate |enddate |additional attributes... abc |11-24-2015 |11-26-2015 | .... abc |12-12-2015 |12-15-2015 | .... abc |10-05-2015 |10-10-2015 | .... pqr |03-24-2015 |03-27-2015 | .... pqr |05-04-2015 |05-08-2015 | ....
provided code (c) , date range (x, y), need able query items like:
query => (code = c) , ((startdate between x , y) or (enddate between x , y))
i planning use primary key hash , range key (code, startdate) additional lsi (enddate) , query on it. not sure if there way achieve this. don't want use scan
operation seems scan entire table costly.
also, achieve in single query.
one option using query
, filterexpression
. no need define lsi
on case. have query hash key
eq
operator , narrow results filter expression. here example java sdk:
table table = dynamodb.gettable(tablename); map<string, object> expressionattributevalues = new hashmap<string, object>(); expressionattributevalues.put(":x", "11-24-2015"); expressionattributevalues.put(":y", "11-26-2015"); queryspec spec = new queryspec() .withhashkey("code", "codevaluehere") .withfilterexpression("(startdate between :x , :y) or (enddate between :x , :y)") .withvaluemap(expressionattributevalues); itemcollection<queryoutcome> items = table.query(spec); iterator<item> iterator = items.iterator(); while (iterator.hasnext()) { system.out.println(iterator.next().tojsonpretty()); }
see specifying conditions condition expressions more details.
additionally, although previous query uses hash key
, can still group records range key
containing dates in following format:
startdate#enddate
table structure:
code daterange |startdate |enddate abc 11-24-2015#11-26-2015 |11-24-2015 |11-26-2015 abc 12-12-2015#12-15-2015 |12-12-2015 |12-15-2015 abc 10-05-2015#10-10-2015 |10-05-2015 |10-10-2015 pqr 03-24-2015#03-27-2015 |03-24-2015 |03-27-2015 pqr 05-04-2015#05-08-2015 |05-04-2015 |05-08-2015
this way if happen query hash key
still records sorted dates. also, believe idea follow advice given unambiguous date format.nu
Comments
Post a Comment