Silmarilos Silmarilos - 1 year ago 43
Java Question

How do I extract properties from Entities in Google App Engine Datastore using Java

I am using Google App Engine and trying to query / pull data from the Datastores. I have followed nearly 20 different tutorials without any luck.

Here is a picture of my Datastore and the respective sample data I have stored in there:

enter image description here

Here is some of the code I have to pull the data:

//To obtain the keys
final DatastoreService dss=DatastoreServiceFactory.getDatastoreService();
final Query query=new Query("Coupon");
List<Key> keys = new ArrayList<Key>();

//Put the keys into a list for iteration
for (final Entity entity : dss.prepare(query).asIterable(FetchOptions.Builder.withLimit(100000))) {

try {
for (int i = 0; i < keys.size(); i++){
Entity myEntity = new Entity("Coupon", keys.get(i));

System.out.println("Size of the Keys array = " + keys.size());

String description = (String) myEntity.getProperty("desc");

String endDate = (String) myEntity.getProperty("endDate");

System.out.println("Description = " + description);
System.out.println("End Date: " + endDate);

//Map here is empty...
Map<String, Object> test = myEntity.getProperties();

System.out.println("MAP SIZE = " + test.size());
} catch (Exception e){

Size of the Keys array = 2
Description = null
End date = null

I have no clue why the description and end date are null. It is clearly pulling in the right Entity as the size shows 2, which matches the picture shown. Also, when I print the keys out, it matches as well

(Something like this: for the keys.get(i).toString(); -- Entity [!global:Coupon(123)/Coupon(no-id-yet)]:
. Or: Key String = !global:Coupon(5730827476402176)

I have followed the documentation (here) and some examples (here) to the best of my ability but I cannot seem to figure it out. Does anyone have any recommendations or experience in how to obtain the properties from Entities once you have them without them returning null?

I have gone through the following Stackoverflow questions without any success so please do not close this with a simple duplicate question marker on it:
1) How do i get all child entities in Google App Engine (Low-level API)
2) Storing hierarchical data in Google App Engine Datastore?
3) How do you use list properties in Google App Engine datastore in Java?
4) Mass updates in Google App Engine Datastore
5) Checking if Entity exists in google app engine datastore. .

Answer Source

Eureka! Many thanks to all that answered. Patrice and user2649908 especially thank you as you led me to the answer.

So, Patrice was entirely correct in that I was querying to get the keys, building a new entity, and then trying to parse the newly created (empty) entity.

The solution was to utilize PersistenceManager to parse the data and then use getter/ accessor methods to do so. The link for persistence manager (which I more or less just copied directly from as it worked perfectly) is here: How to use JDO persistence manager?

Once I setup the persistence manager, I was able to get it to pull the data using this code:

try {
            for (int i = 0; i < keys.size(); i++){

                //See the link for How to use JDO persistence manager on how to use this
                PersistenceManager pm = MyPersistenceManagerClass.getPM();

                //Need to cast it here because it returns an object
                Coupon coupon = (Coupon) pm.getObjectById(Coupon.class, keys.get(i));

                System.out.println("Created by = " + coupon.getCreatedBy());

                System.out.println("Description = " + coupon.getDesc());

                System.out.println("Modified by = " + coupon.getModifiedBy());
        } catch (Exception e){