从hdfs上加载文件并创建graph

scala> var graphs = GraphLoader.edgeListFile(sc,"/tmp/dataTest/graphTest.txt")
graphs: org.apache.spark.graphx.Graph[Int,Int] = org.apache.spark.graphx.impl.GraphImpl@ab5670d

 可以看到只有一个task,也就是说,他的默认task数量默认就是1,我手动设置一下
scala> val graphs = GraphLoader.edgeListFile(sc, "/tmp/dataTest/graphTest.txt",numEdgePartitions=)
graphs: org.apache.spark.graphx.Graph[Int,Int] = org.apache.spark.graphx.impl.GraphImpl@409ea4d1

 这时显示就是4个task
 
查看前10个vertices和edge(vertices和edge的属性值默认会是1)
我来对vertices的值进行修改
scala> var verttmp = graphs.mapVertices((id,attr) => attr*)
verttmp: org.apache.spark.graphx.Graph[Int,Int] = org.apache.spark.graphx.impl.GraphImpl@25d7eb44
scala> verttmp.vertices.take()
// :: WARN executor.Executor: block locks were not released by TID = :
[rdd_37_0]
// :: WARN executor.Executor: block locks were not released by TID = :
[rdd_37_1]
res4: Array[(org.apache.spark.graphx.VertexId, Int)] = Array((,), (,), (,), (,), (,), (,), (,), (,), (,), (,))
也可以使用这个方式,这个方式更优化一些
scala> var verttmp = graphs.mapVertices((_,attr) => attr*)
verttmp: org.apache.spark.graphx.Graph[Int,Int] = org.apache.spark.graphx.impl.GraphImpl@76828ce4
修改edge的属性值
scala> var edgetmp=graphs.mapEdges(e => e.attr*)
edgetmp: org.apache.spark.graphx.Graph[Int,Int] = org.apache.spark.graphx.impl.GraphImpl@42ce3be7
scala> edgetmp.edges.take()
// :: WARN executor.Executor: block locks were not released by TID = :
[rdd_26_0]
// :: WARN executor.Executor: block locks were not released by TID = :
[rdd_26_1]
res6: Array[org.apache.spark.graphx.Edge[Int]] = Array(Edge(,,), Edge(,,), Edge(,,), Edge(,,), Edge(,,), Edge(,,), Edge(,,), Edge(,,), Edge(,,), Edge(,,))
修改triplets的属性值(要求是:将srcAttr修改为以前的2倍,dstAttr修改为以前的3倍)
scala> var triptmp = graphs.mapTriplets(t => t.srcAttr* + t.dstAttr*)
triptmp: org.apache.spark.graphx.Graph[Int,Int] = org.apache.spark.graphx.impl.GraphImpl@318ec664
scala> triptmp.triplets.take()
[Stage :> ( + ) / ]// :: WARN executor.Executor: block locks were not released by TID = :
[rdd_26_0]
// :: WARN executor.Executor: block locks were not released by TID = :
[rdd_26_1]
res7: Array[org.apache.spark.graphx.EdgeTriplet[Int,Int]] = Array(((,),(,),), ((,),(,),), ((,),(,),), ((,),(,),), ((,),(,),), ((,),(,),), ((,),(,),), ((,),(,),), ((,),(,),), ((,),(,),))
 
structural operators的操作有以下几种函数
class Graph[VD, ED] {
def reverse: Graph[VD, ED]
def subgraph(epred: EdgeTriplet[VD,ED] => Boolean,
vpred: (VertexId, VD) => Boolean): Graph[VD, ED]
def mask[VD2, ED2](other: Graph[VD2, ED2]): Graph[VD, ED]
def groupEdges(merge: (ED, ED) => ED): Graph[VD,ED]
}
subgraph操作
def subgraph(epred: EdgeTriplet[VD,ED] => Boolean,
vpred: (VertexId, VD) => Boolean): Graph[VD, ED]
//改函数返回的graph是满足一个boolean条件的graph
//vd就是verticesRdd,包含vertexId和attr vpred:(vertexId,(vertexId,attr))
subgraph大数多应用场景:限制图的顶点和边,消除失效的链接
scala> var subg = graphs.subgraph(epred = e =>e.srcId>e.dstId)
subg: org.apache.spark.graphx.Graph[Int,Int] = org.apache.spark.graphx.impl.GraphImpl@51483f93
查看结果
scala> subg.edges.take()
res12: Array[org.apache.spark.graphx.Edge[Int]] = Array(
Edge(,,),
Edge(,,),
Edge(,,),
Edge(,,),
Edge(,,),
Edge(,,),
Edge(,,),
Edge(,,),
Edge(,,),
Edge(,,))
查看subgraph的vertices和edge
scala> subg.vertices.count
res11: Long =
scala> subg.edges.count
res13: Long =
查看原来的graphs的vertices和edge
scala> graphs.vertices.count
res9: Long =
scala> graphs.edges.count
res10: Long =
 
