filter the lines by two words Spark Streaming

陌路散爱 提交于 2020-01-04 05:36:28

问题


Is there a way to filter with one expression the lines containing a word "word1" or the other "word2" something like :

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

because this expression doesn't work.

Thank you in advance


回答1:


If line is a String optimal choice would regexp:

val pattern = "word1|word2".r

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

otherwise (other sequence type) you can use Seq.exists:

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

which takes a single which maps from element to boolean (here (String) ⇒ Boolean) and:

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



来源:https://stackoverflow.com/questions/36650184/filter-the-lines-by-two-words-spark-streaming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!