-
Notifications
You must be signed in to change notification settings - Fork 705
Graph API for Scalding #1583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
richwhitjr
wants to merge
4
commits into
twitter:develop
Choose a base branch
from
richwhitjr:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Graph API for Scalding #1583
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
scalding-core/src/main/scala/com/twitter/scalding/graph/Edge.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /* | ||
| Copyright 2012 Twitter, Inc. | ||
|
|
||
| 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 | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-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.twitter.scalding.graph | ||
|
|
||
| case class Edge[T, S](source: T, dest: T, attr: S) |
18 changes: 18 additions & 0 deletions
18
scalding-core/src/main/scala/com/twitter/scalding/graph/EdgeTriplet.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /* | ||
| Copyright 2012 Twitter, Inc. | ||
|
|
||
| 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 | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-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.twitter.scalding.graph | ||
|
|
||
| case class EdgeTriplet[T, S, Q](source: Vertex[T, Q], dest: Vertex[T, Q], edge: Edge[T, S]) |
242 changes: 242 additions & 0 deletions
242
scalding-core/src/main/scala/com/twitter/scalding/graph/Graph.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| /* | ||
| Copyright 2012 Twitter, Inc. | ||
|
|
||
| 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 | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-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.twitter.scalding.graph | ||
|
|
||
| import com.twitter.scalding.TypedPipe | ||
|
|
||
| import scala.reflect.ClassTag | ||
|
|
||
| /** | ||
| * General Graph Object that works on both Vertices and Edges. | ||
| * Graph supports extra data on both edges and vertices. | ||
| * | ||
| * @param inputEdges Directed Edges that make up the graph. | ||
| * @param inputVertices Vertices of the graph. | ||
| */ | ||
| class Graph[T: Ordering, S, Q](inputEdges: TypedPipe[Edge[T, S]], inputVertices: TypedPipe[Vertex[T, Q]]) { | ||
| def edges: TypedPipe[Edge[T, S]] = inputEdges | ||
| def vertices: TypedPipe[Vertex[T, Q]] = inputVertices | ||
|
|
||
| /** | ||
| * Returns a TypedPipe of edges with joined Vertex attributes. | ||
| */ | ||
| def triplets: TypedPipe[EdgeTriplet[T, S, Q]] = | ||
| edges | ||
| .groupBy(_.source) | ||
| .join(vertices.groupBy(_.id)) | ||
| .toTypedPipe | ||
| .map{ case (source, (edge, packet)) => (edge.dest, (edge, packet)) } | ||
| .join(vertices.groupBy(_.id)) | ||
| .values | ||
| .map{ case ((edge, sourceVertex), destVertex) => EdgeTriplet(sourceVertex, destVertex, edge) } | ||
|
|
||
| /** | ||
| * Left join vertices with the graph vertices and generate a new Graph | ||
| */ | ||
| def leftJoinVertices[U, VD2](other: TypedPipe[Vertex[T, U]])(mapFunc: (T, Q, Option[U]) => VD2): Graph[T, S, VD2] = { | ||
| val newVertices = vertices | ||
| .groupBy(_.id) | ||
| .leftJoin(other.groupBy(_.id)) | ||
| .toTypedPipe | ||
| .map{ case (id, (vertex, data)) => Vertex(id, mapFunc(id, vertex.attr, data.map(_.attr))) } | ||
|
|
||
| new GraphUnfilteredEdges(edges, newVertices) | ||
| } | ||
|
|
||
| /** | ||
| * Inner join vertices with the graph vertices and generate a new Graph | ||
| */ | ||
| def joinVertices[U, VD2](other: TypedPipe[Vertex[T, U]])(mapFunc: (T, Q, U) => VD2): Graph[T, S, VD2] = { | ||
| val newVertices = vertices | ||
| .groupBy(_.id) | ||
| .join(other.groupBy(_.id)) | ||
| .toTypedPipe | ||
| .map{ case (id, (vertex, data)) => Vertex(id, mapFunc(id, vertex.attr, data.attr)) } | ||
|
|
||
| new Graph[T, S, VD2](edges, newVertices) | ||
| } | ||
|
|
||
| /** | ||
| * Filtered the vertices of the graph. Defer the edge filtering until necessary. | ||
| */ | ||
| def filterVertices(filter: Vertex[T, Q] => Boolean) = | ||
| new GraphUnfilteredEdges[T, S, Q](edges, vertices.filter(filter)) | ||
|
|
||
| /** | ||
| * Filtered the edges of the graph. Defer the vertex filtering until necessary. | ||
| */ | ||
| def filterEdges(filter: Edge[T, S] => Boolean) = | ||
| new GraphUnfilteredVertices[T, S, Q](edges.filter(filter), vertices) | ||
|
|
||
| def mapVertices[A](map: Vertex[T, Q] => A): Graph[T, S, A] = | ||
| new Graph[T, S, A](edges, vertices.map{ vertex => Vertex(vertex.id, map(vertex)) }) | ||
|
|
||
| def mapEdges[A](map: Edge[T, S] => A): Graph[T, A, Q] = | ||
| new Graph[T, A, Q](edges.map{ edge => edge.copy(attr = map(edge)) }, vertices) | ||
|
|
||
| def mapTriplets[A, B](map: EdgeTriplet[T, S, Q] => EdgeTriplet[T, A, B]): Graph[T, A, B] = { | ||
| val newTriplets = triplets.map(map) | ||
|
|
||
| new Graph[T, A, B]( | ||
| newTriplets.map(_.edge), | ||
| newTriplets.flatMap(trip => List(trip.source, trip.dest)).distinct(Ordering.by(_.id))) | ||
| } | ||
|
|
||
| /** | ||
| * For all vertices collect their neighbors storing only ids. | ||
| */ | ||
| def collectNeighborIds(implicit ct: ClassTag[T]): Graph[T, S, UnsortedNeighbors[T]] = { | ||
| val nbrs = edges | ||
| .map{ edge => (edge.source, edge.dest) } | ||
| .group | ||
| .mapGroup{ | ||
| case (vert, neighbors) => | ||
| Iterator.single(Vertex(vert, UnsortedNeighbors(neighbors.toArray))) | ||
| } | ||
| .values | ||
|
|
||
| Graph(edges, nbrs) | ||
| } | ||
|
|
||
| /** | ||
| * For all vertices collect their neighbors. | ||
| */ | ||
| def collectNeighbors(implicit ct: ClassTag[T]): Graph[T, S, UnsortedNeighbors[Vertex[T, Q]]] = { | ||
| val nbrs = edges | ||
| .map{ edge => (edge.dest, edge.source) } | ||
| .join(vertices.groupBy(_.id)) | ||
| .toTypedPipe | ||
| .map{ case (dest, (source, vertex)) => (source, (dest, vertex)) } | ||
| .group | ||
| .mapGroup{ | ||
| case (id, vertexes) => | ||
| Iterator.single(Vertex(id, UnsortedNeighbors(vertexes.toArray.map(_._2)))) | ||
| } | ||
| .values | ||
|
|
||
| Graph(edges, nbrs) | ||
| } | ||
|
|
||
| /** | ||
| * Returns each Vertex with all out going edges. | ||
| * Optionally sort the edges | ||
| */ | ||
| def collectEdges(implicit ct: ClassTag[T]): Graph[T, S, UnsortedNeighbors[Edge[T, S]]] = { | ||
| val vertices = edges | ||
| .map{ edge => (edge.source, edge) } | ||
| .group | ||
| .mapGroup{ | ||
| case (id, edgeList) => | ||
| Iterator.single(Vertex(id, UnsortedNeighbors(edgeList.toArray.sortBy(_.dest)))) | ||
| } | ||
| .values | ||
|
|
||
| Graph(edges, vertices) | ||
| } | ||
|
|
||
| /** | ||
| * Filter the graph by the Edge and Vertex filters. | ||
| */ | ||
| def subgraph(epred: EdgeTriplet[T, S, Q] => Boolean = _ => true, vpred: Vertex[T, Q] => Boolean = _ => true): Graph[T, S, Q] = { | ||
| val newTriplets = triplets.filter(epred) | ||
|
|
||
| new GraphUnfilteredEdges[T, S, Q]( | ||
| newTriplets.map(_.edge), | ||
| newTriplets | ||
| .flatMap(trip => List(trip.source, trip.dest)) | ||
| .filter(vpred) | ||
| .distinct(Ordering.by(_.id))) | ||
| } | ||
|
|
||
| /** | ||
| * The current graph is filtered to only include the edges and vertices from the other graph. | ||
| * The attribute of the other graph does not matter, the current attributes are kept. | ||
| */ | ||
| def mask[A, B](other: Graph[T, A, B]): Graph[T, S, Q] = { | ||
| val fEdges = edges | ||
| .map{ e => ((e.dest, e.source), e) } | ||
| .group | ||
| .join(other.edges.map{ e => ((e.dest, e.source), ()) }.group) | ||
| .toTypedPipe | ||
| .map{ case (_, (e, _)) => e } | ||
|
|
||
| val fVertices = vertices | ||
| .groupBy(_.id) | ||
| .join(other.vertices.groupBy(_.id)) | ||
| .toTypedPipe | ||
| .map{ case (_, (v, _)) => v } | ||
|
|
||
| new Graph[T, S, Q](fEdges, fVertices) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sometimes working just on deges is required, in those cases we don't also want to | ||
| * take the computational hit of filtering vertices by the updated edges. In those cases | ||
| * you can return this subgraph that will only filter the vertices when necessary | ||
| */ | ||
| class GraphUnfilteredVertices[T: Ordering, S, Q](inputEdges: TypedPipe[Edge[T, S]], inputVertices: TypedPipe[Vertex[T, Q]]) | ||
| extends Graph[T, S, Q](inputEdges, inputVertices) { | ||
| override def vertices = { | ||
| val graphVertices = edges.flatMap(e => List(e.source, e.dest)).distinct | ||
| inputVertices | ||
| .groupBy(_.id) | ||
| .join(graphVertices.asKeys) | ||
| .values | ||
| .map(_._1) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sometimes working just on vertices is required, in those cases we don't also want to | ||
| * take the computational hit of filtering edges by the updated vertices. In those cases | ||
| * you can return this subgraph that will only filter the edges when necessary | ||
| */ | ||
| class GraphUnfilteredEdges[T: Ordering, S, Q](inputEdges: TypedPipe[Edge[T, S]], inputVertices: TypedPipe[Vertex[T, Q]]) | ||
| extends Graph[T, S, Q](inputEdges, inputVertices) { | ||
|
|
||
| override def edges = | ||
| inputEdges | ||
| .groupBy(_.source) | ||
| .join(vertices.map(_.id).asKeys) | ||
| .values | ||
| .map(_._1) | ||
| .groupBy(_.dest) | ||
| .join(vertices.map(_.id).asKeys) | ||
| .values | ||
| .map(_._1) | ||
|
|
||
| } | ||
|
|
||
| object Graph { | ||
| /** | ||
| * Generate a graph from a set of Directed Edges. | ||
| */ | ||
| def fromEdges[T: Ordering, S](edges: TypedPipe[Edge[T, S]]): Graph[T, S, Unit] = { | ||
| val vertices = edges | ||
| .flatMap(e => List(e.source, e.dest)) | ||
| .distinct | ||
| .map(vertex => Vertex(vertex, ())) | ||
|
|
||
| new Graph(edges, vertices) | ||
| } | ||
|
|
||
| /** | ||
| * Generate a graph from a set of Directed Edges and Vertices | ||
| */ | ||
| def apply[T: Ordering, S, Q](edges: TypedPipe[Edge[T, S]], vertices: TypedPipe[Vertex[T, Q]]): Graph[T, S, Q] = | ||
| new Graph(edges, vertices) | ||
| } |
32 changes: 32 additions & 0 deletions
32
scalding-core/src/main/scala/com/twitter/scalding/graph/Neighbors.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| Copyright 2012 Twitter, Inc. | ||
|
|
||
| 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 | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-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.twitter.scalding.graph | ||
|
|
||
| sealed trait Neighbors[T] { | ||
| def neighbors: Array[T] | ||
| } | ||
|
|
||
| /** | ||
| * List of Sorted Neighbors backed by an Array | ||
| */ | ||
| case class SortedNeighbors[T](val neighbors: Array[T]) extends Neighbors[T] | ||
| /** | ||
| * Unsorted List of Neighbors backed by an Array | ||
| */ | ||
| case class UnsortedNeighbors[T](val neighbors: Array[T]) extends Neighbors[T] { | ||
| def toSorted(implicit ord: Ordering[T]): SortedNeighbors[T] = SortedNeighbors(neighbors.sorted) | ||
| } | ||
|
|
||
18 changes: 18 additions & 0 deletions
18
scalding-core/src/main/scala/com/twitter/scalding/graph/Vertex.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /* | ||
| Copyright 2012 Twitter, Inc. | ||
|
|
||
| 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 | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-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.twitter.scalding.graph | ||
|
|
||
| case class Vertex[T, S](id: T, attr: S) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why an
Arrayhere and notSet[T]orVector[T]? Why do we need the mutation?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need mutation but what is really important here is the least amount of memory overhead for the Neighbors. Moving from Set -> Array saves a lot of memory. Does Vector have memory characteristics close to native arrays?
Also about the classtags above, Array needs a classtag so it can be constructed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we move the
ClassTagcloser to where it is used so we don't pay that cost everywhere?