4

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?

4

2 に答える 2

4

私は問題を理解しました.Wei Haoは上記のコメントで正しかった. ある時点で、テキストではなく longDescription としてテスト文字列を挿入したようです。これは、データストアに少し慣れていないため、ハードノックの学校から学んだ教訓であると考えています。

この質問に出くわした他の人にとって、答えは次のとおりです。クエリ結果のリストを反復処理している場合は、返されるすべての結果で期待どおりの結果が得られることを確認してください! これは RDBMS ではなく、すべてのエンティティが同じプロパティに対して異なるデータ型を持つことができることに注意してください。そうです、longDescription がテキストである 1,572,394 個のエンティティと、longDescription が文字列である 1 つのエンティティを持つことができます。

この問題の診断に役立つと思われる小さなコード スニペットを次に示します。

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());
}
于 2012-07-09T06:30:23.983 に答える
0

これが私のコードです。

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();}
于 2013-07-12T14:27:09.533 に答える