从Spark2中的Spark临时表创建表后,记录丢失

时间:2018-12-17 08:23:56

标签: scala dataframe rdd partitioning apache-spark-2.0

我从下面的序列创建了一个数据框。

val df = sc.parallelize(Seq((100,23,9.50),
(100,23,9.51),
(100,24,9.52),
(100,25,9.54),
(100,23,9.55),
(101,21,8.51),
(101,23,8.52),
(101,24,8.55),
(101,20,8.56))).toDF("id", "temp","time")

我想通过添加更多行(其中暂时缺少数据)来更新DF。因此,我从mapPartitions迭代了DF以添加新行。

import org.apache.spark.sql.functions._
import org.apache.spark.sql.{DataFrame, Row, Column}

@transient val w = org.apache.spark.sql.expressions.Window.partitionBy("id").orderBy("time")

val leadDf = df.withColumn("time_diff", ((lead("time", 1).over(w) - df("time")).cast("Float")*100).cast("int"))

数据帧迭代在这里:

val result =   leadDf.rdd.mapPartitions(itr =>
  new Iterator[Row] {
    var prevRow = null: Row
    var prevDone = true
    var firstRow = true
    var outputRow: Row = null: Row
    var counter  = 0
    var currRecord = null :Row
    var currRow: Row = if (itr.hasNext) {currRecord = itr.next;  currRecord } else null
    prevRow = currRow
    override def hasNext: Boolean = {
      if (!prevDone) {
        prevRow = incrementValue(prevRow,2)
        outputRow = prevRow
        counter = counter -1
        if(counter == 0) {
          prevDone = true
        }
        true
      } else if (itr.hasNext) {
        prevRow = currRow
        if(counter == 0 && prevRow.getAs[Int](3) != 1 && !isNullValue(prevRow,3 )){
          outputRow = prevRow
          counter = prevRow.getAs[Int](3) - 1
          prevDone = false
        }else if(counter > 0) {
          counter = counter -1
          prevDone = false
        }
        else {
          outputRow = currRow
        }
        //if(counter == 0){
        currRow = itr.next
        true
      } else if (currRow != null) {
        outputRow = currRow
        currRow =null
        true
      } else {
        false
      }
    }
    override def next(): Row = outputRow
  })
  val newDf = spark.createDataFrame(result,leadDf.schema)

在此之后,我可以在数据框中看到12条记录。但是从通过“ newDf”数据帧创建的临时表创建的物理表中获得了10条记录。

newDf.registerTempTable("test")
spark.sql("create table newtest as select * from test")

scala> newDf.count
res14: Long = 12

scala> spark.sql("select * from newtest").count
res15: Long = 10

相同的代码在Spark 1.6中工作正常,最终表计数与数据帧记录计数匹配。

有人可以解释为什么会这样吗?以及解决问题的任何解决方案或解决方法

1 个答案:

答案 0 :(得分:1)

我找到了一种解决方案或解决方法,该方法或解决方法是从RDD [Row]对新创建的数据帧调用修复方法。

val newDf = spark.createDataFrame(result,leadDf.schema).repartition(result.getNumPartitions)