Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 653
Jyothi Sony Avatar asked Nov 26 '25 02:11

Jyothi Sony


2 Answers

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 - e.g., 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.

like image 75
Nadav Har'El Avatar answered Nov 27 '25 15:11

Nadav Har'El


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());
        } 
like image 44
sumitya Avatar answered Nov 27 '25 16:11

sumitya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!