(小案例,有五个人他们参见相亲节目,这个五个人分别是0,1,2,3,4,号选手,计算出追随者年龄大于被追随者年龄的人数和平均年龄)
scala> import org.apache.spark._
import org.apache.spark._
scala> import org.apache.spark.rdd.RDD
import org.apache.spark.rdd.RDD
scala> import org.apache.spark.graphx._
import org.apache.spark.graphx._
scala> import org.apache.spark.graphx.util._
import org.apache.spark.graphx.util._
 
创建graph
scala> var graphs =
GraphGenerators.logNormalGraph(sc, numVertices = 5).mapVertices( (id, _) => id.toDouble )
graphs: org.apache.spark.graphx.Graph[Double,Int] = org.apache.spark.graphx.impl.GraphImpl@1461f52d
GraphGenerators.logNormalGraph://生成一个图的顶点的出度分布是日志正常。
numVertices:生成5个顶点,也就是五个人
查看生成的graph
vertices
VertexId, Double)] = Array((4,4.0), (0,0.0), (1,1.0), (3,3.0), (2,2.0))
srcid attr srcid:选手编号 attr:年龄
0 0
1 1
2 2
3 3
4 4
edges
Array(Edge(0,1,1), Edge(0,1,1), Edge(0,3,1), Edge(0,4,1), Edge(1,2,1), Edge(1,4,1), Edge(2,0,1), Edge(2,0,1), Edge(2,3,1), Edge(2,4,1), Edge(3,3,1), Edge(4,0,1), Edge(4,3,1))
srcid dstid attr srcid:追随者 dstid:被追随者 attr:年龄
0 1 1
0 1 1
0 3 1
0 4 1
1 2 1
1 4 1
2 0 1
2 0 1
2 3 1
2 4 1
1 3 1
4 0 1
4 3 1
获取srcid>dstid的数据(把追随者年龄大于被追随者年龄的人,发送给被追随者)
scala> val olderFollowers: VertexRDD[(Int, Double)] = graphs.aggregateMessages[(Int, Double)](
| triplet => { // Map Function
| if (triplet.srcAttr > triplet.dstAttr) {
| // Send message to destination vertex containing counter and age
| triplet.sendToDst(1, triplet.srcAttr)
| }
| },
| // Add counter and age
| (a, b) => (a._1 + b._1, a._2 + b._2) // Reduce Function
| )
获取graphs的triplets,过滤出来srcAttr>srcDst的数据,并将结果发送给DstId
 triplet.sendToDst(1, triplet.srcAttr):中的1是一个计数器,
