繁体   English   中英

优化Flink转换

[英]Optimizing Flink transformation

我有以下方法来计算DataSet值的概率:

/**
   * Compute the probabilities of each value on the given [[DataSet]]
   *
   * @param x single colum [[DataSet]]
   * @return Sequence of probabilites for each value
   */
  private[this] def probs(x: DataSet[Double]): Seq[Double] = {
        val counts = x.groupBy(_.doubleValue)
          .reduceGroup(_.size.toDouble)
          .name("X Probs")
          .collect

        val total = counts.sum

        counts.map(_ / total)
  }

问题是,当我提交使用此方法的flink作业时,由于任务TimeOut导致flink杀死了该作业。 我对只有40.000个实例和9个属性的DataSet上的每个属性执行此方法。

有什么办法可以使我的代码更有效吗?

经过几次尝试,我使其与mapPartition一起mapPartition ,该方法是InformationTheory类的一部分,该类进行一些计算以计算熵,互信息等。因此,例如, SymmetricalUncertainty的计算方法如下:

/**
   * Computes 'symmetrical uncertainty' (SU) - a symmetric mutual information measure.
   *
   * It is defined as SU(X, y) = 2 * (IG(X|Y) / (H(X) + H(Y)))
   *
   * @param xy [[DataSet]] with two features
   * @return SU value
   */
  def symmetricalUncertainty(xy: DataSet[(Double, Double)]): Double = {
    val su = xy.mapPartitionWith {
      case in ⇒
        val x = in map (_._2)
        val y = in map (_._1)

        val mu = mutualInformation(x, y)
        val Hx = entropy(x)
        val Hy = entropy(y)

        Some(2 * mu / (Hx + Hy))
    }

    su.collect.head.head
  }

这样,我可以有效地计算entropy ,互信息等。问题是,它仅在并行度为1的情况下工作,问题出在mapPartition

有什么方法可以与我在SymmetricalUncertainty执行的操作类似,但是可以在任何并行度下进行操作吗?

我终于做到了,不知道它是否是最好的解决方案,但是可以在n个并行级别上工作:

def symmetricalUncertainty(xy: DataSet[(Double, Double)]): Double = {
    val su = xy.reduceGroup { in ⇒
        val invec = in.toVector
        val x = invec map (_._2)
        val y = invec map (_._1)

        val mu = mutualInformation(x, y)
        val Hx = entropy(x)
        val Hy = entropy(y)

        2 * mu / (Hx + Hy)
    }

    su.collect.head
  } 

您可以在InformationTheory.scala中检查整个代码,并对其进行测试InformationTheorySpec.scala

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM