gpt4 book ai didi

apache-spark - 如何在Spark中合并两个RDD?

转载 作者:行者123 更新时间:2023-12-01 22:57:23 26 4
gpt4 key购买 nike

我有 2 个 JavaRDD。第一个是

JavaRDD<CustomClass> data

第二个是

JavaRDD<Vector> features

我的自定义类有 2 个字段,(字符串)文本和(整数)标签。我的 JavaRDD 数据中有 1000 个 CustomClass 实例,JavaRDD 功能中有 1000 个 Vector 实例。

我通过使用 JavaRDD 数据并对其应用 map 函数来计算这 1000 个向量。

现在,我想要一个新的 JavaRDD 形式

JavaRDD<LabeledPoint>

由于 LabeledPoint 的构造函数需要一个标签和一个向量,因此我无法应用同时将 CustomClass 和 Vector 作为调用函数参数的映射函数,因为它只接受一个参数。

有人可以告诉我如何组合这两个 JavaRDD 并获得新的

JavaRDD<LabeledPoint> 

以下是我编写的代码的一些片段:

    Class CustomClass {
String text; int label;
}

JavaRDD<CustomClass> data = getDataFromFile(filename);

final HashingTF hashingTF = new HashingTF();
final IDF idf = new IDF();
final JavaRDD<Vector> td2 = data.map(
new Function<CustomClass, Vector>() {
@Override
public Vector call(CustomClass cd) throws Exception {
Vector v = new DenseVector(hashingTF.transform(Arrays.asList(cd.getText().split(" "))).toArray());
return v;
}
}
);

final JavaRDD<Vector> features = idf.fit(td2).transform(td2);

最佳答案

您可以使用JavaRDD#zip :

Zips this RDD with another one, returning key-value pairs with the first element in each RDD, second element in each RDD, etc. Assumes that the two RDDs have the same number of partitions and the same number of elements in each partition (e.g. one was made through a map on the other).

JavaPairRDD<CustomClass,Vector> dataAndFeatures = data.zip(features);
// TODO dataAndFeatures.map to LabeledPoint instances

文档中突出显示的部分成立,因为您通过数据的简单map创建了td2。然后 df (==features?) 是 transform on IDFModel instance 的结果,这也使值保持一致。

关于apache-spark - 如何在Spark中合并两个RDD?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39507972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com