首页 > 解决方案 > PySpark 中更高效的字符串匹配

问题描述

在我的数据库中,我有数十万家公司的 df,我必须在另一个包含所有现有公司的 df 中检索它们。

为此,我使用 PySpark :

def match_names(df_1, df_2):

    pipeline = Pipeline(stages=[
        RegexTokenizer(
            pattern="", inputCol="name", outputCol="tokens", minTokenLength=1
        ),
        NGram(n=3, inputCol="tokens", outputCol="ngrams"),
        HashingTF(inputCol="ngrams", outputCol="vectors"),
        MinHashLSH(inputCol="vectors", outputCol="lsh")
    ])

    model = pipeline.fit(df_1)

    stored_hashed = model.transform(df_1)
    landed_hashed = model.transform(df_2)
    landed_hashed = landed_hashed.withColumnRenamed('name', 'name2')

    matched_df = model.stages[-1].approxSimilarityJoin(stored_hashed, landed_hashed, 1, "confidence").select(
            col("datasetA.name"), col("datasetB.name2"), col("confidence"))

    return matched_df

然后我还计算每对的 Levenshtein 距离。

它适用于一百行进行比较,但是对于数十万行,它需要很长时间,我真的需要让它更快。我认为我们可以并行化它,但我不知道该怎么做。

提前致谢 !

标签: pythondataframepysparklevenshtein-distance

解决方案


推荐阅读