Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
spark
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cs525-sp18-g07
spark
Commits
06e4f2a8
Commit
06e4f2a8
authored
11 years ago
by
Matei Zaharia
Browse files
Options
Downloads
Plain Diff
Merge pull request #789 from MLnick/master
Adding Scala version of PageRank example
parents
71c63de2
c4eea875
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/src/main/scala/spark/examples/SparkPageRank.scala
+50
-0
50 additions, 0 deletions
examples/src/main/scala/spark/examples/SparkPageRank.scala
with
50 additions
and
0 deletions
examples/src/main/scala/spark/examples/SparkPageRank.scala
0 → 100644
+
50
−
0
View file @
06e4f2a8
package
spark.examples
import
spark.SparkContext._
import
spark.SparkContext
/**
* Computes the PageRank of URLs from an input file. Input file should
* be in format of:
* URL neighbor URL
* URL neighbor URL
* URL neighbor URL
* ...
* where URL and their neighbors are separated by space(s).
*/
object
SparkPageRank
{
def
main
(
args
:
Array
[
String
])
{
if
(
args
.
length
<
3
)
{
System
.
err
.
println
(
"Usage: PageRank <master> <file> <number_of_iterations>"
)
System
.
exit
(
1
)
}
var
iters
=
args
(
2
).
toInt
val
ctx
=
new
SparkContext
(
args
(
0
),
"PageRank"
,
System
.
getenv
(
"SPARK_HOME"
),
Seq
(
System
.
getenv
(
"SPARK_EXAMPLES_JAR"
)))
val
lines
=
ctx
.
textFile
(
args
(
1
),
1
)
val
links
=
lines
.
map
{
s
=>
val
parts
=
s
.
split
(
"\\s+"
)
(
parts
(
0
),
parts
(
1
))
}.
distinct
().
groupByKey
().
cache
()
var
ranks
=
links
.
mapValues
(
v
=>
1.0
)
for
(
i
<-
1
to
iters
)
{
val
contribs
=
links
.
join
(
ranks
).
values
.
flatMap
{
case
(
urls
,
rank
)
=>
val
size
=
urls
.
size
urls
.
map
(
url
=>
(
url
,
rank
/
size
))
}
ranks
=
contribs
.
groupByKey
().
mapValues
{
ranks
=>
val
sumRanks
=
ranks
.
foldLeft
(
0.0
)(
_
+
_
)
0.15
+
sumRanks
*
0.85
}
}
val
output
=
ranks
.
collect
()
output
.
foreach
(
tup
=>
println
(
tup
.
_1
+
" has rank: "
+
tup
.
_2
+
"."
))
ctx
.
stop
()
System
.
exit
(
0
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment