首页 > 解决方案 > 具有多种功能的 GraphX 边

问题描述

我正在使用 Graphx 并尝试向边缘添加功能。我有一个带有 Id1、Id2、重量、类型的 csv 文件

我能够获得 ID 和一个功能 - 重量或类型。有没有办法为一条边保存多个特征。这是我的代码片段:

val edgesWriterWriterCollaborated: RDD[Edge[String]] = sc.textFile(edgeWeightedWriterWriterCollaborated).map {
  line =>
    val row = line.split(",")
    Edge(row(0).toLong, row(1).toLong, row(2))
}

这给了我一个错误:

val edgesWriterWriterCollaborated: RDD[Edge[Tuple2]] = sc.textFile(edgeWeightedWriterWriterCollaborated).map {
  line =>
    val row = line.split(",")
    Edge(row(0).toLong, row(1).toLong, (row(2), row(3)))
}

更新:

我这样修复了我的代码:

    case class WriterWriterProperties(weight: String, edgeType: String)
 val edgesWriterWriterCollaborated: RDD[Edge[WriterWriterProperties]] = sc.textFile(edgeWeightedWriterWriterCollaborated).map {
  line =>
    val row = line.split(",")
    Edge(row(0).toLong, row(1).toLong, WriterWriterProperties(row(2), row(3)))
}

但是,当我尝试打印时:

   graph4.triplets.foreach(println)

我收到一个错误:Caused by: java.io.NotSerializableException

标签: apache-sparkspark-graphx

解决方案


当然。使用Tuple2

Edge(row(0).toLong, row(1).toLong, (row(2), row(3)))

或在您的情况下有意义的任何特定于域的对象:

case class FooBar(foo: String, bar: String)

Edge(row(0).toLong, row(1).toLong, FooBar(row(2), row(3)))

推荐阅读