Recently AWS announced JSON support for DynamoDB among many other cool features. DynamoDB is becoming a favorite data storage among developers who don't want the overhead of maintaing and looking after of databases. Being NOSQL in nature, it doesn't need any database query expertise unlike its counterpart - the RDBMS.
I'll try to explain in short how this works wonder for RESTful services. As you know the common usage of RESTful services is through JSON request/responses. JSON is a pretty powerful way of passing data around and is becoming an industry standard. For demonstration purposes I created a simple Play-framework app.
I've implement 2 endpoints -
POST - /rest/setData
PUT - /rest/updateData
The setData accepts a POST json request and insert the entire json body into the dynamoDB table as an item.
The controller for the POST looks something like this -
public Result setData(){
JsonNode jsonNode = request().body().asJson();
DataResponse response = this.dbService.set(jsonNode);
return ok(Json.toJson(response));
}
The access layer setter looks something like this then -
public String set(JsonNode json) {
String id = UUID.randomUUID().toString().replaceAll("-","");
Table table = dynamoDB.getTable("jsonTable");
Item item =new Item()
.withPrimaryKey("id", id)
.withJSON("document", json.toString());
table.putItem(item);
return id;
}
As you can see, I created a unique ID for the HashKey of the table, Then used the item to create the new item to be inserted into the table and finally did a table.putItem() to insert the item in the database. The only point of interest here is the wither JSON to which I passed the column name and the entire json POST body. This is a great neat feature that I really liked.
Now lets say you want to update/delete items within this document item entry you can simply use the UpdateItem class to do so. Heres how I went about it -
public JsonNode update(JsonNode json) {
Table table = dynamoDB.getTable("people");
UpdateItemOutcome item = table.updateItem(new UpdateItemSpec()
.withPrimaryKey("id", json.get("id").textValue())
.withUpdateExpression("SET document.current_city = :city")
.withValueMap(new ValueMap().withString(":city", json.get("location").textValue())));
return Json.parse(item.getItem().getJSON("document"));
}
This is automatically add a new entry to existing JSON blob, easy peasy! You can download the entire app from here - girishpandit88/dynamoDB-demo-json
There is bit more to read about this on AWS official blog here - https://aws.amazon.com/blogs/aws/dynamodb-update-json-and-more/
Hope you found my blog interesting and will give a shot at Json support for dynamoDB yourself!
I'll try to explain in short how this works wonder for RESTful services. As you know the common usage of RESTful services is through JSON request/responses. JSON is a pretty powerful way of passing data around and is becoming an industry standard. For demonstration purposes I created a simple Play-framework app.
I've implement 2 endpoints -
POST - /rest/setData
PUT - /rest/updateData
The setData accepts a POST json request and insert the entire json body into the dynamoDB table as an item.
The controller for the POST looks something like this -
public Result setData(){
JsonNode jsonNode = request().body().asJson();
DataResponse response = this.dbService.set(jsonNode);
return ok(Json.toJson(response));
}
The access layer setter looks something like this then -
public String set(JsonNode json) {
String id = UUID.randomUUID().toString().replaceAll("-","");
Table table = dynamoDB.getTable("jsonTable");
Item item =new Item()
.withPrimaryKey("id", id)
.withJSON("document", json.toString());
table.putItem(item);
return id;
}
As you can see, I created a unique ID for the HashKey of the table, Then used the item to create the new item to be inserted into the table and finally did a table.putItem() to insert the item in the database. The only point of interest here is the wither JSON to which I passed the column name and the entire json POST body. This is a great neat feature that I really liked.
Now lets say you want to update/delete items within this document item entry you can simply use the UpdateItem class to do so. Heres how I went about it -
public JsonNode update(JsonNode json) {
Table table = dynamoDB.getTable("people");
UpdateItemOutcome item = table.updateItem(new UpdateItemSpec()
.withPrimaryKey("id", json.get("id").textValue())
.withUpdateExpression("SET document.current_city = :city")
.withValueMap(new ValueMap().withString(":city", json.get("location").textValue())));
return Json.parse(item.getItem().getJSON("document"));
}
This is automatically add a new entry to existing JSON blob, easy peasy! You can download the entire app from here - girishpandit88/dynamoDB-demo-json
There is bit more to read about this on AWS official blog here - https://aws.amazon.com/blogs/aws/dynamodb-update-json-and-more/
Hope you found my blog interesting and will give a shot at Json support for dynamoDB yourself!
No comments:
Post a Comment