最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SparkGraphx計(jì)算指定節(jié)點(diǎn)的N度關(guān)系節(jié)點(diǎn)源碼

 更新時(shí)間:2017年10月09日 09:39:32   作者:一人淺醉-  
這篇文章主要介紹了SparkGraphx計(jì)算指定節(jié)點(diǎn)的N度關(guān)系節(jié)點(diǎn)源碼,小編覺得挺不錯(cuò)的,這里分享給大家,希望給各位一個(gè)參考。

直接上代碼:

package horizon.graphx.util
import java.security.InvalidParameterException
import horizon.graphx.util.CollectionUtil.CollectionHelper
import org.apache.spark.graphx._
import org.apache.spark.rdd.RDD
import org.apache.spark.storage.StorageLevel
import scala.collection.mutable.ArrayBuffer
import scala.reflect.ClassTag
/**
 * Created by yepei.ye on 2017/1/19.
 * Description:用于在圖中為指定的節(jié)點(diǎn)計(jì)算這些節(jié)點(diǎn)的N度關(guān)系節(jié)點(diǎn),輸出這些節(jié)點(diǎn)與源節(jié)點(diǎn)的路徑長(zhǎng)度和節(jié)點(diǎn)id
 */
object GraphNdegUtil {
 val maxNDegVerticesCount = 10000
 val maxDegree = 1000
 /**
 * 計(jì)算節(jié)點(diǎn)的N度關(guān)系
 *
 * @param edges
 * @param choosedVertex
 * @param degree
 * @tparam ED
 * @return
 */
 def aggNdegreedVertices[ED: ClassTag](edges: RDD[(VertexId, VertexId)], choosedVertex: RDD[VertexId], degree: Int): VertexRDD[Map[Int, Set[VertexId]]] = {
 val simpleGraph = Graph.fromEdgeTuples(edges, 0, Option(PartitionStrategy.EdgePartition2D), StorageLevel.MEMORY_AND_DISK_SER, StorageLevel.MEMORY_AND_DISK_SER)
 aggNdegreedVertices(simpleGraph, choosedVertex, degree)
 }
 def aggNdegreedVerticesWithAttr[VD: ClassTag, ED: ClassTag](graph: Graph[VD, ED], choosedVertex: RDD[VertexId], degree: Int, sendFilter: (VD, VD) => Boolean = (_: VD, _: VD) => true): VertexRDD[Map[Int, Set[VD]]] = {
 val ndegs: VertexRDD[Map[Int, Set[VertexId]]] = aggNdegreedVertices(graph, choosedVertex, degree, sendFilter)
 val flated: RDD[Ver[VD]] = ndegs.flatMap(e => e._2.flatMap(t => t._2.map(s => Ver(e._1, s, t._1, null.asInstanceOf[VD])))).persist(StorageLevel.MEMORY_AND_DISK_SER)
 val matched: RDD[Ver[VD]] = flated.map(e => (e.id, e)).join(graph.vertices).map(e => e._2._1.copy(attr = e._2._2)).persist(StorageLevel.MEMORY_AND_DISK_SER)
 flated.unpersist(blocking = false)
 ndegs.unpersist(blocking = false)
 val grouped: RDD[(VertexId, Map[Int, Set[VD]])] = matched.map(e => (e.source, ArrayBuffer(e))).reduceByKey(_ ++= _).map(e => (e._1, e._2.map(t => (t.degree, Set(t.attr))).reduceByKey(_ ++ _).toMap))
 matched.unpersist(blocking = false)
 VertexRDD(grouped)
 }
 def aggNdegreedVertices[VD: ClassTag, ED: ClassTag](graph: Graph[VD, ED],
              choosedVertex: RDD[VertexId],
              degree: Int,
              sendFilter: (VD, VD) => Boolean = (_: VD, _: VD) => true
              ): VertexRDD[Map[Int, Set[VertexId]]] = {
 if (degree < 1) {
  throw new InvalidParameterException("度參數(shù)錯(cuò)誤:" + degree)
 }
 val initVertex = choosedVertex.map(e => (e, true)).persist(StorageLevel.MEMORY_AND_DISK_SER)
 var g: Graph[DegVertex[VD], Int] = graph.outerJoinVertices(graph.degrees)((_, old, deg) => (deg.getOrElse(0), old))
  .subgraph(vpred = (_, a) => a._1 <= maxDegree)
  //去掉大節(jié)點(diǎn)
  .outerJoinVertices(initVertex)((id, old, hasReceivedMsg) => {
  DegVertex(old._2, hasReceivedMsg.getOrElse(false), ArrayBuffer((id, 0))) //初始化要發(fā)消息的節(jié)點(diǎn)
 }).mapEdges(_ => 0).cache() //簡(jiǎn)化邊屬性
 choosedVertex.unpersist(blocking = false)
 var i = 0
 var prevG: Graph[DegVertex[VD], Int] = null
 var newVertexRdd: VertexRDD[ArrayBuffer[(VertexId, Int)]] = null
 while (i < degree + 1) {
  prevG = g
  //發(fā)第i+1輪消息
  newVertexRdd = prevG.aggregateMessages[ArrayBuffer[(VertexId, Int)]](sendMsg(_, sendFilter), (a, b) => reduceVertexIds(a ++ b)).persist(StorageLevel.MEMORY_AND_DISK_SER)
  g = g.outerJoinVertices(newVertexRdd)((vid, old, msg) => if (msg.isDefined) updateVertexByMsg(vid, old, msg.get) else old.copy(init = false)).cache()
  prevG.unpersistVertices(blocking = false)
  prevG.edges.unpersist(blocking = false)
  newVertexRdd.unpersist(blocking = false)
  i += 1
 }
 newVertexRdd.unpersist(blocking = false)
 val maped = g.vertices.join(initVertex).mapValues(e => sortResult(e._1)).persist(StorageLevel.MEMORY_AND_DISK_SER)
 initVertex.unpersist()
 g.unpersist(blocking = false)
 VertexRDD(maped)
 }
 private case class Ver[VD: ClassTag](source: VertexId, id: VertexId, degree: Int, attr: VD = null.asInstanceOf[VD])
 private def updateVertexByMsg[VD: ClassTag](vertexId: VertexId, oldAttr: DegVertex[VD], msg: ArrayBuffer[(VertexId, Int)]): DegVertex[VD] = {
 val addOne = msg.map(e => (e._1, e._2 + 1))
 val newMsg = reduceVertexIds(oldAttr.degVertices ++ addOne)
 oldAttr.copy(init = msg.nonEmpty, degVertices = newMsg)
 }
 private def sortResult[VD: ClassTag](degs: DegVertex[VD]): Map[Int, Set[VertexId]] = degs.degVertices.map(e => (e._2, Set(e._1))).reduceByKey(_ ++ _).toMap
 case class DegVertex[VD: ClassTag](var attr: VD, init: Boolean = false, degVertices: ArrayBuffer[(VertexId, Int)])
 case class VertexDegInfo[VD: ClassTag](var attr: VD, init: Boolean = false, degVertices: ArrayBuffer[(VertexId, Int)])
 private def sendMsg[VD: ClassTag](e: EdgeContext[DegVertex[VD], Int, ArrayBuffer[(VertexId, Int)]], sendFilter: (VD, VD) => Boolean): Unit = {
 try {
  val src = e.srcAttr
  val dst = e.dstAttr
  //只有dst是ready狀態(tài)才接收消息
  if (src.degVertices.size < maxNDegVerticesCount && (src.init || dst.init) && dst.degVertices.size < maxNDegVerticesCount && !isAttrSame(src, dst)) {
  if (sendFilter(src.attr, dst.attr)) {
   e.sendToDst(reduceVertexIds(src.degVertices))
  }
  if (sendFilter(dst.attr, dst.attr)) {
   e.sendToSrc(reduceVertexIds(dst.degVertices))
  }
  }
 } catch {
  case ex: Exception =>
  println(s"==========error found: exception:${ex.getMessage}," +
   s"edgeTriplet:(srcId:${e.srcId},srcAttr:(${e.srcAttr.attr},${e.srcAttr.init},${e.srcAttr.degVertices.size}))," +
   s"dstId:${e.dstId},dstAttr:(${e.dstAttr.attr},${e.dstAttr.init},${e.dstAttr.degVertices.size}),attr:${e.attr}")
  ex.printStackTrace()
  throw ex
 }
 }
 private def reduceVertexIds(ids: ArrayBuffer[(VertexId, Int)]): ArrayBuffer[(VertexId, Int)] = ArrayBuffer() ++= ids.reduceByKey(Math.min)
 private def isAttrSame[VD: ClassTag](a: DegVertex[VD], b: DegVertex[VD]): Boolean = a.init == b.init && allKeysAreSame(a.degVertices, b.degVertices)
 private def allKeysAreSame(a: ArrayBuffer[(VertexId, Int)], b: ArrayBuffer[(VertexId, Int)]): Boolean = {
 val aKeys = a.map(e => e._1).toSet
 val bKeys = b.map(e => e._1).toSet
 if (aKeys.size != bKeys.size || aKeys.isEmpty) return false
 aKeys.diff(bKeys).isEmpty && bKeys.diff(aKeys).isEmpty
 }
}

