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
af645be5
Commit
af645be5
authored
11 years ago
by
Ankur Dave
Browse files
Options
Downloads
Patches
Plain Diff
Fix all code examples in guide
parent
2cd9358c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
docs/graphx-programming-guide.md
+23
-23
23 additions, 23 deletions
docs/graphx-programming-guide.md
graphx/data/users.txt
+7
-6
7 additions, 6 deletions
graphx/data/users.txt
with
30 additions
and
29 deletions
docs/graphx-programming-guide.md
+
23
−
23
View file @
af645be5
...
...
@@ -357,7 +357,7 @@ val relationships: RDD[Edge[String]] =
val defaultUser = ("John Doe", "Missing")
// Build the initial Graph
val graph = Graph(users, relationships, defaultUser)
// Notice that there is a user 0 (for which we have no information) connect
ing
users
// Notice that there is a user 0 (for which we have no information) connect
ed to
users
// 4 (peter) and 5 (franklin).
graph.triplets.map(
triplet => triplet.srcAttr._1 + " is the " + triplet.attr + " of " + triplet.dstAttr._1
...
...
@@ -858,11 +858,11 @@ val graph = GraphLoader.edgeListFile(sc, "graphx/data/followers.txt")
val ranks = graph.pageRank(0.0001).vertices
// Join the ranks with the usernames
val users = sc.textFile("graphx/data/users.txt").map { line =>
val fields = line.split("
\\
s+
")
val fields = line.split("
,
")
(fields(0).toLong, fields(1))
}
val ranksByUsername = users.
leftOuterJ
oin(ranks).map {
case (id, (username, rank
Opt
)) => (username, rank
Opt.getOrElse(0.0)
)
val ranksByUsername = users.
j
oin(ranks).map {
case (id, (username, rank)) => (username, rank)
}
// Print the result
println(ranksByUsername.collect().mkString("
\n
"))
...
...
@@ -881,11 +881,11 @@ val graph = GraphLoader.edgeListFile(sc, "graphx/data/followers.txt")
val cc = graph.connectedComponents().vertices
// Join the connected components with the usernames
val users = sc.textFile("graphx/data/users.txt").map { line =>
val fields = line.split("
\\
s+
")
val fields = line.split("
,
")
(fields(0).toLong, fields(1))
}
val ccByUsername = users.join(cc).map {
case (id, (username, cc)) =>
(username, cc)
val ccByUsername = users.join(cc).map {
case (id, (username, cc)) =>
(username, cc)
}
// Print the result
println(ccByUsername.collect().mkString("
\n
"))
...
...
@@ -900,12 +900,12 @@ A vertex is part of a triangle when it has two adjacent vertices with an edge be
{% highlight scala %}
// Load the edges in canonical order and partition the graph for triangle count
val graph = GraphLoader.edgeListFile(sc, "graphx/data/followers.txt", true).partitionBy(RandomVertexCut)
val graph = GraphLoader.edgeListFile(sc, "graphx/data/followers.txt", true).partitionBy(
PartitionStrategy.
RandomVertexCut)
// Find the triangle count for each vertex
val triCounts = graph.triangleCount().vertices
// Join the triangle counts with the usernames
val users = sc.textFile("graphx/data/users.txt").map { line =>
val fields = line.split("
\\
s+
")
val fields = line.split("
,
")
(fields(0).toLong, fields(1))
}
val triCountByUsername = users.join(triCounts).map { case (id, (username, tc)) =>
...
...
@@ -934,32 +934,32 @@ all of this in just a few lines with GraphX:
// Connect to the Spark cluster
val sc = new SparkContext("spark://master.amplab.org", "research")
// Load my user data and p
r
ase into tuples of user id and attribute list
val users = sc.textFile("
hdfs://user_attribute
s.t
sv
")
.map(line => line.split).map( parts => (parts.head, parts.tail) )
// Load my user data and pa
r
se into tuples of user id and attribute list
val users =
(
sc.textFile("
graphx/data/user
s.t
xt
")
.map(line => line.split
(",")
).map( parts => (parts.head
.toLong
, parts.tail) )
)
// Parse the edge data which is already in userId -> userId format
val followerGraph = Graph
.textFile(sc, "hdfs:/
/followers.t
sv
")
val followerGraph = Graph
Loader.edgeListFile(sc, "graphx/data
/followers.t
xt
")
// Attach the user attributes
val graph = followerGraph.outerJoinVertices(users){
val graph = followerGraph.outerJoinVertices(users)
{
case (uid, deg, Some(attrList)) => attrList
// Some users may not have attributes so we set them as empty
case (uid, deg, None) => Array.empty[String]
}
}
// Restrict the graph to users w
hich have exactly two attribut
es
val subgraph = graph.subgraph((vid, attr) => attr.size == 2)
// Restrict the graph to users w
ith usernames and nam
es
val subgraph = graph.subgraph(
vpred =
(vid, attr) => attr.size == 2)
// Compute the PageRank
val pagerankGraph =
Analytics
.page
r
ank(
subgraph
)
val pagerankGraph =
subgraph
.page
R
ank(
0.001
)
// Get the attributes of the top pagerank users
val userInfoWithPageRank = subgraph.outerJoinVertices(pagerankGraph.vertices){
case (uid, attrList, Some(pr)) => (pr, attrList)
case (uid, attrList, None) => (
pr
, attrList)
}
val userInfoWithPageRank = subgraph.outerJoinVertices(pagerankGraph.vertices)
{
case (uid, attrList, Some(pr)) => (pr, attrList
.toList
)
case (uid, attrList, None) => (
0.0
, attrList
.toList
)
}
println(userInfoWithPageRank.
top(5
))
println(userInfoWithPageRank.
vertices.top(5)(Ordering.by(_._2._1)).mkString("
\n
"
))
{% endhighlight %}
This diff is collapsed.
Click to expand it.
graphx/data/users.txt
+
7
−
6
View file @
af645be5
1 BarackObama
2 ladygaga
3 jeresig
4 justinbieber
6 matei_zaharia
7 odersky
1,BarackObama,Barack Obama
2,ladygaga,Goddess of Love
3,jeresig,John Resig
4,justinbieber,Justin Bieber
6,matei_zaharia,Matei Zaharia
7,odersky,Martin Odersky
8,anonsys
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