2

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))) {
    keys.add(entity.getKey());
}

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){
    e.printStackTrace();
}

    **OUPUT:** 
    Size of the Keys array = 2
    Description = null
    End date = null
    MAP SIZE = 0

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. .

Community
  • 1
  • 1
PGMacDesign
  • 6,092
  • 8
  • 41
  • 78
  • https://cloud.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/package-summary The "normal way" seems to use "getProperty" and then name the property, not "getProperties" – Patrice Apr 01 '15 at 18:32
  • Indeed you are right, the line in my code: myEntity.getProperty("desc"); Is attempting to do that, but null is being returned. I tried the getProperties() call for the Map test, but no luck there either. – PGMacDesign Apr 01 '15 at 18:38
  • OH WOW I missed that one. My bad sorry :P I'll make sure to read the code thoroughly next time, not focus on one line. When do you put your entities? Can we completely rule out eventual consistency? – Patrice Apr 01 '15 at 18:39
  • That's an excellent question, short answer is, not with 100% certainty. I can confirm though that different entities are being returned and that each has a unique String to it, though why no other data is being returned I do not know. Maybe the issue has to do with me not pinging the server for the Entity, but I am for the key, if that is right, not exactly sure how to query the server using the key. – PGMacDesign Apr 01 '15 at 18:44
  • in your "for" where you add keys to the actual key list, mind trying to run " entity.getProperty("desc") to see?. – Patrice Apr 01 '15 at 18:49
  • 1
    I think I know what you're doing. you're doing a query to get all the keys, then you build a NEW entity with the same key (so it's an empty entity) and then you ask for that NEW entity's parameters. In no way do you ask using the entity the server returned. – Patrice Apr 01 '15 at 18:49
  • 1
    Entity myEntity = new Entity("Coupon", keys.get(i)); <== you create not reading, I don't see datastore.get( XXX ), am I missing something in your code – user2649908 Apr 01 '15 at 18:50
  • Out of curiosity, any reason in particular you are using the low level API to access the datastore? – jirungaray Apr 02 '15 at 01:29
  • jirungaray, honestly, I could not get the higher-level API to work right :( do you happen to have any links to good higher-level tutorials I could follow? I would gladly swap it out if I could – PGMacDesign Apr 02 '15 at 03:24

2 Answers2

2

have you tried this?

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


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

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

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

In your example, you creating entity and it is expected that properties will be empty

user2649908
  • 557
  • 7
  • 15
  • Tis a good thought, but sadly, the issue was revolving around me having an empty object altogether. I updated my code to show the solution I came up with – PGMacDesign Apr 01 '15 at 21:38
2

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){
            e.printStackTrace();
        }
Community
  • 1
  • 1
PGMacDesign
  • 6,092
  • 8
  • 41
  • 78