本文主要是介绍spark graphx 图结构 画图/可视化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
所需的包: maven包:
<!--<!– https://mvnrepository.com/artifact/org.graphstream/gs-core –>-->
<dependency><groupId>org.graphstream</groupId><artifactId>gs-core</artifactId><version>1.2</version>
</dependency><!--<!– https://mvnrepository.com/artifact/org.graphstream/gs-ui –>-->
<dependency><groupId>org.graphstream</groupId><artifactId>gs-ui</artifactId><version>1.2</version>
</dependency><!--<!– https://mvnrepository.com/artifact/org.scalanlp/breeze –>-->
<dependency><groupId>org.scalanlp</groupId><artifactId>breeze_2.11</artifactId><version>0.12</version>
</dependency><!--<!– https://mvnrepository.com/artifact/org.scalanlp/breeze-viz –>-->
<dependency><groupId>org.scalanlp</groupId><artifactId>breeze-viz_2.11</artifactId><version>0.12</version>
</dependency><!--<!– https://mvnrepository.com/artifact/org.jfree/jcommon –>-->
<dependency><groupId>org.jfree</groupId><artifactId>jcommon</artifactId><version>1.0.24</version>
</dependency><!-- https://mvnrepository.com/artifact/org.jfree/jfreechart -->
<dependency><groupId>org.jfree</groupId><artifactId>jfreechart</artifactId><version>1.0.19</version>
</dependency>
运行代码:
package net.qihoo.antispam.personal.graphximport org.apache.spark.graphx.{Edge, Graph, VertexId}
import org.apache.spark.{SparkConf, SparkContext}
import org.graphstream.graph.implementations.{AbstractEdge, SingleGraph, SingleNode}object playGraph{def main(args: Array[String]): Unit = {val sparkConf = new SparkConf().setAppName("GraphStreamDemo").set("spark.master", "local[*]")val sc = new SparkContext(sparkConf)val graph: SingleGraph = new SingleGraph("graphDemo")val vertices = sc.parallelize(List((1L, "ip"),(2L, "user"),(3L, "device")))val edges = sc.parallelize(List(Edge(1L, 2L, "1-2"),Edge(1L, 3L, "1-3"),Edge(2L, 3L, "2-4")))val srcGraph = Graph(vertices, edges)graph.setAttribute("ui.stylesheet", "url(file:XXX/mystylesheets)")graph.setAttribute("ui.quality")graph.setAttribute("ui.antialias")// load the graphx vertices into GraphStreamfor ((id, name) <- srcGraph.vertices.collect()){val node = graph.addNode(id.toString).asInstanceOf[SingleNode]node.addAttribute("ui.label",name)}// load the graphx edges into GraphStream edgesfor (Edge(x, y, info) <- srcGraph.edges.collect()){val edge = graph.addEdge(x.toString ++ y.toString, x.toString, y.toString, true).asInstanceOf[AbstractEdge]edge.addAttribute("ui.label",info)}graph.display()}
结果图:
节点指定颜色见后面的文档
这篇关于spark graphx 图结构 画图/可视化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!