本文整理汇总了Java中org.apache.spark.api.java.JavaRDD.cartesian方法的典型用法代码示例。如果您正苦于以下问题:Java JavaRDD.cartesian方法的具体用法?Java JavaRDD.cartesian怎么用?Java JavaRDD.cartesian使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.spark.api.java.JavaRDD
的用法示例。
在下文中一共展示了JavaRDD.cartesian方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calculateSimilarityFromVector
import org.apache.spark.api.java.JavaRDD; //导入方法依赖的package包/类
/**
* Calculate term similarity from vector.
*
* @param importRDD the {@link org.apache.spark.api.java.JavaPairRDD}
* data structure containing the vectors.
* @param simType the similarity calculation to execute e.g.
* <ul>
* <li>{@link org.apache.sdap.mudrod.utils.SimilarityUtil#SIM_COSINE} - 3,</li>
* <li>{@link org.apache.sdap.mudrod.utils.SimilarityUtil#SIM_HELLINGER} - 2,</li>
* <li>{@link org.apache.sdap.mudrod.utils.SimilarityUtil#SIM_PEARSON} - 1</li>
* </ul>
* @return a new {@link org.apache.spark.api.java.JavaPairRDD}
*/
public static JavaRDD<LinkageTriple> calculateSimilarityFromVector(JavaPairRDD<String, Vector> importRDD, int simType) {
JavaRDD<Tuple2<String, Vector>> importRDD1 = importRDD.map(f -> new Tuple2<String, Vector>(f._1, f._2));
JavaPairRDD<Tuple2<String, Vector>, Tuple2<String, Vector>> cartesianRDD = importRDD1.cartesian(importRDD1);
return cartesianRDD.map(new Function<Tuple2<Tuple2<String, Vector>, Tuple2<String, Vector>>, LinkageTriple>() {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public LinkageTriple call(Tuple2<Tuple2<String, Vector>, Tuple2<String, Vector>> arg) {
String keyA = arg._1._1;
String keyB = arg._2._1;
if (keyA.equals(keyB)) {
return null;
}
Vector vecA = arg._1._2;
Vector vecB = arg._2._2;
Double weight = 0.0;
if (simType == SimilarityUtil.SIM_PEARSON) {
weight = SimilarityUtil.pearsonDistance(vecA, vecB);
} else if (simType == SimilarityUtil.SIM_HELLINGER) {
weight = SimilarityUtil.hellingerDistance(vecA, vecB);
}
LinkageTriple triple = new LinkageTriple();
triple.keyA = keyA;
triple.keyB = keyB;
triple.weight = weight;
return triple;
}
}).filter(new Function<LinkageTriple, Boolean>() {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public Boolean call(LinkageTriple arg0) throws Exception {
if (arg0 == null) {
return false;
}
return true;
}
});
}