2

I'm trying to use AWS DynamoDB to store some flexible document model data. Because the document model is purposefully flexible, I don't want to be constrained by building an explicit @DynamoDBTable specification and instead want to insert JSON directly into the table. Is this possible? I have searched low and high for this information. It appears from here:

AWS DynamoDB and Android Development (Put Item into table in DynamoDB)

that the answer may be no.

Community
  • 1
  • 1
heights1976
  • 480
  • 6
  • 16

4 Answers4

4

DynamoDB does support flexible JSON documents, but there is one catch, you will need to make sure the Hash or Hash/Range keys are specified in the document before it is inserted.

Since you want to avoid using the annotated Java objects, the API you want is simply the putItem method on the Table class. This method allows you to insert an Item, which you can construct from a JSON string using the fromJSON method.

The code would look something like:

DynamoDB dynamodb = new DynamoDB(client);
Table myTable = dynamodb.getTable(tableName);

// Make sure your object includes the hash or hash/range key
String myJsonString = "{\"foo\": \"bar\", \"one\": \"two\"}";

// Convert the JSON into an object
Item myItem = Item.fronJSON(myJsonString);

// Insert the Object
table.putItem(myItem);

Edit

I stand corrected, the above code snippet is specific to the AWS Java SDK and I mistakenly believed the fromJSON method was available in the AWS Android SDK.

JaredHatfield
  • 6,381
  • 2
  • 29
  • 32
  • Thanks, I think this helps pin down that this cannot be done in Android (yet). com.amazonaws.services.dynamodbv2.document.Item doesn't come with the Android SDK, and attempting to import the Java SDK crashes Eclipse :P – heights1976 Aug 04 '15 at 21:28
0

Yes, you can store a entire json document into a single 'field'

JSON Document Support: You can now store entire JSON-formatted documents as single DynamoDB items (subject to the newly increased 400 KB size limit that I will talk about in a moment).

https://aws.amazon.com/blogs/aws/dynamodb-update-json-and-more/

E.J. Brennan
  • 45,870
  • 7
  • 88
  • 116
  • Yes, but the new DynamoDB document model should allow fields in the JSON document to serve as the attributes for the DynamoDB table in question. It would seem to defeat some of the advantage of the NoSQL approach if I have to define the specific document model in advance. – heights1976 Aug 04 '15 at 17:31
0

You are correct. The thing which you are trying to do is not currently supported in the AWS SDK for Android. We have noted this down as a feature request and would try to include it in a future release. Thank you for the feedback!

Rohan Dubal
  • 847
  • 5
  • 10
0

The answers below are correct if you write code in Java for native Android -- the AWS SDK for Android DOES NOT support fromJSON uploading. BUT if you are writing your apps in Xamarin Forms, you can use cross platform calls to the AWSSDK for Xamarin Forms that DOES support fromJSON insertion.

heights1976
  • 480
  • 6
  • 16