【发布时间】:2019-04-17 05:21:58
【问题描述】:
我有一个 Spark 数据框,我需要根据条件进行过滤。
条件是: 数据框中有一列"keyword",我需要调用一个API 传递这个keyword 列值。将对所有 keyword 列值执行此操作。 API 将发回一个我需要与阈值匹配的数字。如果它更大,则需要返回 true 否则为 false。
我为此写了一个 UDF,如下所示..
val filteredDf = df.filter(apiUdf(col("keyword_text")) === true))
val apiUdf = udf((topic: String) => {..
.....
HTTP API call ..
parse the result ...
find out the number from the API resposne..
and then compare it with some threshold value and return true/false
这里的问题是我打开和关闭 HTTP 连接的次数与我拥有多个关键字的次数一样多。有人可以告诉我如何优化这一点,以及这里的 UDF 方法是否可以?
【问题讨论】:
-
您是否考虑过改用
mapPartitions? -
@shay__ 你的意思是使用 mapPartitions 而不是过滤器?
-
不,而不是 udf。类似
df.mapPartitions(... send http ...).filter(... by response ...)
标签: apache-spark apache-spark-sql