如何使用 LIKE 运算符作为 pyspark 中的 JOIN 条件作为列

How to use LIKE operator as a JOIN condition in pyspark as a column

我想在 pyspark 中执行以下操作(对于 AWS Glue 作业):

JOIN a and b ON a.name = b.name AND a.number= b.number AND a.city LIKE b.city

例如:

Table一个:

Number Name City
1000 Bob %
2000 Joe London

Table b:

Number Name City
1000 Bob Boston
1000 Bob Berlin
2000 Joe Paris

结果

Number Name City
1000 Bob Boston
1000 Bob Berlin

所以我不知道该怎么做的部分是实现通配符“%”并使用 LIKE 运算符。我知道您可以在字符串上使用 .like(),例如:

df.where(col('col1').like("%string%")).show()

但它需要一个字符串,在我的例子中,我想把它作为一个列来做。类似于以下内容:

result = a.join(
    b,
    (a.name == b.name) &
    (a.number == b.number) &
    (a.city.like(b.city)) # <-- This doesnt work since it is not a string

如有任何帮助,我们将不胜感激!

尝试使用表达式:

import pyspark.sql.functions as F

result = a.alias('a').join(
    b.alias('b'),
    (a.name == b.name) &
    (a.number == b.number) &
    F.expr("b.city like a.city")
)

我认为你打算做 b like a 而不是 a like b 因为 % 在 table a.