Degrees 有(indegrees,outdegrees,Degrees)
 
indegrees:就是srcID到dstId的度数 ,自我理解就是条数
scala> graphs.inDegrees
res15: Array[(org.apache.spark.graphx.VertexId, Int)] = Array((,),
(,), (,), (,), (,), (,),
(,))
outdegrees:就是dstId到srcId的度数
scala> graphs.outDegrees.collect
[Stage :>( + ) / ]// :: WARN executor.Executor:
res18: Array[(org.apache.spark.graphx.VertexId, Int)] = Array((,), (,),
(,), (,), (,), (,), (,),
(,), (,), (,), (,), (,))
degrees:总度数
 
查出最大的出度,入度,总度数
创建函数
scala> def max(a:(VertexId,Int),b:(VertexId,Int))={if(a._2>b._2) a else b }
max: (a: (org.apache.spark.graphx.VertexId, Int), b: (org.apache.spark.graphx.VertexId, Int))
(org.apache.spark.graphx.VertexId, Int)
inDdgrees
scala> graphs.inDegrees.reduce(max)
// :: WARN executor.Executor: block locks were not released by TID = :
[rdd_14_0]
res35: (org.apache.spark.graphx.VertexId, Int) = (,) scala> graphs.outDegrees.reduce(max)
// :: WARN executor.Executor: block locks were not released by TID = :
[rdd_14_0]
res36: (org.apache.spark.graphx.VertexId, Int) = (,) scala> graphs.degrees.reduce(max)
// :: WARN executor.Executor: block locks were not released by TID = :
[rdd_14_0]
res38: (org.apache.spark.graphx.VertexId, Int) = (,)
joinVertices:将各个顶点改为他的入度 
outerJoinVertices:将各个顶点改为他的出度
将graphs中所有的vertexId的属性都设置为0
scala> var rawG=graphs.mapVertices((id,attr) => )
rawG: org.apache.spark.graphx.Graph[Int,String] = org.apache.spark.graphx.impl.GraphImpl@43d06473
查看结果
scala> rawG.vertices.collect
res47: Array[(org.apache.spark.graphx.VertexId, Int)] = Array((,), (,), (,), (,))
获取rwaG的inDegrees数据集
scala> var ind=rawG.inDegrees;
ind: org.apache.spark.graphx.VertexRDD[Int] = VertexRDDImpl[] at RDD at VertexRDD.scala:
查看结果
scala> ind.collect
// :: WARN executor.Executor: block locks were not released by TID = :
[rdd_60_0]
res49: Array[(org.apache.spark.graphx.VertexId, Int)] = Array((,), (,), (,))
使用joinVertices
scala> var temp=rawG.joinVertices[Int](ind)((_,_,optdeg) => optdeg)
temp: org.apache.spark.graphx.Graph[Int,String] = org.apache.spark.graphx.impl.GraphImpl@af0e7ce
查看结果
scala> temp.vertices.take();
// :: WARN executor.Executor: block locks were not released by TID = :
[rdd_60_0, rdd_77_0]
res51: Array[(org.apache.spark.graphx.VertexId, Int)] = Array((,), (,), (,), (,))
joinVertices从字面上看就是把两个数据集根据vertexId合并,集合的属性用右边的vertices,最后一个属性是0,是因为主的数据集没有vertexId与辅的对应,
 
outerJoinVertices
 
 
aggregateMessages

