简体   繁体   English

使用 Scala 和 Spark 将列表值与案例类进行比较

[英]Compare a list values with case class using Scala and Spark

I have a dataframe like below.我有一个如下所示的数据框。

+-------+------+-------+-------+
| num1  | num2 |   x   |   y   |
+-------+------+-------+-------+
|    25 |   10 | a&c   | i&j&k |
|    35 |   15 | a&b&d | i&k   |
+-------+------+-------+-------+

I have another data frame structure with the headers like,我有另一个带有标题的数据帧结构,例如,

num1, num2, a, b, c, d, i, j, k

I want to split the column data of x and y from the symbol "&".我想从符号“&”中拆分 x 和 y 的列数据。 Then check whether the split data are matching with the headers above, also considering the columns num1 and num2.然后检查拆分数据是否与上面的标题匹配,还要考虑列 num1 和 num2。 If it so fill the values with 1 else with 0.如果是这样,则用 1 填充值,否则用 0 填充。

The required output is:所需的输出是:

+-------+------+---+---+---+---+---+---+---+
| num1  | num2 | a | b | c | d | i | j | k |
+-------+------+---+---+---+---+---+---+---+
|    25 |   10 | 1 | 0 | 1 | 0 | 1 | 1 | 1 |
|    35 |   15 | 1 | 1 | 0 | 1 | 1 | 0 | 1 |
+-------+------+---+---+---+---+---+---+---+

I have achieved the above output in a method like following.我已经通过以下方法实现了上述输出。 I created another data frame same like the first data frame but the x and y contains with an array of split data like following.我创建了另一个与第一个数据帧相同的数据帧,但 x 和 y 包含一个拆分数据数组,如下所示。

+------+-------+---------+---------+
| num1 | num2  |    x    |    y    |
+------+-------+---------+---------+
|   25 |    10 | [a,c]   | [i,j,k] |
|   35 |    15 | [a,b,d] | [i,k]   |
+------+-------+---------+---------+

Then followed the solution in this question然后按照这个问题中的解决方案

Although it gives me the exact solution, it is ineffective when it comes to the case where there are lot of columns like x and y.虽然它给了我确切的解决方案,但当涉及到像 x 和 y 这样的列很多的情况时,它是无效的。

So now I want to create a case class and match the header values with the data in x,y columns by splitting them to a list.所以现在我想创建一个案例类,并通过将它们拆分为一个列表来将标题值与 x,y 列中的数据进行匹配。 Is it possible or is there any other solution?是否有可能或有其他解决方案? Can someone help me?有人能帮我吗?

After trying several methods at last I came up with the following solution.最后尝试了几种方法后,我想出了以下解决方案。 I found my solution by adding some few changes to the answer for this question: Compare rows of an array column with the headers of another data frame using Scala and Spark .我通过对这个问题的答案添加一些更改找到了我的解决方案: 使用 Scala 和 Spark 将数组列的行与另一个数据帧的标题进行比较 It worked for multiple array columns also.它也适用于多个数组列。 This is the code for it.这是它的代码。

 val df = Seq((25, 10, "a&c", "i&j&k"), (35, 15, "a&b&d", "i&k")
      .toDF("num1", "num2", "x", "y")
  val dfProcessed = df.withColumn("x", split($"x", "&"))
      .withColumn("y", split($"y", "&"))
      .select("num1", "num2", "x", "y")

    val headers = Seq("a", "b", "c", "d", "i", "j", "k")
    val report = dfProcessed.select(Seq("num1", "num2").map(col) ++ headers.map(line => array_contains('x, line)
      || array_contains('y, line) as line) : _*)

    report.show()

I think this may help you.我想这可能对你有帮助。

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

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