GraphX图计算图处理知识图谱简单可视化核心技术

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wzwdcld/article/details/51585093

  准备在CSDN上写一个关于GraphX的专栏,这是第一篇文章。
  本文介绍使用GraphX创建一张图并可视化的关键技术,创建好的图存储在Graph[VD,ED]对象中,可视化所使用的技术框架是第三方Java动态图形管理组件GraphStream

可视化效果

  在可视化结果中,使用标签分别在顶点和边的旁边标注了人物的姓名和人物之间的关系,如下图所示。
这里写图片描述

编程语言

  GraphX目前只支持Scala编程语言。

核心代码

import org.apache.spark.{SparkContext, SparkConf}
import org.apache.spark.graphx._
import org.apache.spark.rdd.RDD
import org.graphstream.graph.{Graph => GraphStream}
import org.graphstream.graph.implementations._

/**
  * 显示原始图可视化对象
  */
object GraphStreamTest extends App{
  /**
    * configuration of spark
    * Spark配置部分
    */
  val conf = new SparkConf().setAppName("GraphStreamTest")
  conf.setMaster("spark://MacdeMacBook-Pro-3.local:7077")
  val sc = new SparkContext(conf)
  sc.addJar("/Users/mac/Documents/GraphXSurvey/SparkTest/out/artifacts/SparkTest_jar3/SparkTest.jar")

  case class Person(name:String, sex:String)
  case class Link(relationship:String, happenDate:String)
  /**
    * create a graph from files which have specified form
    *
    * @param vertexFilePath file path of vertexs.csv
    * @param edgeFilePath file path of edges.csv
    * @return
    */
  def createGraph(vertexFilePath:String, edgeFilePath:String): Graph[Person,Link] ={
    //读取数据文件
    val vertices = sc.textFile(vertexFilePath)
    val links= sc.textFile(edgeFilePath)
    //构建边、顶点RDD
    val verticesRDD: RDD[(VertexId,Person)] = vertices map {line
    =>
      val row = line split ','
      (row(0).toLong,Person(row(1),row(2)))
    }

    val linksRDD:RDD[Edge[Link]] = links map {line =>
      val row = line split ','
      Edge(row(0).toLong, row(1).toLong, Link(row(2), row(3)))
    }
    //构建图
    val social: Graph[Person,Link] = Graph(verticesRDD, linksRDD)
    return social

  }
  /**
    * the main graph
    */
  var graph:Graph[Person,Link] = createGraph("/Users/mac/Documents/GraphXSurvey/GraphX/SocialNetwork/vertexs.csv","/Users/mac/Documents/GraphXSurvey/GraphX/SocialNetwork/edges.csv")
  graph.cache()



  //创建原始可视化对象
  val graphStream:SingleGraph = new SingleGraph("GraphStream")

  // 设置graphStream全局属性. Set up the visual attributes for graph visualization
  graphStream.addAttribute("ui.stylesheet","url(./style/stylesheet.css)")
  graphStream.addAttribute("ui.quality")
  graphStream.addAttribute("ui.antialias")


  // 加载顶点到可视化图对象中
  for ((id,person:Person) <- graph.vertices.collect()) {
    val node = graphStream.addNode(id.toString).asInstanceOf[SingleNode]
    node.addAttribute("ui.label",id  +"\n"+person.name)
  }
  //加载边到可视化图对象中
  for (Edge(x,y,link:Link) <- graph.edges.collect()) {
    val edge = graphStream.addEdge(x.toString ++ y.toString,
      x.toString, y.toString,
      true).
      asInstanceOf[AbstractEdge]

  }
  //显示
  graphStream.display()

}
展开阅读全文

没有更多推荐了,返回首页