无法从数据存储中读取

时间:2013-12-04 09:53:15

标签: java google-app-engine google-cloud-datastore

创建实体并设置属性

package pack.exp;
import java.io.IOException;
import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@SuppressWarnings("serial")
public class IkaiLanServlet extends HttpServlet 
{
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws...
{
    Entity alice = new Entity("Person", "Alice");
    alice.setProperty("gender", "female");
    alice.setProperty("age", 20);

    Key bobKey = KeyFactory.createKey("Person", "Bob");
    Entity bob = new Entity(bobKey);
    bob.setProperty("gender", "male");
    bob.setProperty("age", "23");

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    datastore.put(alice);
    datastore.put(bob);


    resp.setContentType("text/plain");
    resp.getWriter().println("Bye Bye");
}
}

在同一个包中创建另一个servlet

package pack.exp;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class Read extends HttpServlet
{

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws... 
{
    // TODO Auto-generated method stub
    super.doGet(req, resp);

    Key bobKey = KeyFactory.createKey("Person", "Bob");
    Key aliceKey = KeyFactory.createKey("Person", "Alice");

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity alice, bob;

    try {
        alice = datastore.get(aliceKey);
        bob = datastore.get(bobKey);

        Long aliceAge = (Long) alice.getProperty("age");
        Long bobAge = (Long) bob.getProperty("age");
        System.out.println("Alice’s age: " + aliceAge);
        System.out.println("Bob’s age: " + bobAge);
    } 

    catch (EntityNotFoundException e) 
    {
        // Alice or Bob doesn't exist!
    }
}
}

当我部署应用程序时,输出是“Bye Bye”。为什么不读实体。 请帮助我,我是谷歌应用引擎数据存储区的新手..

1 个答案:

答案 0 :(得分:1)

当您转到已映射到IkaiLanServlet的任何网址时,它会回复“再见”,因为您拨打了resp.getWriter().println("Bye Bye")。要阅读实体,请更改

System.out.println("Alice’s age: " + aliceAge);
System.out.println("Bob’s age: " + bobAge);

resp.setContentType("text/plain");
resp.getWriter().println("Alice’s age: " + aliceAge);
resp.getWriter().println("Bob’s age: " + bobAge);

根据文档(https://developers.google.com/appengine/docs/java/#Java_Logging):

  

servlet将标记输出流(System.out)和标准错误流(System.err)写入的所有内容都由App Engine捕获并记录在应用程序日志中。写入标准输出流的行记录在“INFO”级别,写入标准错误流的行记录在“警告”级别。

要在浏览器中查看输出,您必须使用resp

此外,请确保您按顺序访问IkaiLanServletRead的网址,以确保实体位于数据存储区中。