首页 > 其他分享 >GraphFrames介绍和基本用法

GraphFrames介绍和基本用法

时间:2023-11-30 21:14:05浏览次数:31  
标签:show 36 介绍 用法 follow GraphFrames 29 friend e2

阅读本篇博客前需先了解图数据、scala、spark相关知识

 

GraphFrames是一款图处理类库。该类库构建在DataFrame之上,既能利用DataFrame良好的扩展性和强大的性能,同时也为Scala、Java和Python提供了统一的图处理API。

github:https://github.com/graphframes/graphframes
官方文档:https://graphframes.github.io/graphframes/docs/_site/user-guide.html#graphframe-to-graphx

一、对比graphX

 GraphFramesGraphX
数据模型 DataFrames RDD
开发语言 Scala/Java/Python Scala
使用场景 数据查询、图计算 图计算
顶点ID Any Type Long
点边属性 DataFrame columns Any Type(VD, ED)
返回类型 GraphFrame、DataFrame Graph[VD, ED] 、RDD[Long, VD]

二、scala下GraphFrames使用

//导入graphframes依赖
<dependency>   <groupId>graphframes</groupId>   <artifactId>graphframes</artifactId>   <version>0.8.1-spark2.4-s_2.11</version> </dependency>

三、官网案例实践

简单获取

import org.apache.spark.SparkConf
import org.apache.spark.sql.SparkSession
import org.graphframes.GraphFrame


object GraphFramesExample {


  def main(args: Array[String]): Unit = {
    val sparkConfig = new SparkConf().setAppName("GraphFrames").setMaster("local[2]")
      .set("spark.sql.shuffle.partitions", "1")//分区大小
    val spark: SparkSession = SparkSession.builder().config(sparkConfig).getOrCreate()
    // Vertex DataFrame
    val v = spark.createDataFrame(List(
      ("a", "Alice", 34),
      ("b", "Bob", 36),
      ("c", "Charlie", 30),
      ("d", "David", 29),
      ("e", "Esther", 32),
      ("f", "Fanny", 36),
      ("g", "Gabby", 60)
    )).toDF("id", "name", "age")
    // Edge DataFrame
    val e = spark.createDataFrame(List(
      ("a", "b", "friend"),
      ("b", "c", "follow"),
      ("c", "b", "follow"),
      ("f", "c", "follow"),
      ("e", "f", "follow"),
      ("e", "d", "friend"),
      ("d", "a", "friend"),
      ("a", "e", "friend")
    )).toDF("src", "dst", "relationship")
    // Create a GraphFrame
    val g = GraphFrame(v, e)
//    g.find("(a)-[e]->(b); (b)-[e2]->(a)").show()
//     g.find("(a)-[e]->(b); (b)-[e2]->(a)").show()
     g.find("(a)-[e]->(b); (b)-[e2]->(c); (c)-[e3]->(a)")
       .where("a.age > 29")
       .show()
    //获取图内所有点
    g.vertices.show()
    //获取图内所有边
    g.edges.show()
    //获取点的入度表
    g.inDegrees.show()
    //获取点的出度表
    g.outDegrees.show()
    //获取点的出入度表
    g.degrees.show()
    //获取图内所有三元组
    g.triplets.show()
  }
}

输出

//获取图内所有点
g.vertices.show()

