[英]Is there a way to filter a field not containing something in a spark dataframe using scala?
希望我是愚蠢的,這很容易。
我有一個包含“url”和“referrer”列的數據框。
我想提取包含頂級域名“www.mydomain.com”和“mydomain.co”的所有引薦來源。
我可以用
val filteredDf = unfilteredDf.filter(($"referrer").contains("www.mydomain."))
但是,由於某種原因,這會刪除包含我的網域的網址www.google.co.uk搜索網址。 有沒有辦法,在火花中使用scala,我可以用谷歌過濾掉任何東西,同時保持我的正確結果?
謝謝
院長
您可以使用not
或者否定謂詞!
所以剩下的就是添加另一個條件:
import org.apache.spark.sql.functions.not
df.where($"referrer".contains("www.mydomain.") &&
not($"referrer".contains("google")))
或單獨的過濾器:
df
.where($"referrer".contains("www.mydomain."))
.where(!$"referrer".contains("google"))
您可以使用正則Regex
。 在這里,您可以找到Scala中正則表達式用法的參考。 在這里,您可以找到有關如何為URL創建正確的正則表達式的一些提示。
因此,在您的情況下,您將擁有以下內容:
val regex = "PUT_YOUR_REGEX_HERE".r // something like (https?|ftp)://www.mydomain.com?(/[^\s]*)? should work
val filteredDf = unfilteredDf.filter(regex.findFirstIn(($"referrer")) match {
case Some => true
case None => false
} )
這個解決方案需要一些工作但是最安全的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.