在Apache Spark中使用单例模式

时间:2019-01-24 19:58:31

标签: scala apache-spark singleton

我必须在RDD的mapPartition函数中使用一个外部库,为此,我必须通过其单例模式的实现来调用它,由于该库不是我的,所以我无法对其进行修改

我正在尝试按分区使用库的功能,但是存在并发冲突,因为有时执行令人满意,但其他时候存在nullPointerException异常,并且我想这是不同分区对该库的并发访问。这就是库被称为Strategy.getStrategy的方式。如何使该库以我提供的全局配置运行,但在每个分区中独立运行,而无需同时访问其内部进程?

private def executeStrategy(iter: Iterator[Row]): Iterator[(String,(MyState,Double))] = {
  val listBest = new util.ArrayList[State]
  Predicate.fuzzyValues = iter.toList

  for (i <- 0 until conf.runNumber) {
    Strategy.getStrategy.executeStrategy(conf.iterByRun, 1, conf.algorithm("algorithm").asInstanceOf[GeneratorType])
    listBest.addAll(Strategy.getStrategy.listBest)
  }

  val result = postMining(listBest)

  result.map(x => (x.getCode.toString, (MyState(x.getCode), x.getEvaluation.get(0).asInstanceOf[Double]))).iterator
}

def run(sparkSession: SparkSession, n: Int): Unit = {
  var data0 = conf.dataBase.repartition(n).persist(StorageLevel.MEMORY_AND_DISK_SER)
  var listBest = new util.ArrayList[State]

  val data1 = data0.rdd.mapPartitions(executeStrategy).reduceByKey((x,y) => x)
}

我希望该库按照我提供的全局配置,按分区执行代码Strategy.getStrategy.executeStrategy (conf.iterByRun, 1, conf.algorithm("algorithm").asInstanceOf[GeneratorType]),但不会出现冲突

0 个答案:

没有答案
相关问题