Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DyanmoDb is storing value 1 instead of boolean value true

I have a method which is simply storing flag value true for a particular record. My flag attribute has been defined as Boolean which has value true/false in my DynamoDB database. While running this method, somehow instead of storing true value, it is inserting a new column for the flag attribute as number datatype and writing value 1 instead of true. While debugging I can see it is reading the value as "true" but while writing my guess is it is taking 1 for true and 0 for false, and hence writing 1.

public static ArrayList<UserWishListBuks> removeNotification(int Statusid) {
    AmazonDynamoDBClient ddb = NavigationDrawerActivity.clientManager
            .ddb();
    DynamoDBMapper mapper = new DynamoDBMapper(ddb);
    DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
    Boolean value = true;
    try{
        PaginatedScanList<UserWishListBuks> result = mapper.scan(
                UserWishListBuks.class, scanExpression);
        for (UserWishListBuks bre : result) {
            if( (bre.getBOOK_STATUS_ID()==(Statusid))   )
            {
                bre.setNOTIFICATION_FLAG(true);
                mapper.save(bre);
            }
        }
    }catch (AmazonServiceException ex) {
        NavigationDrawerActivity.clientManager
                .wipeCredentialsOnAuthError(ex);
    }
    return null;
   }
like image 905
Tag Avatar asked Nov 23 '17 01:11

Tag


People also ask

How to store boolean value in DynamoDB?

DynamoDb will store the boolean value as 0 or 1 by default. Use the following decorators to save the attribute as false or true respectively.

Does DynamoDB support Boolean?

DynamoDB supports the Java Set , List , and Map collection types. The following table summarizes how these Java types map to the DynamoDB types. BOOL (Boolean type), 0 or 1. S (string type).

What does S mean in DynamoDB?

But the only attribute types that are valid for keys are S (string), N (number), and B (binary). Maps are not valid attribute types for either partition or sort keys, so you can't use them when defining your table or index. DynamoDB is schema-less.

What does M mean in DynamoDB?

M. An attribute of type Map. For example: "M": {"Name": {"S": "Joe"}, "Age": {"N": "35"}} Type: String to AttributeValue object map.


2 Answers

DynamoDb will store the boolean value as 0 or 1 by default.

Use the following decorators to save the attribute as false or true respectively.

@DynamoDBTyped(DynamoDBAttributeType.BOOL)
@DynamoDBAttribute
private boolean notificationFlag;

Note: @DynamoDBNativeBoolean which used to do this is deprecated

like image 121
VKB Avatar answered Oct 23 '22 22:10

VKB


That's expected, have a look at the datatypes docs for dynamodb: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.DataTypes.html

The Java type of Boolean will be stored as a number type in dynamodb, 0 or 1. Alternatively, you can use @DynamoDBNativeBooleanType to map a Java Boolean to the DynamoDB BOOL data type

like image 32
Moe Avatar answered Oct 23 '22 21:10

Moe