如何以编程方式将数据从远程可执行文件放入Google Appengine数据库?

时间:2022-11-22 23:35:00

I would like to pre-fill and periodically put data to the Google Appengine database.

我想预先填写并定期将数据放入Google Appengine数据库。

I would like to write a program in java and python that connect to my GAE service and upload data to my database.

我想在java和python中编写一个连接到我的GAE服务并将数据上传到我的数据库的程序。

How can I do that?

我怎样才能做到这一点?

Thanks

1 个解决方案

#1


3  

Please use RemoteAPI for doing this programmatically.

请使用RemoteAPI以编程方式执行此操作。

In python, you can first configure the appengine_console.py as described here

在python中,您可以首先配置appengine_console.py,如此处所述

Once you have that, you can launch and write the following commands in the python shell:

完成后,您可以在python shell中启动并编写以下命令:

$ python appengine_console.py yourapp

$ python appengine_console.py yourapp

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

And here is code from the java version which is self explanatory (directly borrowed from the remote api page on gae docs):

这里是java版本的代码,它是自解释的(直接从gae docs上的远程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();
        }
    }
}

#1


3  

Please use RemoteAPI for doing this programmatically.

请使用RemoteAPI以编程方式执行此操作。

In python, you can first configure the appengine_console.py as described here

在python中,您可以首先配置appengine_console.py,如此处所述

Once you have that, you can launch and write the following commands in the python shell:

完成后,您可以在python shell中启动并编写以下命令:

$ python appengine_console.py yourapp

$ python appengine_console.py yourapp

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

And here is code from the java version which is self explanatory (directly borrowed from the remote api page on gae docs):

这里是java版本的代码,它是自解释的(直接从gae docs上的远程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();
        }
    }
}