+---+-------+---+
| id|   name|age|
+---+-------+---+
|  a|  Alice| 34|
|  b|    Bob| 36|
|  c|Charlie| 30|
|  d|  David| 29|
|  e| Esther| 32|
|  f|  Fanny| 36|
|  g|  Gabby| 60|
+---+-------+---+
//获取图内所有边
g.edges.show()
+---+---+------------+
|src|dst|relationship|
+---+---+------------+
|  a|  b|      friend|
|  b|  c|      follow|
|  c|  b|      follow|
|  f|  c|      follow|
|  e|  f|      follow|
|  e|  d|      friend|
|  d|  a|      friend|
|  a|  e|      friend|
+---+---+------------+
//获取点的入度表
g.inDegrees.show()
+---+--------+
| id|inDegree|
+---+--------+
|  b|       2|
|  c|       2|
|  f|       1|
|  d|       1|
|  a|       1|
|  e|       1|
+---+--------+
//获取点的出度表
g.outDegrees.show()
+---+---------+
| id|outDegree|
+---+---------+
|  a|        2|
|  b|        1|
|  c|        1|
|  f|        1|
|  e|        2|
|  d|        1|
+---+---------+
//获取点的出入度表
g.degrees.show()
+---+------+
| id|degree|
+---+------+
|  a|     3|
|  b|     3|
|  c|     3|
|  f|     2|
|  e|     3|
|  d|     2|
+---+------+
//获取图内所有三元组
g.triplets.show()
+----------------+--------------+----------------+
|             src|          edge|             dst|
+----------------+--------------+----------------+
|  [a, Alice, 34]|[a, b, friend]|    [b, Bob, 36]|
|    [b, Bob, 36]|[b, c, follow]|[c, Charlie, 30]|
|[c, Charlie, 30]|[c, b, follow]|    [b, Bob, 36]|
|  [f, Fanny, 36]|[f, c, follow]|[c, Charlie, 30]|
| [e, Esther, 32]|[e, f, follow]|  [f, Fanny, 36]|
| [e, Esther, 32]|[e, d, friend]|  [d, David, 29]|
|  [d, David, 29]|[d, a, friend]|  [a, Alice, 34]|
|  [a, Alice, 34]|[a, e, friend]| [e, Esther, 32]|
+----------------+--------------+----------------+

Motif finding(主题查找)

  GraphFrame主题查找使用特定语言(DSL)来表达结构查询。使用()表示点,[]表示边
例如,graph.find("(a)-[e]->(b); (b)-[e2]->(a)")将搜索由双向边缘连接的顶点a,b对。它将返回图形中所有此类结构DataFrame,
中包含主题中每个命名元素(顶点或边缘)的列。

+----------------+--------------+----------------+--------------+
|               a|             e|               b|            e2|
+----------------+--------------+----------------+--------------+
|    [b, Bob, 36]|[b, c, follow]|[c, Charlie, 30]|[c, b, follow]|
|[c, Charlie, 30]|[c, b, follow]|    [b, Bob, 36]|[b, c, follow]|
+----------------+--------------+----------------+--------------+

查找a点指向b点,同时存在b点指向a点的模式

g.find("(a)-[e]->(b); (b)-[e2]->(a)").show()

+----------------+--------------+----------------+--------------+
|               a|             e|               b|            e2|
+----------------+--------------+----------------+--------------+
|    [b, Bob, 36]|[b, c, follow]|[c, Charlie, 30]|[c, b, follow]|
|[c, Charlie, 30]|[c, b, follow]|    [b, Bob, 36]|[b, c, follow]|
+----------------+--------------+----------------+--------------+

查找a点指向b点,b点指向c点,c点指向a点,相当于一个有向的三点成环
g.find("(a)-[e]->(b); (b)-[e2]->(c); (c)-[e3]->(a)").show()

+---------------+--------------+---------------+--------------+---------------+--------------+
|              a|             e|              b|            e2|              c|            e3|
+---------------+--------------+---------------+--------------+---------------+--------------+
|[e, Esther, 32]|[e, d, friend]| [d, David, 29]|[d, a, friend]| [a, Alice, 34]|[a, e, friend]|
| [d, David, 29]|[d, a, friend]| [a, Alice, 34]|[a, e, friend]|[e, Esther, 32]|[e, d, friend]|
| [a, Alice, 34]|[a, e, friend]|[e, Esther, 32]|[e, d, friend]| [d, David, 29]|[d, a, friend]|
+---------------+--------------+---------------+--------------+---------------+--------------+

在以上基础之上添加条件过滤,此处使用where等同于filter
g.find("(a)-[e]->(b); (b)-[e2]->(c); (c)-[e3]->(a)")
.where("a.age > 29")
.show()

+---------------+--------------+---------------+--------------+--------------+--------------+
|              a|             e|              b|            e2|             c|            e3|
+---------------+--------------+---------------+--------------+--------------+--------------+
|[e, Esther, 32]|[e, d, friend]| [d, David, 29]|[d, a, friend]|[a, Alice, 34]|[a, e, friend]|
| [a, Alice, 34]|[a, e, friend]|[e, Esther, 32]|[e, d, friend]|[d, David, 29]|[d, a, friend]|
+---------------+--------------+---------------+--------------+--------------+--------------+

 

