我试图通过只获取数组中包含单词的行来筛选数据集。我使用contains方法,它适用于字符串,但不适用于数组。下面是代码
val dataSet = spark.read.option("header","true").option("inferschema","true").json(path).na.drop.cache()
val threats_path = spark.read.textFile("src/main/resources/cyber_threats").collect()
val newData = dataSet.select("*").filter(col("_source.raw_text").contains(threats_path)).show()它不能工作,因为threats_path是字符串数组,包含字符串的工作。任何帮助都将不胜感激。
发布于 2018-10-02 17:38:20
可以在列上使用isin udf。
会像这样,
val threats_path = spark.read.textFile("src/main/resources/cyber_threats").collect()
val dataSet = ???
dataSet.where(col("_source.raw_text").isin(thread_path: _*))注意,如果thread_paths的大小很大,这将对性能产生影响,因为collect和使用isin的过滤器都会影响性能。
我建议您使用filter dataSet和threats_path使用join。会像这样,
val dataSet = spark.read.option("header","true").option("inferschema","true").json(path).na.drop
val threats_path = spark.read.textFile("src/main/resources/cyber_threats")
val newData = threats_path.join(dataSet, col("_source.raw_text") === col("<col in threats_path >"), "leftouter").show()希望这能有所帮助
https://stackoverflow.com/questions/52613487
复制相似问题