Google Datastore JAVA

we will discover Google Datastore Queries, in this example we will manipulate a book entity with proporties:  id, titleauthorisbndate.



So let's Start;

Create new Entity or Add new Entries:



DatastoreService datastore DatastoreServiceFactory.getDatastoreService();
 Entity entityBook new Entity("Books");        
 entityBook.setProperty("title", "title1");    
 entityBook.setProperty("author", "author1");    
 entityBook.setProperty("isbn", "12E3E31E");  
 entityBook.setProperty("date", new Date());
 datastore.put(entityBook);
In this case the id will be generated automatically by GAE.
you can use Key to manually add the ID
     Key bookKey = KeyFactory.createKey("Books", "id1");
     Entity entityBook = new Entity("books", bookKey);
     entityBook.setProperty("title", "title1");
     entityBook.setProperty("author", "author1);
     entityBook.setProperty("isbn", "12E3E31E");
     entityBook.setProperty("date",new Date() );    
     DatastoreService datastore=DatastoreServiceFactory.getDatastoreService();
     datastore.put(entityBook);
 Filters   
A query's filters set constraints on the propertieskeys, and ancestors of the entities to be retrieved.

Return 5 Books as a List.

 DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
 Query query = new Query("Books").addSort("date", Query.SortDirection.DESCENDING);
 List<Entity> customers = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));
Find and returned a book with matched filter.

 Query query = new Query("Books");
 query.addFilter("title", FilterOperator.EQUAL, "title1");
 PreparedQuery pq = datastore.prepare(query);
 Entity Book = pq.asSingleEntity();
Note: The comparison operator can be any of the following (defined as enumerated constants in the nested class Query.FilterOperator):

OperatorMeaning
EQUALEqual to
LESS_THANLess than
LESS_THAN_OR_EQUALLess than or equal to
GREATER_THANGreater than
GREATER_THAN_OR_EQUALGreater than or equal to
NOT_EQUALNot equal to
INMember of (equal to any of the values in a specified list)
The NOT_EQUAL operator actually performs two queries: one in which all other filters are unchanged and the NOT_EQUAL filter is replaced with a LESS_THAN filter, and one where it is replaced with a GREATER_THAN filter. The results are then merged, in order. A query can have no more than one NOT_EQUAL filter, and a query that has one cannot have any other inequality filters.
The IN operator also performs multiple queries: one for each item in the specified list, with all other filters unchanged and the IN filter replaced with an EQUAL filter. The results are merged in order of the items in the list. If a query has more than one IN filter, it is performed as multiple queries, one for each possible combination of values in the IN lists.
A single query containing NOT_EQUAL or IN operators is limited to no more than 30 subqueries.
Update Entity:
To update, just modify the existing Entity and save it again.
 Query query = new Query("Books");
 query.addFilter("title", FilterOperator.EQUAL, "title1");
 PreparedQuery pq = datastore.prepare(query);
 Entity Book = pq.asSingleEntity();
 
 Book.setProperty("name", name);
 Book.setProperty("email", email);
 
 DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
 datastore.put(Book); //GAE will know save or update
Note: you can get an entity by the id:

         DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
               Key k=KeyFactory.createKey("Books", "id1");       
               Entity book=datastore.get(k);
 
Delete:

To delete, need the entity key.
                DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
                Key k=KeyFactory.createKey("Books", "id1");
                datastore.delete(k);
Note: you can search for an entity using Filters then deleted using datastore.delete(book.getKey());

For more details about datastore queries see: developers.google.com/appengine/docs/java/datastore/queries

Post a Comment

CodeNirvana
Newer Posts Older Posts
Copyright © JEsmairi
Back To Top