1

I'm trying to enter values into DynamoDB from a JSON string and one of those values is a binary. For example, binaryString = 'AAAcd'. I want 'AAAcd' to be the binary in the DynamoDB entry, but when I convert the string to a binary and put it into an item, the value has been changed due to the encoding. For example, the item in DynamoDB will be 'Qha=e' How can I get the binary value in DynamoDB to be 'AAAcd'?

    String binary = "AAAcd";
    byte[] bytes = binary.getBytes();
    Item item = new Item();
    item.withBinary("binary_col", bytes);

With the above, the value under "binary_col" is not "AAAcd", but "Qha=e".

2
  • where are you seeing the value as "Qha=e". Did you get the data from database using Java API and printed in console? I have tried it and it is working fine for me. Also, you should see the datatype as "B" in DynamoDB if you have defined it correctly. Commented Aug 23, 2016 at 15:37
  • Was my answer helpful? May I request you to accept my answer if it was helpful? Commented Aug 30, 2016 at 13:22

1 Answer 1

1

Convert the data as string as below and check the result. You should see the value as "AAAcd".

Short Answer:-

String binaryDataAsString = new String (item.getBinary("binary_col"));              
System.out.println("Binary data as string ====================>" + binaryDataAsString);

Just for your reference:-

public Boolean getAutoTableDataWithoutMapper(String autoId) {

    DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);

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

    ItemCollection<QueryOutcome> items = null;

    QuerySpec querySpec = new QuerySpec();

    querySpec.withKeyConditionExpression("autoID = :val1").withValueMap(new ValueMap().withString(":val1", autoId));
    items = table.query(querySpec);

    Iterator<Item> iterator = items.iterator();

    while (iterator.hasNext()) {
        Item itemData = iterator.next();

        System.out.println("Json data ====================>" + itemData.toJSONPretty());
        System.out.println("Binary data ====================>" + itemData.getBinary("binaryData"));

        String binaryDataAsString = new String(itemData.getBinary("binaryData"));

        System.out.println("Binary data as string ====================>" + binaryDataAsString);

    }

    return true;

}

Output:- Please look at the "Binary data as string". The string is displayed correctly.

If you look at Json data, the value is displayed as mentioned in your question (i.e. "QUFBY2Q=").

Json data ====================>{
  "binaryData" : "QUFBY2Q=",
  "autoID" : "fge",
  "alexandriaID" : "122"
}
Binary data ====================>[B@3954d008
Binary data as string ====================>AAAcd
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.