简体   繁体   中英

Fetching items from dynamoDB with sorting

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. 动态数据库表结构

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM