import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;
import java.util.Arrays;
import java.util.List;

/**
* cartesian 算子:
* 相当于笛卡尔积计算,将两个RDD中的数据一一对应起来
*
*/
public class CartesianOperator {
public static void main(String[] args) {
SparkConf conf = new SparkConf().setMaster("local").setAppName("cartesian");
JavaSparkContext sc = new JavaSparkContext(conf);
List<String> names1 = Arrays.asList("w1","w2","w3","w4");
List<String> names2 = Arrays.asList("a1","a2","a3","a4");

JavaRDD<String> namesRdd1 = sc.parallelize(names1);
JavaRDD<String> namesRdd2 = sc.parallelize(names2);

namesRdd1.cartesian(namesRdd2).foreach(new VoidFunction<Tuple2<String, String>>() {
@Override
public void call(Tuple2<String, String> tuple) throws Exception {
System.err.println(tuple._1+":"+tuple._2);
}
});
}
}

微信扫描下图二维码加入博主知识星球,获取更多大数据、人工智能、算法等免费学习资料哦!
02-12 10:21