基于 API 过滤 Spark Dataframe 中的多列

Filter on multiple columns in Spark Dataframe based API

我有一个像这样的数据框:

+--------+-------+--------------------+-------------------+
|     id1|    id2|                body|         created_at|
+--------+-------+--------------------+-------------------+
|1       |      4|....................|2017-10-01 00:00:05|
|2       |      3|....................|2017-10-01 00:00:05|
|3       |      2|....................|2017-10-01 00:00:05|
|4       |      1|....................|2017-10-01 00:00:05|
+--------+-------+--------------------+-------------------+

我想使用 id1id2 过滤 table。例如获取 id1=1, id2=4id1=2, id2=3.

的行

目前,我正在使用循环为 df.filter() 生成一个巨大的查询字符串,即 ((id1 = 1) and (id2 = 4)) or ((id1 = 2) and (id2 = 3))。只是想知道是否有更合适的方法来实现这一目标?

您可以生成一个辅助 DF (table):

tmp:

+--------+-------+
|     id1|    id2|
+--------+-------+
|1       |      4|
|2       |      3|
+--------+-------+

然后加入他们:

SELECT a.*
FROM tab a
JOIN tmp b
  ON (a.id1 = b.id1 and a.id2 = b.id2)

其中 tab 是您的原始 DF,注册为 table