Gae MapReduce。如何以编程方式发送帖子请求

时间:2012-03-13 14:30:53

标签: google-app-engine post mapreduce

我正在尝试将发送请求发送到GAE中的mapreduce。 Google提供了

private String generateHtml(String configXml) {
return "<html>"
    + "<body>"
    + "<form action=\"/mapreduce/start\" method=\"POST\">"
    + "<textarea name=\"configuration\" rows=20 cols=80>"
    + configXml
    + "</textarea>"
    + "<input type=\"submit\" value=\"Start\">"
    + "</form>";

}

我如何配置:

 String configString = ConfigurationXmlUtil.convertConfigurationToXml(config);
 String body = "configuration=" + configString;

这样做当服务器尝试获取参数“configuration”时,我得到空指针异常;

我应该如何构建请求,以便与textarea的html场景相同?

1 个答案:

答案 0 :(得分:3)

以下是我使用TaskQueue执行此操作的方法:

import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit;
import static com.google.appengine.api.taskqueue.TaskOptions.Builder.withUrl;

import com.google.appengine.api.taskqueue.TaskOptions;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.InputFormat;
import org.apache.hadoop.mapreduce.Mapper;
//... other imports

// mapreduce config
Configuration conf = new Configuration(false);
conf.setClass("mapreduce.map.class", YourMapperClass.class, Mapper.class);
conf.setClass("mapreduce.inputformat.class", DatastoreInputFormat.class, InputFormat.class);

// setup callbacks if you need to know when the job is done.
//conf.set(AppEngineJobContext.DONE_CALLBACK_URL_KEY, "/job-done-callback");

// you can also specify a queue. Defaults to "default" :)
//conf.set(AppEngineJobContext.CONTROLLER_QUEUE_KEY, "mrqueue");
//conf.set(AppEngineJobContext.DONE_CALLBACK_QUEUE_KEY, "mrqueue");
//conf.set(AppEngineJobContext.WORKER_QUEUE_KEY, "mrqueue");


conf.set(DatastoreInputFormat.ENTITY_KIND_KEY, 'YourEntityKind');
String xml = ConfigurationXmlUtil.convertConfigurationToXml(conf);

TaskOptions opts = withUrl("/mapreduce/start")
    .param("configuration", xml)
    .method(TaskOptions.Method.POST)
    .header("X-Requested-With", "XMLHttpRequest");

// use our dedicated dbm4g queue
// this needs to be configured by a user
QueueFactory.getQueue("mrqueue").add(opts);

我使用非默认队列来确保我的MapReduce作业不会达到太多限制。但您也可以安全地使用默认队列。