King Skippus
King Skippus

Reputation: 3826

How to get text from app engine datastore?

Can someone please tell me how to get a Text value out of a Google App Engine datastore using Java? I have some entities in the datastore with a Text property named longDescription. When I try this:

DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
Query q = new Query("Items");
PreparedQuery pq = ds.prepare(q);
for (Entity result : pq.asIterable()) {
    Text longDescription = (Text)result.getProperty("longDescription");
}

I'm getting this warning on the longDescription assignment line:

WARNING: /pstest
java.lang.ClassCastException: java.lang.String cannot be cast to
    com.google.appengine.api.datastore.Text

I'm absolutely bumfuzzled here. The only string in my code is the literal "longDescription" that is used to fetch the correct property. If I put this just above the assignment line:

log.warning("Type is " + (result.getProperty("longDescription")).getClass());

I see the following output:

WARNING: Type is class com.google.appengine.api.datastore.Text

Okay, so result.getProperty("longDescription") really, really is a Text object that is being passed back as an object. I've even tried using the fully qualified name (com.google.appengine.api.datastore.Text) instead of just Text with the same results. Where is the String cast coming in? And more importantly, how do I get that Text out of the datastore? I'm at my wit's end here, and any help would be appreciated!

Oh, one other possibly relevant note: This is the assignment I used when inserting the property into the datastore:

Entity eItem = new Entity("Items");
eItem.setProperty("longDescription", new Text(req.getParameter("ldes")));
ds.put(eItem);

When I look at the description in my management console, it seems to be over 500 characters, and it's displayed like this:

<Text: This is a long form description of an item in the store that is access...>

Did I screw something up when inserting it? If so, how do you insert Text items into the datastore?

Upvotes: 4

Views: 3235

Answers (2)

David Okwii
David Okwii

Reputation: 7830

Here's my code;

DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        Query q = new Query(entityKind);

        PreparedQuery pq = ds.prepare(q);
        for (Entity e : pq.asIterable()) {
            String longtext = ((Text)e.getProperty("somelongdescription")).getValue();}

Upvotes: 0

King Skippus
King Skippus

Reputation: 3826

I figured out the problem, Wei Hao was right in the comments above. It seems that at some point, I inserted a test String as a longDescription instead of a Text. I'm going to chalk this up to being a lesson learned from the school of hard knocks due to being a bit of a noob with the datastore.

For anyone else who runs across this question, the answer is: If you're iterating over a list of query results, make sure that you're getting back what you expect on every result that comes back! Remember, this isn't an RDBMS and every entity can have a different datatype for the same property. So yes, you can have 1,572,394 entities in which longDescription is a Text and one entity in which longDescription is a String, and this will hose you up.

Here's a little code snippet that probably would help to diagnose this issue:

DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
Query q = new Query("Items");
PreparedQuery pq = ds.prepare(q);
for (Entity result : pq.asIterable()) {
    if (longDescription isinstanceof Text)
        Text longDescription = (Text)result.getProperty("longDescription");
    else
        log.severe("Unexpected datatype: longDescription is a "
            + result.getProperty("longDescription").getClass().toString());
}

Upvotes: 4

Related Questions