在集群中使用文件加载graph的更多相关文章

  1. 虹软人脸识别在 linux中so文件加载不到的问题

    其实是可以加载到的,不过是so文件放的位置不一对,最简单的方式是放在 /usr/lib64 目录下,也可自己设置. so文件加载不到会报这个错误:    .lang.UnsatisfiedLinkEr ...

  2. 读书笔记(一)—— 浅析浏览器渲染过程和html中的文件加载

    在构建页面时,我们会在html中载入一个或多个css和js文件.或许大家都已经习惯了"最佳实践"中,css文件应该放在<head>标签中引入,而js文件则是放在< ...

  3. ssm中静态文件加载路径

    项目在本地软件和在服务器上的项目路径如果写死,有可能会出现项目在本机上可以访问,架设在服务器上后就不能访问 这儿介绍在ssm框架中使用 @WebServlet(urlPatterns = {},loa ...

  4. js中xml文件加载

  5. 前端设计中关于外部js文件加载的速度优化

    在一般情况下,许多人都是将<script>写在了<head>标签中,而许多浏览器都是使用单一的线程来加载js文件的,从上往下,从左往右. 若是加载过程出错,那么网页就会阻塞,就 ...

  6. redis/分布式文件存储系统/数据库 存储session,解决负载均衡集群中session不一致问题

    先来说下session和cookie的异同 session和cookie不仅仅是一个存放在服务器端,一个存放在客户端那么笼统 session虽然存放在服务器端,但是也需要和客户端相互匹配,试想一个浏览 ...

  7. 在seajs中使用require加载静态文件的问题

    注意,在seajs中使用require加载静态文件时,必须使用常量,不能用变量.如果一定要用变量,请使用require.async var html = require("view/sys/ ...

  8. html文件在head标签中引入js地址和直接写js代码,所用时间是不同的,因为引入js地址,文件加载的时候需要通过通讯协议去解析地址,读取外部文件

    html文件在head标签中引入js地址和直接写js代码,所用时间是不同的,因为引入js地址,文件加载的时候需要通过通讯协议去解析地址,读取外部文件

  9. Java中的资源文件加载方式

    文件加载方式有两种: 使用文件系统自带的路径机制,一个应用程序只能有一个当前目录,但可以有Path变量来访问多个目录 使用ClassPath路径机制,类路径跟Path全局变量一样也是有多个值 在Jav ...

随机推荐

  1. Codeforces 825F - String Compression

    825F - String Compression 题意 给出一个字符串,你要把它尽量压缩成一个短的字符串,比如一个字符串ababab你可以转化成3ab,长度为 3,比如bbbacacb转化成3b2a ...

  2. docker环境准备及理论

    1.预热 内核运行在内核空间,进程运行在用户空间,linux进程特性:父进程负责子进程的创建和回收,白发人送黑发人.容器就是为了保护它里面的内容物,不受其他容器干扰,也不去干扰其他容器.容器让进程认为 ...

  3. JDK内置性能监测工具使用

    Java自带的性能监测工具用法简介——jstack.jconsole.jinfo.jmap.jdb.jsta.jvisualvmJDK内置工具使用 一.javah命令(C Header and Stu ...

  4. java File和Byte[]数组 相互转换

    public class Test { public static void main(String[] args){ String filePath = "E:\\softoon\\wor ...

  5. Python学习笔记——基本数据结构

    列表list List是python的一个内置动态数组对象,它的基本使用方式如下: shoplist = ['apple', 'mango', 'carrot', 'banana'] print 'I ...

  6. 用CSS3产生动画效果

    相关属性: @keyframes规则:定义动画 语法:@keyframes animationname{keyframes-selector {CSS-style;}} animationname:动 ...

  7. Thread.Join(int millisecondsTimeout)

    Join 就是加入的意思,也就是说新创建的线程加入到进程中,并马上执行. 看下面这段代码 Console.WriteLine("start"); Thread myTask = n ...

  8. 在ubuntu12.04中安装wine和source insight

    1.安装wine sudo apt-get install wine 2.安装source insight 将source insight安装的可运行文件拷贝到ubuntu中.我拷贝到了~/Deskt ...

  9. web-inf文件夹的使用

    web-inf文件夹在正常情况下系统是不允许外界直接访问的,访问里面的文件就会出现404错误,但是系统可以自身进行访问,所有要使系统去访问的话就需要使用拦截控制器去接受外界命令,由控制器来转发访问请求 ...

  10. Litepal 数据库操作框架的使用 (火)

    LitePal是GitHub上一款开源的Android数据库框架. 它採用了对象关系映射(ORM)的模式,将平时开发时最经常使用的一些数据库功能进行了封装.使得开发人员不用编写一行SQL语句就能够完毕 ...