2012-03-13 76 views
1

我正嘗試向GAE中的mapreduce發送發佈請求。 谷歌提供Gae MapReduce。如何以編程方式發送發佈請求

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; 

這樣做,我得到空指針異常,當服務器試圖獲取參數 「配置」;

我應該如何構建請求,使其與textarea的html場景相同?

回答

3

以下是我做到了一次使用任務隊列:

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作業不會打的限制太多。但是您也可以安全地使用默認隊列。