我是 Spark 新手,需要 parallelizePairs()
(使用 Java 工作)。
首先,我启动了我的驱动程序:
SparkSession spark = SparkSession
.builder()
.appName("My App")
.config("driver", "org.postgresql.Driver")
.getOrCreate();
但是spark
没有我需要的功能。就parallelize()
通过spark.sparkContext()
现在我想添加
SparkConf sparkConf = new SparkConf();
sparkConf.setAppName("My App");
JavaSparkContext context = new JavaSparkContext(sparkConf);
这样,上下文就有了我需要的功能,但我在这里很困惑。
首先,我从来不需要JavaSparkContext
因为我正在使用 spark-submit
运行并在那里设置主地址。
二、为什么spark.sparkContext()
与 JavaSparkContext
不同以及如何使用 SparkSession
获取它?
如果我在命令行中传递master,我必须设置sparkConf.setMaster( '<master-address-again>' )
?
我已经读过这个:How to create SparkSession from existing SparkContext并理解了这个问题,但我确实需要构建器方式,因为我需要传递 .config("driver", "org.postgresql.Driver")
到它。
请给这里一些光...
编辑
Dataset<Row> graphDatabaseTable = spark.read()
.format("jdbc")
.option("url", "jdbc:postgresql://192.168.25.103:5432/graphx")
.option("dbtable", "public.select_graphs")
.option("user", "postgres")
.option("password", "admin")
.option("driver", "org.postgresql.Driver")
.load();
SQLContext graphDatabaseContext = graphDatabaseTable.sqlContext();
graphDatabaseTable.createOrReplaceTempView("select_graphs");
String sql = "select * from select_graphs where parameter_id = " + indexParameter;
Dataset<Row> graphs = graphDatabaseContext.sql(sql);
最佳答案
使用现有的 SparkContext
初始化 JavaSparkContext
:
JavaSparkContext context = JavaSparkContext(spark.sparkContext());
why spark.sparkContext() is not the same of JavaSparkContext and how to get it using the SparkSession
简而言之,因为 Scala 是比 Java 丰富得多的语言,并且 JavaSparkContext
是一个方便的包装器,旨在解决一些 Java 限制。同时RDD API被移到一边。
If I'm passing the master in command line, must I set sparkConf.setMaster( '' )
没有。优先级是:
- 配置文件
- spark 提交选项
SparkConf
和SparkContext
选项。
but I realy need the builder way because I need to pass the .config("driver", "org.postgresql.Driver") to it.
看起来不对劲。 driver
选项由 DataFrameWriter
和 DataFrameReader
使用。它应该被传递到那里。
关于java - SparkContext和SparkSession : How to get the "parallelizePairs()"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46137174/