其中sortResult方法里對(duì)Traversable[(K,V)]類型的集合使用了reduceByKey方法,這個(gè)方法是自行封裝的,使用時(shí)需要導(dǎo)入,代碼如下:

/**
 * Created by yepei.ye on 2016/12/21.
 * Description:
 */
object CollectionUtil {
 /**
 * 對(duì)具有Traversable[(K, V)]類型的集合添加reduceByKey相關(guān)方法
 *
 * @param collection
 * @param kt
 * @param vt
 * @tparam K
 * @tparam V
 */
 implicit class CollectionHelper[K, V](collection: Traversable[(K, V)])(implicit kt: ClassTag[K], vt: ClassTag[V]) {
 def reduceByKey(f: (V, V) => V): Traversable[(K, V)] = collection.groupBy(_._1).map { case (_: K, values: Traversable[(K, V)]) => values.reduce((a, b) => (a._1, f(a._2, b._2))) }
 /**
  * reduceByKey的同時(shí),返回被reduce掉的元素的集合
  *
  * @param f
  * @return
  */
 def reduceByKeyWithReduced(f: (V, V) => V)(implicit kt: ClassTag[K], vt: ClassTag[V]): (Traversable[(K, V)], Traversable[(K, V)]) = {
  val reduced: ArrayBuffer[(K, V)] = ArrayBuffer()
  val newSeq = collection.groupBy(_._1).map {
  case (_: K, values: Traversable[(K, V)]) => values.reduce((a, b) => {
   val newValue: V = f(a._2, b._2)
   val reducedValue: V = if (newValue == a._2) b._2 else a._2
   val reducedPair: (K, V) = (a._1, reducedValue)
   reduced += reducedPair
   (a._1, newValue)
  })
  }
  (newSeq, reduced.toTraversable)
 }
 }
}

