apache-spark - 在 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
到每台机器
我发现 foreach
或foreachPartition
可以完成工作,但我已经尝试过。他们不允许我在df
那里通过 rdd。
有没有解决这类问题的最佳实践?
解决方案
推荐阅读
- r - 在K意味着使用R进行聚类后,检索最接近每个聚类质心的100个样本
- java - 如何让 ANTLR 消耗所有可见元素?
- php - Laravel 8 - 当工厂中的自引用关系时,该过程已用信号“11”发出信号
- android - Delphi FMX:DisposeOf 控件在 windows paltform 中有效,但在 Android 中无效。有什么问题?
- css - 如何隐藏 flexbox 中的文本,然后在 flexbox 项目悬停时使其显示?
- c - spin_lock_init 采用什么参数?
- python - 有没有一种方法或函数可以从程序中隐藏 selenium 的输出?
- regex - Hive 中的 input.regex
- python - 仅在 PC 启动 sqlite3.OperationalError 时出现 Python 错误:无法打开数据库文件
- c# - 有哪些 C# 约定用于组合父方法和子方法输出?