spark scala最有效的方法来进行部分字符串计数

Gam*_*ows 2 string scala match apache-spark

我有一个问题,关于在1000万长度的火花RDD(或scala数组)中进行部分字符串匹配的最有效方法.考虑以下:

val set1 = Array("star wars", "ipad") //These are the String I am looking for
val set2 = RDD[("user1", "star wars 7 is coming out"),
           ("user1", "where to watch star wars"),
           ("user2", "star wars"),
           ("user2", "cheap ipad")]
Run Code Online (Sandbox Code Playgroud)

我希望能够计算Set1中属于Set1的每个字符串的出现次数.所以结果应该是这样的:

Result = ("star wars", 3),("ipad", 1)
Run Code Online (Sandbox Code Playgroud)

我还想计算搜索该术语的用户数(即不同的用户),因此结果应为:

Result = ("star wars", 2), ("ipad", 1)
Run Code Online (Sandbox Code Playgroud)

我尝试了两种方法,第一种方法是将RDD字符串转换为set,flatMapValues然后进行连接操作,但它耗费内存.我正在考虑的另一种方法是正则表达式方法,因为只需要计数并给出确切的字符串,但我不知道如何使其有效(通过创建函数并在映射RDD时调用它?)

我似乎能够在使用LIKE的pgsql中很容易地做到这一点,但不确定是否存在以相同方式工作的RDD连接.

任何帮助将不胜感激.

Odo*_*ois 5

因此,根据Yijie Shen的建议,您可以使用正则表达式:

val regex = set1.mkString("(", "|", ")").r
val results = rdd.flatMap {
  case (user, str) => regex.findAllIn(str).map(user -> _)
}
val count = results.map(_._2).countByValue()
val byUser = results.distinct().map(_._2).countByValue()
Run Code Online (Sandbox Code Playgroud)