java - 转换 JavaRDD> 进入 Spark 数据集在爪哇
问题描述
在 Java(不是 Scala!)中,Spark 3.0.1 有一个JavaRDD实例对象neighborIdsRDD
,其类型为JavaRDD<Tuple2<Object, long[]>>
.
我与生成 JavaRDD 相关的部分代码如下:
GraphOps<String, String> graphOps = new GraphOps<>(graph, stringTag, stringTag);
JavaRDD<Tuple2<Object, long[]>> neighborIdsRDD = graphOps.collectNeighborIds(EdgeDirection.Either()).toJavaRDD();
我不得不使用 JavaRDD,toJavaRDD()
因为collectNeighborIds
它返回一个org.apache.spark.graphx.VertexRDD<long[]>
对象(VertexRDD doc)。
但是,在我的应用程序的其余部分中,我需要从对象Dataset<Row>
构建一个 Spark。collectNeighborIds
将JavaRDD<Tuple2<Object, long[]>>转换为Dataset<Row>的可能性和最佳方法是什么?
评论更新:
我根据评论调整了代码:
GraphOps<String, String> graphOps = new GraphOps<>(graph, stringTag, stringTag);
JavaRDD<Tuple2<Object, long[]>> neighborIdsRDD = graphOps.collectNeighborIds(EdgeDirection.Either()).toJavaRDD();
System.out.println("VertexRDD neighborIdsRDD is:");
for (int i = 0; i < neighborIdsRDD.collect().size(); i++) {
System.out.println(
((Tuple2<Object, long[]>) neighborIdsRDD.collect().get(i))._1() + " -- " +
Arrays.toString(((Tuple2<Object, long[]>) neighborIdsRDD.collect().get(i))._2())
);
}
Dataset<Row> dr = spark_session.createDataFrame(neighborIdsRDD.rdd(), Tuple2.class);
System.out.println("converted Dataset<Row> is:");
dr.show();
但我得到一个空数据集,如下所示:
VertexRDD neighborIdsRDD is:
4 -- [3]
1 -- [2, 3]
5 -- [3, 2]
2 -- [1, 3, 5]
3 -- [1, 2, 5, 4]
converted Dataset<Row> is:
++
||
++
||
||
||
||
||
++
解决方案
我遇到了同样的情况,但幸运的是我找到了找回Dataframe的解决方案。
解决方案代码在步骤[1]
和[2]
注释[3]
。
GraphOps<String, String> graphOps = new GraphOps<>(graph, stringTag, stringTag);
System.out.println("VertexRDD neighborIdsRDD is:");
JavaRDD<Tuple2<Object, long[]>> neighborIdsRDD = graphOps.collectNeighborIds(EdgeDirection.Either()).toJavaRDD();
for (int i = 0; i < neighborIdsRDD.collect().size(); i++) {
System.out.println(
((Tuple2<Object, long[]>) neighborIdsRDD.collect().get(i))._1() + " -- " +
Arrays.toString(((Tuple2<Object, long[]>) neighborIdsRDD.collect().get(i))._2())
);
}
// [1] Define encoding schema
StructType graphStruct = new StructType(new StructField[]{
new StructField("father", DataTypes.LongType, false, Metadata.empty()),
new StructField("children", DataTypes.createArrayType(DataTypes.LongType), false, Metadata.empty()),
});
// [2] Build a JavaRDD<Row> from a JavaRDD<Tuple2<Object,long[]>>
JavaRDD<Row> dr = neighborIdsRDD.map(tupla -> RowFactory.create(tupla._1(), tupla._2()));
// [3] Finally build the reqired Dataframe<Row>
Dataset<Row> dsr = spark_session.createDataFrame(dr.rdd(), graphStruct);
System.out.println("DATASET IS:");
dsr.show();
打印输出:
VertexRDD neighborIdsRDD is:
4 -- [3]
1 -- [2, 3]
5 -- [3, 2]
2 -- [1, 3, 5]
3 -- [1, 2, 5, 4]
DATASET IS:
+------+------------+
|father| children|
+------+------------+
| 4| [3]|
| 1| [2, 3]|
| 5| [3, 2]|
| 2| [1, 3, 5]|
| 3|[1, 2, 5, 4]|
+------+------------+