如何在数组[DataFrame]中组合(连接)信息

时间:2015-12-28 22:05:01

标签: scala apache-spark spark-dataframe

我有一个Array [DataFrame],我想检查每个数据帧的每一行,是否有值按列的变化。假设我有三个数据框的第一行,例如:

 (0,1.0,0.4,0.1)
 (0,3.0,0.2,0.1)
 (0,5.0,0.4,0.1)

第一列是ID,我对此ID的理想输出是:

 (0, 1, 1, 0)

表示第二列和第三列已更改,而第三列未更改。 我在这里附上一些数据来复制我的设置

val rdd = sc.parallelize(Array((0,1.0,0.4,0.1),
                               (1,0.9,0.3,0.3),
                               (2,0.2,0.9,0.2),
                               (3,0.9,0.2,0.2),
                               (4,0.3,0.5,0.5)))
val rdd2 = sc.parallelize(Array((0,3.0,0.2,0.1),
                                (1,0.9,0.3,0.3),
                                (2,0.2,0.5,0.2),
                                (3,0.8,0.1,0.1),
                                (4,0.3,0.5,0.5)))
val rdd3 = sc.parallelize(Array((0,5.0,0.4,0.1),
                                (1,0.5,0.3,0.3),
                                (2,0.3,0.3,0.5),
                                (3,0.3,0.3,0.1),
                                (4,0.3,0.5,0.5)))
val df = rdd.toDF("id", "prop1", "prop2", "prop3")
val df2 = rdd2.toDF("id", "prop1", "prop2", "prop3")
val df3 = rdd3.toDF("id", "prop1", "prop2", "prop3")
val result:Array[DataFrame] = new Array[DataFrame](3)
result.update(0, df)
result.update(1,df2)
result.update(2,df3)

如何映射数组并获取输出?

2 个答案:

答案 0 :(得分:2)

您可以将<asp:Label id="lblError" runat="server" style="display:none;"></asp:Label> countDistinct

一起使用
groupBy

答案 1 :(得分:1)

首先,我们需要将所有DataFrames加在一起。

val combined = result.reduceLeft((a,b) => a.join(b,"id"))

为了比较同一标签的所有列(例如“prod1”),我发现在RDD级别上操作更容易(至少对我而言)。我们将数据转换为(id, Seq[Double])

val finalResults = combined.rdd.map{
  x => 
    (x.getInt(0), x.toSeq.tail.map(_.asInstanceOf[Double]))
}.map{ 
  case(i,d) => 
     def checkAllEqual(l: Seq[Double]) = if(l.toSet.size == 1) 0 else 1
     val g = d.grouped(3).toList 
     val g1 = checkAllEqual(g.map(x => x(0)))
     val g2 = checkAllEqual(g.map(x => x(1)))
     val g3 = checkAllEqual(g.map(x => x(2)))
     (i, g1,g2,g3)
}.toDF("id", "prod1", "prod2", "prod3")

finalResults.show()

这将打印:

+---+-----+-----+-----+
| id|prod1|prod2|prod3|
+---+-----+-----+-----+
|  0|    1|    1|    0|
|  1|    1|    0|    0|
|  2|    1|    1|    1|
|  3|    1|    1|    1|
|  4|    0|    0|    0|
+---+-----+-----+-----+