【问题标题】:How to use LIKE operator as a JOIN condition in pyspark as a column如何在 pyspark 中将 LIKE 运算符用作 JOIN 条件作为列
【发布时间】:2021-03-11 11:54:09
【问题描述】:

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

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

例如:

表a:

Number Name City
1000 Bob %
2000 Joe London

表 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

对此的任何帮助将不胜感激!

【问题讨论】:

标签: python apache-spark pyspark apache-spark-sql aws-glue


【解决方案1】:

尝试使用表达式:

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,因为% 在表a 中。

【讨论】:

    猜你喜欢
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 2017-03-06
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 2022-06-21
    相关资源
    最近更新 更多