简体   繁体   中英

How to apply filters in dynamodb getItem

I want to fetch specific item from dynamoDB by using table.getItem(xxx) and while fetching the item need to apply some filters.

select * from emp where empid=1 and isadmin=true;

How can I write above query in dynamo-db.

Below is the sample code which I have tried:

GetItemSpec spec = new GetItemSpec()
.withPrimaryKey("empid", "1", "deptno", "12");
 Item outcome = table.getItem(spec);
  System.out.pritnln("outcome "+outcome);

In above code I want to apply one more filter like "isAdminU=true" please give me some suggestions to add filters in getItem(....).

Note:

I am able to resolve my use by using table.query(spec) But this method is returning collections I have to parse and get the first item every time which is not required in my case.

As you noticed, DynamoDB's GetItem operation does not accept a filter. It always returns a single item if it exists, so often there is no need for such filtering. But if you really do want such filtering - eg, your items are large and you don't want to waste network bandwidth on sending them if they don't pass your filter - as you suggested yourself, you can use a Query .

Yes, Query 's response format is a little different from GetItem , but any DynamoDB library in any language makes getting the first (and only) item in the response trivial. If you're worried about the possibility of getting more than the first item, you can always set Limit=1 but this isn't necessary if your key condition ensures just one item will match.

You should know that regardless of whether you do an unconditional GetItem and filter in the client or do a Query with a filter at the server side, in both cases the read cost will be the same as the entire item will be read from disk in both cases. The only - small - difference may be in the network bandwidth costs, and this only becomes relevant if the items are large.

You can't specify filter in the GetItem API call in the DynamoDB.

It just supports the projection, for example:

GetItemSpec spec = new GetItemSpec()
    .withPrimaryKey("Id", 206)
    .withProjectionExpression("Id, Title, RelatedItems[0], Reviews.FiveStar")
    .withConsistentRead(true);

However, there is a QuerySpec API, which can be used to get an item with filter condition in the DynamoDB.

 Table table = dynamoDB.getTable("test");

        QuerySpec spec = new QuerySpec()
                .withKeyConditionExpression("apartId = :v_id")
                .withFilterExpression("apartName = :apartName")
                .withValueMap(new ValueMap()
                        .withString(":v_id", "123")
                        .withString(":apartName", "somewhere")
                        );

        ItemCollection<QueryOutcome> items = table.query(spec);

        Iterator<Item> iterator = items.iterator();
        Item item = null;
        while (iterator.hasNext()) {
            item = iterator.next();
            System.out.println(item.toJSONPretty());
        } 

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