如何通过远程可执行文件程序化地将数据写入Google App Engine数据库?

4 投票
1 回答
1243 浏览
提问于 2025-04-16 15:45

我想要提前填充一些数据,并定期将数据放入Google Appengine数据库。

我想用Java和Python写一个程序,连接到我的GAE服务,并把数据上传到我的数据库里。

我该怎么做呢?

谢谢!

1 个回答

3

请使用RemoteAPI来以编程方式完成这个任务。

在Python中,你可以先按照这里的说明配置appengine_console.py。

配置好之后,你可以在Python的命令行中输入以下命令:

$ python appengine_console.py yourapp

>>> import yourdbmodelclassnamehere
>>> m = yourmodelclassnamehere(x='',y='')
>>> m.put()

这里有一段Java版本的代码,说明得很清楚(直接引用自GAE文档的Remote API页面):

package remoteapiexample;

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.tools.remoteapi.RemoteApiInstaller;
import com.google.appengine.tools.remoteapi.RemoteApiOptions;
import java.io.IOException;

public class RemoteApiExample {
    public static void main(String[] args) throws IOException {
        String username = System.console().readLine("username: ");
        String password = 
            new String(System.console().readPassword("password: "));
        RemoteApiOptions options = new RemoteApiOptions()
            .server("<your app>.appspot.com", 443)
            .credentials(username, password);
        RemoteApiInstaller installer = new RemoteApiInstaller();
        installer.install(options);
        try {
            DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
            System.out.println("Key of new entity is " + 
                ds.put(new Entity("Hello Remote API!")));
        } finally {
            installer.uninstall();
        }
    }
}

撰写回答