用两个词过滤行 Spark Streaming

filter the lines by two words Spark Streaming

有没有办法用一个表达式过滤包含单词 "word1" 或另一个 "word2" 的行 像 :

val res = lines.filter(line => line.contains("word1" or "word2"))

因为这个表达式不起作用。

提前致谢

如果行是 String 最佳选择将正则表达式:

val pattern = "word1|word2".r

lines.filter(line => pattern.findFirstIn(line).isDefined)

否则(其他序列类型)你可以使用Seq.exists:

lines.filter(line => Seq("foo", "bar").exists(s => line.contains(s)))

它接受一个从元素到布尔值的映射(这里是 (String) ⇒ Boolean)并且:

tests whether a predicate holds for at least one element of this iterable collection.