python - 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 距离。
它适用于一百行进行比较,但是对于数十万行,它需要很长时间,我真的需要让它更快。我认为我们可以并行化它,但我不知道该怎么做。
提前致谢 !
解决方案
推荐阅读
- java - MLKit 上的 Tensorflow Lite 给出此错误::#vk Got 1 class(es) for output index 0,根据标签映射预期为 2
- python - 如何创建 Turtle 克隆并将它们附加到列表中?
- unity3d - 灯到花箱的距离
- ios - 手势识别器正在影响其他功能
- swift - 使用 Mailgun 使用 Swift 发送电子邮件
- python - Python - 如何删除文本文件中的特定行?
- python - 散点图中数据大小的问题
- javascript - 如果 .textContent 包含 X 然后单击
- swift - 返回一个值并且在同一个函数中还有一个转义参数是否被认为是不好的做法?
- python - 如何使用 Python 和 JSON 为 API POST 查询设置参数