2

In my project i want to fetch all records from table with sort by created_date descending order. Also i want to add condition of fetch all item not created_by login user. I have tried many ways but not able to achieve it.

Below is my table structure. dynamodb table structure

Here is my java code to fetch records from dynamoDB.

Map<String, Condition> filter = new HashMap<String, Condition>();
//filter.put(RealmConstant.Expo.created_by, new Condition().withComparisonOperator(ComparisonOperator.NE).withAttributeValueList(new AttributeValue().withS(userId)));
filter.put(RealmConstant.Expo.created_date, new Condition().withComparisonOperator(ComparisonOperator.LE.toString()).withAttributeValueList(new AttributeValue().withS(""+new Date())));

Expo expo =new Expo();
expo.setCreated_by(userId);

DynamoDBQueryExpression<Expo> queryExpression = new DynamoDBQueryExpression<Expo>();
queryExpression.setHashKeyValues(expo);
queryExpression.setIndexName(AppConstant.DynamoDBTableIndex.created_by_created_date_index);
queryExpression.setConsistentRead(false);
queryExpression.setRangeKeyConditions(filter);

queryExpression.setScanIndexForward(false);
return mapper.query(Expo.class, queryExpression);

As per above code i am getting all records created by me only. I want to fetch all records not created by me.

Also tried .withFilterExpression("created_by <> :val1").withExpressionAttributeValues(eav); but not working. As already question posted. Why is there no **not equal** comparison in DynamoDB queries?

and

DynamoDB: Filter Expression can only contain non-primary key attributes

Devganiya Hitesh
  • 1,207
  • 2
  • 17
  • 31

1 Answers1

1

The short answer is that you can’t fetch *all the items from a DynamoDB table in sorted order, by any attribute. DynamoDB just doesn’t work that way.

Think of DynamoDB as a distributed hash map of lists.

Just the same as you can’t expect to be able to get globally sorted results from such a map of lists, you can’t get them from DynamoDB either.

You can scan the whole table, and even filter, out some unwanted results as you go, but for sorting, you need to do it after you’ve fetched the records.

What you can do is retrieve items that have the same partition key, in order or the sort key.

And you can create an index where you pick an arbitrary attribute as the partition key and another as the sort key but even that approach has some limitations.

The best way to go is to really take some time and think about what you are going to do with the data. Why are trying to retrieve all items from the table in sorted order? Perhaps there is a better way to organize your data such the you din’t need to retrieve all of it.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • I want to fetch data in sort order because i want data in batching with normal load more functionality. What is batter way to achieve it? – Devganiya Hitesh Apr 06 '18 at 04:29
  • I can not get all records from table and sort at application side. Is it feasible solution ? – Devganiya Hitesh Apr 06 '18 at 04:30
  • You can but that is only feasible if there aren’t that many records to begin with. At scale pulling all data locally and sorting becomes a bad idea. – Mike Dinescu Apr 06 '18 at 04:37