HackToHell. Powered by Blogger.

Writing data to Google Datastore in java THE EASY WAY!

I must say that I have got quite obsessed with google's App engine.


I wanted to design advanced applications with , for which i had to master Google's DataStore.
At first after reading through their tutorial , I felt confused and many people have felt the same way , do not worry actually the DataStore is easy to use.
This tutorial is for beginners so if you are an advanced user better check out their javadocs!

Okay I have made a small program to demonstarte.




import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Text;



@SuppressWarnings("serial")
public class IpLoggerServlet extends HttpServlet {

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
Entity loger = new Entity("Writeit");
loger.setProperty("ip",req.getRemoteHost().toString());
loger.setProperty("Date", "today");
         datastore.put(loger);
PrintWriter pw = resp.getWriter();
pw.println("Done");


}
}
Assuming you know how to deploy and do the stuff needed to get it running .Let's analyse how I write to the Datastore!

The data to the DataStore is Written in the form of Entity , which stores custom values!
In the example, I created an Entity loger and set Properties to it (setProperty()).
The data in the properties is only written to the datastore.
In the setProperty method, the first argument is the identifier and the second is data , that can be ANY OBJECT!

Hope this gives you a good idea of Google Data Store.
Share on Google Plus

About hacktohell

Love technology.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment