-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
PageRankExample.scala
130 lines (116 loc) · 5.13 KB
/
PageRankExample.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Tencent is pleased to support the open source community by making Angel available.
*
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/Apache-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
*/
package com.tencent.angel.spark.examples.cluster
import com.tencent.angel.spark.context.PSContext
import com.tencent.angel.spark.ml.core.ArgsUtil
import com.tencent.angel.graph.rank.pagerank.edgecut.{PageRank => EdgeCutPageRank}
import com.tencent.angel.graph.rank.pagerank.vertexcut.{PageRank => VertexCutPageRank}
import com.tencent.angel.graph.utils.{Delimiter, GraphIO}
import org.apache.spark.sql.DataFrame
import org.apache.spark.storage.StorageLevel
import org.apache.spark.{SparkConf, SparkContext}
object PageRankExample {
def main(args: Array[String]): Unit = {
val params = ArgsUtil.parse(args)
val mode = params.getOrElse("mode", "yarn-cluster")
val sc = start(mode)
val input = params.getOrElse("input", "")
val partitionNum = params.getOrElse("dataPartitionNum", "100").toInt
val storageLevel = StorageLevel.fromString(params.getOrElse("storageLevel", "MEMORY_ONLY"))
val output = params.getOrElse("output", "")
val psPartitionNum = params.getOrElse("psPartitionNum",
sc.getConf.get("spark.ps.instances", "10")).toInt
val tol = params.getOrElse("tol", "0.01").toFloat
val resetProp = params.getOrElse("resetProp", "0.15").toFloat
val isWeight = params.getOrElse("isWeight", "false").toBoolean
val srcIndex = params.getOrElse("srcIndex", "0").toInt
val dstIndex = params.getOrElse("dstIndex", "1").toInt
val weightIndex = params.getOrElse("weightIndex", "2").toInt
val useBalancePartition = params.getOrElse("useBalancePartition", "false").toBoolean
val balancePartitionPercent = params.getOrElse("balancePartitionPercent", "0.7").toFloat
val version = params.getOrElse("version", "edge-cut")
val numBatch = params.getOrElse("numBatch", "1").toInt
val batchSize = params.getOrElse("batchSize", "1000").toInt
val sep = params.getOrElse("sep", "space") match {
case "space" => " "
case "comma" => ","
case "tab" => "\t"
}
val edges = GraphIO.load(input, isWeighted = isWeight,
srcIndex = srcIndex, dstIndex = dstIndex,
weightIndex = weightIndex, sep = sep)
val ranks = version match {
case "edge-cut" => edgeCutPageRank(edges, partitionNum, psPartitionNum,
storageLevel, tol, resetProp, isWeight,
useBalancePartition, balancePartitionPercent, numBatch, batchSize)
case "vertex-cut" => vertexCutPageRank(edges, partitionNum, psPartitionNum,
storageLevel, tol, resetProp, isWeight,
useBalancePartition, balancePartitionPercent, numBatch, batchSize)
}
GraphIO.save(ranks, output)
stop()
}
def vertexCutPageRank(edges: DataFrame, partitionNum: Int,
psPartitionNum: Int,
storageLevel: StorageLevel,
tol: Float, resetProb: Float, isWeighted: Boolean,
useBalancePartition: Boolean,
balancePartitionPercent: Float, numBatch: Int, batchSize: Int): DataFrame = {
val pageRank = new VertexCutPageRank()
.setPartitionNum(partitionNum)
.setStorageLevel(storageLevel)
.setPSPartitionNum(psPartitionNum)
.setTol(tol)
.setResetProb(resetProb)
.setIsWeighted(isWeighted)
.setUseBalancePartition(useBalancePartition)
.setBalancePartitionPercent(balancePartitionPercent)
.setNumBatch(numBatch)
.setBatchSize(batchSize)
pageRank.transform(edges)
}
def edgeCutPageRank(edges: DataFrame, partitionNum: Int,
psPartitionNum: Int,
storageLevel: StorageLevel,
tol: Float, resetProb: Float, isWeighted: Boolean,
useBalancePartition: Boolean,
balancePartitionPercent: Float, numBatch: Int, batchSize: Int): DataFrame = {
val pageRank = new EdgeCutPageRank()
.setPartitionNum(partitionNum)
.setStorageLevel(storageLevel)
.setPSPartitionNum(psPartitionNum)
.setTol(tol)
.setResetProb(resetProb)
.setIsWeighted(isWeighted)
.setUseBalancePartition(useBalancePartition)
.setBalancePartitionPercent(balancePartitionPercent)
.setNumBatch(numBatch)
.setBatchSize(batchSize)
pageRank.transform(edges)
}
def start(mode: String): SparkContext = {
val conf = new SparkConf()
conf.setMaster(mode)
conf.setAppName("PageRank")
val sc = new SparkContext(conf)
sc
}
def stop(): Unit = {
PSContext.stop()
SparkContext.getOrCreate().stop()
}
}