火花地图功能中的异步代码

时间:2016-02-25 07:23:29

标签: java multithreading asynchronous apache-spark mapreduce

我想在MapReduce任务中使用spark。在我的map函数中,我使用第三方库进行计算。问题是这个库是异步工作的,如下所示:

long id = lib.doJob(inputData); // start async job

然后我可以将一个监听器附加到库中以获得结果,这就是它的工作原理:

lib.setListener(new Listener() {
    @Override
    public void onDone(long id) {
        Result result = lib.getResult(id); // I can only get result this way
    }
    // ... other progress callbacks
});

所以我的问题是 - 如何在我的地图功能中使用该库?我想要这样的东西:

JavaRDD<Result> results = rdd.map(new MapFuntion<Source, Result> {
    public Result call(Source src) {
        long id = lib.doJob(src);
        // ... ?? wait while lib's job is done and get the result
        return result;
    }
}); 

0 个答案:

没有答案