Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamodb: how to filer all items which do not have a certain attribute?

I have a table of users with a primary hash key of userId. each user may/may not have a string attribute called "environment". I would like to get all the users which have "environment"="xyz" or which do not have the "environment" attribute.

The following code will filter those users with environment=xyz, but how do I filter those items with no environment at all? Dynamo API will not allow to filter on an empty String.

    AmazonDynamoDBClient client = DbClientManager.getDynamoDbClient();

    ArrayList<AttributeValue> avList = new ArrayList<AttributeValue>();
    avList.add(new AttributeValue().withS("xyz"));

    Condition scanFilterCondition = new Condition()
            .withComparisonOperator(ComparisonOperator.EQ.toString())
            .withAttributeValueList(avList);
    Map<String, Condition> conditions = new HashMap<>();
    conditions.put("environment", scanFilterCondition);

    ScanRequest scanRequest = new ScanRequest()
            .withTableName("users")
            .withAttributesToGet(
                    "userId", 
                    "environment");
            .withScanFilter(conditions);

    ScanResult result = client.scan(scanRequest);

For now I just dropped the scan filter, and I do the filtering client-side. Bit is there any way to do it server side?

Thanks, Aliza

like image 486
Aliza Avatar asked Jun 02 '14 13:06

Aliza


People also ask

Does Amazon DynamoDB support conditional operations?

Yes, like all the other database management systems, DynamoDB also supports all the conditional operators, User can specify a condition that is satisfied for a put, update, or delete operation to work on an item.

Can DynamoDB sort null keys?

Can the DynamoDB sort key be null? DynamoDB does not support null for sort key.

Does DynamoDB query return all items?

The Query operation in Amazon DynamoDB finds items based on primary key values. You must provide the name of the partition key attribute and a single value for that attribute. Query returns all items with that partition key value.

What is the difference between scan and query in DynamoDB?

DynamoDB offers two ways to access information stored: Query and Scan. A Query will rely on the primary-key to find information. Query can point directly to a particular item (or set ot items) and retrieve them in a fast and efficient way. Scan, as the name suggests, will browse table items from start to finish.


1 Answers

Hope I'm not too late. I've found useful function which you could use in the query. I did not check with ScanRequest but with QueryRequest works as charm.

QueryRequest queryRequest = new QueryRequest()
            .withTableName("YouTableName")
    queryRequest.setFilterExpression(" attribute_not_exists(yourAttributeName) ")
    queryRequest.setExpressionAttributeValues(expressionAttributeValues)
    queryRequest.setExclusiveStartKey(ifYouHave)
    queryRequest.setSelect('ALL_ATTRIBUTES')
    queryRequest.setExpressionAttributeNames(youNames)

attribute_not_exists(yourAttributeName) works with ":aws-sdk:1.11.11" also you could use attribute_exists(yourAttributeName)

like image 95
Vadim Avatar answered Sep 17 '22 11:09

Vadim