首页 > 解决方案 > 在 pyspark 中获取分桶随机投影结果的最佳实践

问题描述

目前,我已经建立了一个BucketedRandomProjectionLSH模型来计算数据的相似度approxNearestNeighbors。下面的代码是这样的。

df = sql_context.read.format("org.apache.spark.sql.cassandra").options(table="data", keyspace="spark").load()
brp = BucketedRandomProjectionLSH(inputCol="features", outputCol="hashes", bucketLength=2.0,
                                  numHashTables=3)
model = brp.fit(df)

df_collected = df.collect()

for x in df_collected:
    result = model.approxNearestNeighbors(df, x["features"], 30).collect()
    write(result)  ## save result to db

我在想是否有任何方法可以分配 for 循环的工作量

for x in df_collected:
    result = model.approxNearestNeighbors(df, x["features"], 30).collect()
    write(result)  ## save result to db

到每台机器

我发现 foreachforeachPartition可以完成工作,但我已经尝试过。他们不允许我在df那里通过 rdd。

有没有解决这类问题的最佳实践?

标签: apache-sparkpyspark

解决方案


推荐阅读