How to get text from the application data store?

Can someone tell me how to get Text value from Google App Engine datastore using Java? I have some objects in a data store with a Text attribute 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 get this warning in the longDescription target line:

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

Here I am absolutely without hesitation. The only line in my code is the literal "longDescription", which is used to retrieve the correct property. If I put this just above the destination line:

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

I see the following output:

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

Ok, so result.getProperty ("longDescription") really, really is a Text object that is passed as an object. I even tried using the full name (com.google.appengine.api.datastore.Text), and not just the text with the same results. Where does the cast of the string go? And more importantly, how do I get this text from the data store? I am here and any help would be appreciated!

Oh, another relevant note: This is the purpose I used when pasting the property into the data store:

 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 looks like this:

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

Did I screw something when I inserted it? If so, how do you insert text elements into the data warehouse?

+4
source share
2 answers

I understood the problem, Wei Hao was right in the comments above. It seems that at some point I inserted a test string as longDescription instead of Text. I am going to do this before being a lesson learned from the school of hard knocks due to the fact that you are a little loaded with a data warehouse.

For everyone who comes across this question, the answer is: if you repeat the list of query results, make sure that you return what you expect from each result is returned! Remember that this is not a DBMS, and each object can have a different data type for the same property. So yes, you can have 1,572,394 objects in which longDescription is Text and one object in which longDescription is String, and that will stretch you.

Here is a small piece of code that will probably help diagnose this problem:

 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()); } 
+3
source

Here is 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();} 
0
source

All Articles