查看数据
res3: Array[(org.apache.spark.graphx.VertexId, (Int, Double))] = Array((0,(3,8.0)), (3,(1,4.0)))
(0,(3,8.0):
0是srcId
3是srcId的入度
8.0是attr的和
计算出平均年龄
scala> val avgAgeOfOlderFollowers: VertexRDD[Double] =
| olderFollowers.mapValues( (id, value) =>
| value match { case (count, totalAge) => totalAge / count } )
avgAgeOfOlderFollowers: org.apache.spark.graphx.VertexRDD[Double] = VertexRDDImpl[48] at RDD at VertexRDD.scala:57
olderFollowers.mapValues:只对value进行操作
count是计数后的值
tatalAge:是attr的和

graph小案例的更多相关文章

  1. 机械表小案例之transform的应用

    这个小案例主要是对transform的应用. 时钟的3个表针分别是3个png图片,通过setInterval来让图片转动.时,分,秒的转动角度分别是30,6,6度. 首先,通过new Date函数获取 ...

  2. shell讲解-小案例

    shell讲解-小案例 一.文件拷贝输出检查 下面测试文件拷贝是否正常,如果cp命令并没有拷贝文件myfile到myfile.bak,则打印错误信息.注意错误信息中basename $0打印脚本名.如 ...

  3. [jQuery学习系列六]6-jQuery实际操作小案例

    前言最后在这里po上jQuery的几个小案例. Jquery例子1_占位符使用需求: 点击第一个按钮后 自动去check 后面是否有按钮没有选中, 如有则提示错误消息. <html> &l ...

  4. 02SpringMvc_springmvc快速入门小案例(XML版本)

    这篇文章中,我们要写一个入门案例,去整体了解整个SpringMVC. 先给出整个项目的结构图:

  5. React.js入门小案例

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...

  6. SqlDependency缓存数据库表小案例

    SqlDependency的简介: SqlDependency是outputcache网页缓存的一个参数,它的作用是指定缓存失效的数据库依赖项,可以具体到数据库和表. SqlDependency能解决 ...

  7. JavaScript apply函数小案例

    //回调函数1 function callback(a,b,c) { alert(a+b+c); } //回调函数2 function callback2(a,b) { alert(a+b); } / ...

  8. Session小案例------完成用户登录

    Session小案例------完成用户登录     在项目开发中,用户登陆功能再平常只是啦,当用户完毕username和password校验后.进入主界面,须要在主界面中显示用户的信息,此时用ses ...

  9. ch1-vuejs基础入门(hw v-bind v-if v-for v-on v-model 应用组件简介 小案例)

    1 hello world 引入vue.min.js 代码: ----2.0+版本 <div id="test"> {{str}} </div> <s ...

随机推荐

  1. (2)OLEDB数据库操作

    1.首先要引入 System.Data.OracleClient.dll 2.引入命名空间 using System.Data.OleDb; OleDb类  https://msdn.microsof ...

  2. 动态路由协议(2)--rip

    1.设置pc ip 网关 192.168.1.1 192.168.1.254 192.168.4.1 192.168.4.254 2.设置路由器 (1)设置接口ip Router(config-/ R ...

  3. 利用osql/ocmd批处理批量执行sql文件

    原文:利用osql/ocmd批处理批量执行sql文件 上周在测试环境建了几十张表,保存了.sql文件,准备在正式环境重建的时候懒得一个个打开建了,做一在网上搜寻了一下,果然有简单点的方法. 利用osq ...

  4. 玩转shell之符号篇

    转:http://hi.baidu.com/hellosimple/item/21b31dfefd23e811e2e3bd47 在shell中常用的特殊符号罗列如下: # ; ;; . , / \\ ...

  5. Uri详解之——Uri结构与代码提取

    目录(?)[+] 前言:依然没有前言…… 相关博客:1.<Uri详解之——Uri结构与代码提取>2.<Uri详解之二——通过自定义Uri外部启动APP与Notification启动& ...

  6. 遨游maxthon打开页面一片黑色,百度地图等黑屏解决办法

    遨游maxthon使用webkit极速核心,打开百度地图等页面一片黑色,黑屏了. 找了好久,不知道什么问题. 版本一样,都是4.4.xxx版本.另外一台机器又正常. 后来上傲游社区,好多人也有这个问题 ...

  7. 工具分享:GitHub的克隆工具Cl0neMast3r,轻松搞定各种测试

    GitHub,相信大家并不陌生,咱搞技术的应该都会用到它,GitHub主要是进行代码工具的存储.下载等工作.今天介绍一款让我们操作GitHub相关工作变的更简单的工具, GitHub的克隆工具. Cl ...

  8. 大量数据更新导致fgc频繁引起jvm服务暂停。

    线上跑的几台server突然出现大量fgc,因为在fgc过程的stop the world太久.引起其他应用訪问该server上的接口大量超时.(发生超时的时间点和fgc时间点一致) 先进行初步的优化 ...

  9. selenium执行报错:Process refused to die after 10 seconds, and couldn't taskkill it

    十二月 02, 2015 5:16:56 下午 org.openqa.selenium.os.ProcessUtils killWinProcess 警告: Process refused to di ...

  10. zabbix自定义监控项二

    为zabbix增加支持传参的自定义监控项 例如使用zabbix来监控tcp的12种状态 tcp的12种状态可以通过man netstat来找到,即 LISTEN:等待从任何远端TCP 和端口的连接请求 ...