總結(jié)

以上就是本文關(guān)于SparkGraphx計(jì)算指定節(jié)點(diǎn)的N度關(guān)系節(jié)點(diǎn)源碼的全部?jī)?nèi)容了,希望對(duì)大家有所幫助。感興趣的朋友可以參閱:淺談七種常見的Hadoop和Spark項(xiàng)目案例  Spark的廣播變量和累加器使用方法代碼示例  Spark入門簡(jiǎn)介等,有什么問題請(qǐng)留言,小編會(huì)及時(shí)回復(fù)大家的。

相關(guān)文章

  • FileZilla Server搭建FTP服務(wù)器配置及425錯(cuò)誤與TLS警告解決方法詳解

    FileZilla Server搭建FTP服務(wù)器配置及425錯(cuò)誤與TLS警告解決方法詳解

    本文詳細(xì)講解了FileZilla Server搭建FTP服務(wù)器配置以及425 Can't open data,You appear to be behind a NAT router,FTP over TLS is not enabled等相關(guān)問題的解決方法
    2018-10-10
  • 手把手教你搭建騰訊云服務(wù)器入門(圖文教程)

    手把手教你搭建騰訊云服務(wù)器入門(圖文教程)

    這篇文章主要介紹了手把手教你搭建騰訊云服務(wù)器入門,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 10個(gè)常見的HTTP狀態(tài)碼詳解

    10個(gè)常見的HTTP狀態(tài)碼詳解

    HTTP狀態(tài)碼是用以表示網(wǎng)頁(yè)服務(wù)器HTTP響應(yīng)狀態(tài)的3位數(shù)字代碼,下面為大家介紹500內(nèi)部服務(wù)器錯(cuò)誤,404文件未找到,403禁止訪問 等常見的10個(gè)HTTP狀態(tài)碼
    2018-09-09
  • 常用的web服務(wù)器軟件整理(win+linux)

    常用的web服務(wù)器軟件整理(win+linux)

    這篇文章主要介紹了常用的web服務(wù)器軟件整理,包括windows與linux下的,需要的朋友可以參考下
    2017-12-12
  • 使用Npcap庫(kù)開發(fā)簡(jiǎn)單的掃描功能

    使用Npcap庫(kù)開發(fā)簡(jiǎn)單的掃描功能

    nmap(Network?Mapper)是一款開源免費(fèi)的針對(duì)大型網(wǎng)絡(luò)的端口掃描工具,nmap可以檢測(cè)目標(biāo)主機(jī)是否在線、主機(jī)端口開放情況、檢測(cè)主機(jī)運(yùn)行的服務(wù)類型及版本信息、檢測(cè)操作系統(tǒng)與設(shè)備類型等信息,本文主要介紹nmap工具安裝和基本使用方法,
    2024-08-08
  • 深入解析Apache?Hudi內(nèi)核文件標(biāo)記機(jī)制

    深入解析Apache?Hudi內(nèi)核文件標(biāo)記機(jī)制

    這篇文章主要為大家介紹了深入解析Apache?Hudi內(nèi)核文件標(biāo)記機(jī)制,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-03-03
  • 阿里云服務(wù)器Ubuntu?20.04上安裝Odoo?15的詳細(xì)過程

    阿里云服務(wù)器Ubuntu?20.04上安裝Odoo?15的詳細(xì)過程

    這篇文章主要介紹了在阿里云服務(wù)器Ubuntu?20.04上安裝Odoo?15的過程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • 一文讓你知道服務(wù)器是什么

    一文讓你知道服務(wù)器是什么

    服務(wù)器指的是網(wǎng)絡(luò)環(huán)境下能為其它客戶機(jī)(如PC機(jī)、智能手機(jī)、ATM等終端甚至是火車系統(tǒng)等大型設(shè)備)提供某種服務(wù)的專用計(jì)算機(jī),它比普通計(jì)算機(jī)運(yùn)行更快、負(fù)載更高、價(jià)格更貴,服務(wù)器具有高速的CPU運(yùn)算能力、長(zhǎng)時(shí)間的可靠運(yùn)行、強(qiáng)大的I/O外部數(shù)據(jù)吞吐能力以及更好的擴(kuò)展性
    2023-08-08
  • vscode 遠(yuǎn)程服務(wù)器 上傳至 github的操作步驟

    vscode 遠(yuǎn)程服務(wù)器 上傳至 github的操作步驟

    這篇文章主要介紹了vscode 遠(yuǎn)程服務(wù)器 上傳至 github的操作步驟,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-12-12
  • Windows下實(shí)現(xiàn)簡(jiǎn)單的libevent服務(wù)器

    Windows下實(shí)現(xiàn)簡(jiǎn)單的libevent服務(wù)器

    這篇文章主要介紹了Windows下實(shí)現(xiàn)簡(jiǎn)單的libevent服務(wù)器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10

最新評(píng)論

阿尔山市| 土默特左旗| 永登县| 青浦区| 上饶市| 当雄县| 宜丰县| 武冈市| 彩票| 南郑县| 吴桥县| 抚州市| 大宁县| 温宿县| 关岭| 收藏| 微山县| 全椒县| 利津县| 潍坊市| 中西区| 麦盖提县| 房产| 江源县| 兰州市| 河南省| 承德县| 辽阳市| 叶城县| 夹江县| 莱州市| 同德县| 新宁县| 吉林省| 青岛市| 桦甸市| 晋江市| 门源| 梅州市| 奉化市| 连山|