Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用single Spark上下文在Apache Spark中运行并发作业(操作)_Java_Concurrency_Apache Spark - Fatal编程技术网

Java 如何使用single Spark上下文在Apache Spark中运行并发作业(操作)

Java 如何使用single Spark上下文在Apache Spark中运行并发作业(操作),java,concurrency,apache-spark,Java,Concurrency,Apache Spark,它在Apache Spark文档中指出,“在每个Spark应用程序中,如果多个“作业”(Spark操作)是由不同的线程提交的,则它们可能同时运行。”。有人能解释一下如何为下面的示例代码实现这种并发性吗 SparkConf conf = new SparkConf().setAppName("Simple_App"); JavaSparkContext sc = new JavaSparkContext(conf); JavaRDD<String> file1


它在Apache Spark文档中指出,“在每个Spark应用程序中,如果多个“作业”(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());
SparkConf conf=new SparkConf().setAppName(“简单应用”);
JavaSparkContext sc=新的JavaSparkContext(conf);
javarddfile1=sc.textFile(“/path/to/test_doc1”);
javarddfile2=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());
final JavaSparkContext sc=新的JavaSparkContext(“本地[2],“简单应用”);
ExecutorService ExecutorService=Executors.newFixedThreadPool(2);
//开始线程1
Future future1=executorService.submit(新的可调用(){
@凌驾
public Long call()引发异常{
javarddfile1=sc.textFile(“/path/to/test_doc1”);
返回file1.count();
}
});
//开始线程2
Future future2=executorService.submit(新的可调用(){
@凌驾
public Long call()引发异常{
javarddfile2=sc.textFile(“/path/to/test_doc2”);
返回file2.count();
}
});
//等待线程1
System.out.println(“File1:+future1.get());
//等待线程2
System.out.println(“File2:+future2.get());

尝试以下方法:

    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());
final JavaSparkContext sc=新的JavaSparkContext(“本地[2],“简单应用”);
ExecutorService ExecutorService=Executors.newFixedThreadPool(2);
//开始线程1
Future future1=executorService.submit(新的可调用(){
@凌驾
public Long call()引发异常{
javarddfile1=sc.textFile(“/path/to/test_doc1”);
返回file1.count();
}
});
//开始线程2
Future future2=executorService.submit(新的可调用(){
@凌驾
public Long call()引发异常{
javarddfile2=sc.textFile(“/path/to/test_doc2”);
返回file2.count();
}
});
//等待线程1
System.out.println(“File1:+future1.get());
//等待线程2
System.out.println(“File2:+future2.get());

使用scala并行集合功能

Range(0,10).par.foreach {
  project_id => 
      {
        spark.table("store_sales").selectExpr(project_id+" as project_id", "count(*) as cnt")
        .write
        .saveAsTable(s"counts_$project_id")
    }
}

上面的PS将启动多达10个并行Spark作业,但根据Spark驱动器上可用内核的数量可能会更少。GQ使用Futures的上述方法在这方面更加灵活。

使用scala并行集合功能

Range(0,10).par.foreach {
  project_id => 
      {
        spark.table("store_sales").selectExpr(project_id+" as project_id", "count(*) as cnt")
        .write
        .saveAsTable(s"counts_$project_id")
    }
}

上面的PS将启动多达10个并行Spark作业,但根据Spark驱动器上可用内核的数量可能会更少。GQ使用Futures的上述方法在这方面更加灵活。

您需要启动一个或两个新线程(我相信您可以在网上找到相关说明),然后从两个线程中使用相同的SparkContext。@pzecevic感谢您的回复。我已经编写了一个示例代码,能够在本地模式下执行线程。然而,在纱线集群中运行时,spark上下文在线程执行完成之前以状态Successed结束,因此我没有得到任何输出。你能提出一些建议吗?我可以共享代码,但在注释部分有一个限制。@Sporty我不确定这一点,但设置此配置不会有帮助吗
spark.streaming.concurrentJobs
@void:顾名思义,该设置仅适用于spark流媒体作业。不过,你是对的。该设置控制要运行的并行作业的数量,默认值为1(至少在Spark 2.0.0及之前版本中),您可以在应用程序中使用公平调度。请参见此处-您需要启动一个或两个新线程(我相信您可以在线找到相关说明),然后从两个线程中使用相同的SparkContext。@pzecevic感谢您的回复。我已经编写了一个示例代码,能够在本地模式下执行线程。然而,在纱线集群中运行时,spark上下文在线程执行完成之前以状态Successed结束,因此我没有得到任何输出。你能提出一些建议吗?我可以共享代码,但在注释部分有一个限制。@Sporty我不确定这一点,但设置此配置不会有帮助吗
spark.streaming.concurrentJobs
@void:顾名思义,该设置仅适用于spark流媒体作业。不过,你是对的。该设置控制要运行的并行作业的数量,默认值为1(至少在Spark 2.0.0及之前版本中),您可以在应用程序中使用公平调度。请看这里-我们不能简单地使用
spark.streaming.concurrentJobs
conf来设置并发级别吗?有人有这方面的python实现吗?我们不能简单地使用
spark.streaming.concurrentJobs
conf来设置并发级别吗?有人有这方面的python实现吗?