标签:show,36,介绍,用法,follow,GraphFrames,29,friend,e2
From: https://www.cnblogs.com/MuXinu/p/17839253.html

相关文章

  • Java常用库介绍
    ApacheCommons与Guava 参考资料:开源工具导航——ApacheCommons与Guava全览https://blog.csdn.net/qq_35946969/article/details/123418026Java开源工具库使用之Apachecommons-lang3https://blog.csdn.net/qq_23091073/article/details/126743040 ......
  • 三大存储协议介绍与存储资源盘活系统 AHCI、NVMe、SCSI
    存储协议目前主流的有三种,AHCI、NVMe、SCSI。HDD磁盘和早期SSD磁盘的传输协议一般采用AHCI(高级主机控制器接口,AdvancedHostControllerInterface)。AHCI为单队列模式,主机和HDD/SSD之间通过单队列进行数据交互。对于HDD这种慢速设备来说,主要瓶颈在存储设备,而非AHCI协议......
  • Redis持久化RDB与AOF介绍
    就是将内存中的数据通过rdb/aof进行持久化写入硬盘中rdb就是进行持久化的快照在指定的时间间隔内,执行数据集的时间点快照。这个快照文件称为(dump.rdb)RDB文件,RedisDataBaserdb的手动保存中都是用bgsave,不用save。在使用save时主程序会阻塞当前Redis服务器,执行期间不能处理其......
  • 数据库系列:MySQL InnoDB锁机制介绍
    数据库系列:MySQL慢查询分析和性能优化数据库系列:MySQL索引优化总结(综合版)数据库系列:高并发下的数据字段变更数据库系列:覆盖索引和规避回表数据库系列:数据库高可用及无损扩容数据库系列:使用高区分度索引列提升性能数据库系列:前缀索引和索引长度的取舍数据库系列:MySQL引擎My......
  • 统信有雀边缘计算介绍
    引言边缘计算介绍​ 边缘计算是使信息存储和计算能力更接近产生该信息的设备和使用它的用户的过程。传统上,应用程序将数据从传感器和智能手机等智能设备传输到中央数据中心进行处理。然而,前所未有的复杂性和数据规模已经超过了网络能力。通过将处理能力转移到更靠近用户和设备的......
  • Numpy-argsort()用法和Numpy-flipud()用法
    Numpy-argsort()用法语法:np.argsort(a,axis=-1,kind='quicksort',order=None)功能:对a进行由小到大排序,并输出其索引实例:importnumpyasnptest=np.array([8,2,-2,3,9,1])new_test=np.argsort(test)print('一维数组的排序结果:{}'.format(new_test))输出结......
  • systemctl和journalctl的用法
    systemctl使用方法 查看当前的服务启动systemctllist-units--type=service查看当前服务开机自启,同时过滤程序ConsumerLogsystemctllist-unit-files|grepConsumerLog服务启动、状态、停止、重启启动systemctlstart *****.service状态systemctlstatus *****.s......
  • C++随机数random库 介绍及应用
    一、摘要随机数可以应用在很多场景下如游戏抽卡、抽奖、场景生成、洗牌,歌曲app中的随机播放,社交app中的匹配等以及随机化算法。以下是针对C中随机函数rand、C++random库使用的总结,以及一些随机应用例子二、C/C++中的rand函数使用时需要引入头文件<stdlib.h>该函数返回一个......
  • 【Python入门教程】Python的shutil库介绍+基础函数使用(文件/目录复制、移动、删除、解
    ​前言        很多时候编过的代码过段时间就忘了,所以想用博文记录一下一些平时常用库的函数,今天跟大家分享一下python的shutil库的常用函数,包括文件复制、删除、移动等常见操作。同时为了复习之前python类的使用,所以今天的代码就用类封装起来了,大家直接看函数就行,不需......
  • C#中TimeSpan和DateTime的用法详解
    在C#编程中,TimeSpan和DateTime是常用的日期和时间处理类。它们提供了丰富的方法和属性,方便我们对日期和时间进行操作和格式化。本篇博客将详细介绍TimeSpan和DateTime的用法。TimeSpanTimeSpan类用于表示一段时间间隔,可以表示从几天到几个纳秒的时间。下面是TimeSpan类的常用属......