如何使用单个Spark上下文在Apache Spark中运行并发作业(动作)
它说,在Apache Spark文档中,“
”。有人可以为以下示例代码解释如何实现此并发吗?
SparkConf conf = new SparkConf().setAppName("Simple_App"); JavaSparkContext sc = new JavaSparkContext(conf);
JavaRDD<String> file1 = sc.textFile("/path/to/test_doc1");
JavaRDD<String> file2 = sc.textFile("/path/to/test_doc2");
System.out.println(file1.count());
System.out.println(file2.count());
这两个作业是独立的,必须同时运行。
谢谢。
回答:
尝试这样的事情:
final JavaSparkContext sc = new JavaSparkContext("local[2]","Simple_App"); ExecutorService executorService = Executors.newFixedThreadPool(2);
// Start thread 1
Future<Long> future1 = executorService.submit(new Callable<Long>() {
@Override
public Long call() throws Exception {
JavaRDD<String> file1 = sc.textFile("/path/to/test_doc1");
return file1.count();
}
});
// Start thread 2
Future<Long> future2 = executorService.submit(new Callable<Long>() {
@Override
public Long call() throws Exception {
JavaRDD<String> file2 = sc.textFile("/path/to/test_doc2");
return file2.count();
}
});
// Wait thread 1
System.out.println("File1:"+future1.get());
// Wait thread 2
System.out.println("File2:"+future2.get());
以上是 如何使用单个Spark上下文在Apache Spark中运行并发作业(动作) 的全部内容, 来源链接: utcz.com/qa/398416.html