Search code examples
androidjsongoogle-cloud-platformgoogle-cloud-endpointspojo

Java POJO classes and No sql Datastores


Sorry if this question is very basic but I started seeing no sql storages for an android app and I found App engine endpoints and servlets that as far as i understood, expose an api to do CRUD operations.

The thing that it is difficult to grasp for me is the format of the objects stored and it's efficiency. I read that by using libraries like objectify/gson the objects are stored in json.

Now, suppose I have a class Person that has an attribute Friends that is a list of Person. If two different persons share the same friend it will be stored in two different json objects, duplicating the information. Should I make a different class for the storage that keeps the id of the friends and then when loading a person find his/her friends from a hash map of Person? But that would imply requesting all the Persons to the Web service to construct that map even though I only want to find a Person without friends.

Another way would be to make the 'query' in the server side, return the friends objects of the person that is requested and put them in a hash map for future 'queries' of other Persons. In this way I would transfer less data each time but i would consume more times the webservice which can lead to exhaust the daily request limit quota.

Again sorry for the beginner question. I would appreciate any directions, patterns to solve this problem, in a nutshell how to efficiently -in the sense of space- store objects and efficiently retrieve them -in the sense of the amount of queries and data transfer from the web service- in a nosql database.


Solution

  • Indeed Google Cloud Endpoints will allow you to do CRUD operations through one or more API(s). But, as detailed in the documentation (https://cloud.google.com/appengine/docs/java/endpoints/) it allows you to do much more than that, e.g. "all of the services and features available in App Engine, such as Google Cloud Storage, Mail, Task Queues" etc.

    You can use Objectify when the back end of your endpoints is Datastore. Objectify is the open-source API for Java which is recommended by Google. However, note that the data is not store as json but as Data objects that are called "Entities" which can have properties of different data types. See https://cloud.google.com/appengine/docs/java/datastore/entities for more info.

    The approach to NoSQL database is very different from relational database when it comes to data modelling. You should not care about normalizing your data and storing the same data multiple times is quite a common approach.

    In your case, if two persons share the same friend, you would save the friend information two times, in each person entity. In such a way, when you will query the list of friends for one person you just have to get the person entity through Objectify in your endpoint: it will include the list of friends and it will automatically be transformed to JSON when sent to the front-end.

    I would suggest you try the Google examples (https://cloud.google.com/appengine/docs/java/endpoints/helloworld-java-maven) or even better follow the Udacity MOOC which will help you understanding the entire stack https://www.udacity.com/course/developing-scalable-apps-in-java--ud859

    The tutorials from Romin Irani are also an excellent entry point to this technology https://rominirani.com/google-cloud-endpoints-tutorial-part-1-b571ad6c7cd2#.p4h8rmkt3 There are tutorials for Eclipse as well as Android Studio (I recommend using the second one).