【问题标题】:Spark (scala) - Iterate over DF column and count number of matches from a set of itemsSpark (scala) - 遍历 DF 列并计算一组项目的匹配数
【发布时间】:2017-08-04 17:40:39
【问题描述】:

所以我现在可以遍历数据框中的一列字符串,并检查任何字符串是否包含大型字典中的任何项目(请参阅here,感谢@raphael-roth 和@tzach-zohar)。基本的 udf(不包括广播 dict 列表)是:

val checkerUdf = udf { (s: String) => dict.exists(s.contains(_)) }

df.withColumn("word_check", checkerUdf($"words")).show()

接下来我要做的是以最有效的方式计算来自dict 集合的匹配数(我正在处理非常大的数据集和dict 文件)。

我一直在尝试在 udf 中使用 findAllMatchIn,同时使用 count 和 map:

val checkerUdf = udf { (s: String) => dict.count(_.r.findAllMatchIn(s))

// OR
val checkerUdf = udf { (s: String) => dict.map(_.r.findAllMatchIn(s))

但这会返回一个迭代器列表(空和非空)我得到一个类型不匹配(找到迭代器,需要布尔值)。我不确定如何计算非空迭代器(countsizelength 不起作用)。

知道我做错了什么吗?有没有更好/更有效的方法来实现我想要做的事情?

【问题讨论】:

  • 为什么不从新列中算出真值?
  • 来自dict.exists(s.contains(_)) ?对于任意数量的事件,它只会返回一个 true。
  • 该 udf 将返回另一列,匹配为真,不匹配为假。所以你可以数数是真的。 :) 我希望你明白了
  • 但它会返回一个带有真/假的列,说明是否存在匹配。我需要为每个字符串(行)找到多少匹配项的计数(Int),而不是为整个列找到多少正确项。
  • 请在下方查看我的答案

标签: scala apache-spark


【解决方案1】:

您可以将其他问题的答案更改为

import org.apache.spark.sql.functions._
val checkerUdf = udf { (s: String) => dict.count(s.contains(_)) }

df.withColumn("word_check", checkerUdf($"words")).show()

鉴于dataframe

+---+---------+
|id |words    |
+---+---------+
|1  |foo      |
|2  |barriofoo|
|3  |gitten   |
|4  |baa      |
+---+---------+

和dict文件为

val dict = Set("foo","bar","baaad")

你的输出应该是

+---+---------+----------+
| id|    words|word_check|
+---+---------+----------+
|  1|      foo|         1|
|  2|barriofoo|         2|
|  3|   gitten|         0|
|  4|      baa|         0|
+---+---------+----------+

希望回答对你有帮助

【讨论】:

  • 很高兴听到@renegademonkey :) 感谢您的接受和支持 :)
猜你喜欢
  • 2020-05-27
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多