diff --git a/docs/graphx-programming-guide.md b/docs/graphx-programming-guide.md
index 826f6d8f371c7..548f0e9572600 100644
--- a/docs/graphx-programming-guide.md
+++ b/docs/graphx-programming-guide.md
@@ -32,6 +32,7 @@ description: GraphX graph processing library guide for Spark SPARK_VERSION_SHORT
[GraphOps.pregel]: api/scala/index.html#org.apache.spark.graphx.GraphOps@pregel[A](A,Int,EdgeDirection)((VertexId,VD,A)⇒VD,(EdgeTriplet[VD,ED])⇒Iterator[(VertexId,A)],(A,A)⇒A)(ClassTag[A]):Graph[VD,ED]
[PartitionStrategy]: api/scala/index.html#org.apache.spark.graphx.PartitionStrategy$
[GraphLoader.edgeListFile]: api/scala/index.html#org.apache.spark.graphx.GraphLoader$@edgeListFile(SparkContext,String,Boolean,Int):Graph[Int,Int]
+[RDFLoader.loadNTriples]: api/scala/index.html#org.apache.spark.graphx.loaders.RDFLoader@loadNTriples(SparkContext,String,Int,Int):Graph[String,String]
[Graph.apply]: api/scala/index.html#org.apache.spark.graphx.Graph$@apply[VD,ED](RDD[(VertexId,VD)],RDD[Edge[ED]],VD)(ClassTag[VD],ClassTag[ED]):Graph[VD,ED]
[Graph.fromEdgeTuples]: api/scala/index.html#org.apache.spark.graphx.Graph$@fromEdgeTuples[VD](RDD[(VertexId,VertexId)],VD,Option[PartitionStrategy])(ClassTag[VD]):Graph[VD,Int]
[Graph.fromEdges]: api/scala/index.html#org.apache.spark.graphx.Graph$@fromEdges[VD,ED](RDD[Edge[ED]],VD)(ClassTag[VD],ClassTag[ED]):Graph[VD,ED]
@@ -875,6 +876,11 @@ object Graph {
+## RDF Graph Builder
+[`RDFLoader.loadNTriples`][RDFLoader.loadNTriples] loads an RDF graph from a .nt dump into a `Graph[String,String]`.
+Both resource nodes and literal nodes are mapped to vertices, with the URI or the literal value (not processed in any way) as the String value. Each `Vertex[String]` is assigned an Id using a simple hash function applied to the URI of the resource node or to the value of the literal node prefixed by the subject and property URI's of the triple where that literal occurs. Thus, literal nodes identical in value but from non-identical triples are each assigned a separate `Vertex[String]` in the `Graph[String, String]`.
+ Each valid triple is mapped to an `Edge[String]`, carrying the RDF property URI as its `String` value.
+
# Vertex and Edge RDDs
GraphX exposes `RDD` views of the vertices and edges stored within the graph. However, because
diff --git a/graphx/src/main/scala/org/apache/spark/graphx/loaders/RDFLoader.scala b/graphx/src/main/scala/org/apache/spark/graphx/loaders/RDFLoader.scala
new file mode 100644
index 0000000000000..d747e4ae0705b
--- /dev/null
+++ b/graphx/src/main/scala/org/apache/spark/graphx/loaders/RDFLoader.scala
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.spark.graphx.loaders
+
+
+import org.apache.spark.storage.StorageLevel
+import org.apache.spark.{Logging, SparkContext}
+import org.apache.spark.graphx.impl.{EdgePartitionBuilder, GraphImpl}
+import org.apache.spark.AccumulatorParam
+import org.apache.spark.rdd.RDD
+import org.apache.spark.graphx.Graph
+import org.apache.spark.graphx.Edge
+
+/**
+ * Provides utilities for loading RDF [[Graph]]s from .NT dumps.
+ */
+object RDFLoader extends Logging {
+
+ val relregex = "^<([^>]+)>\\s<([^>]+)>\\s<([^>]+)>\\s\\.$".r
+ val propregex = "^<([^>]+)>\\s<([^>]+)>\\s(.+)\\s\\.$".r
+ val propvalregex = "^<([^>]+)>\\s(.+)$".r
+
+ /**
+ * Transforms an RDF dump in ntriples (.nt) format into a graph.
+ * Uses a simple hash function to generate VertexIds.
+ * All subject and object values of triples are represented as a vertex with
+ * the hash as VertexId and the URI (for resources) or the value (for literals)
+ * as the associated String value for that vertex.
+ * The edges map between vertices and carry the label of the relation URI.
+ * Literal values that are identical in value but occur in different triples
+ * are mapped to different nodes.
+ *
+ * @param sc SparkContext
+ * @param path the path to the file (e.g., /home/data/file or hdfs://file)
+ *
+ * @param edgeStorageLevel the desired storage level for the edge partitions
+ * @param vertexStorageLevel the desired storage level for the vertex partitions
+ */
+ def loadNTriples(
+ sc: SparkContext,
+ path: String,
+ // numPartitions: Int = -1,
+ edgeStorageLevel: StorageLevel = StorageLevel.MEMORY_ONLY,
+ vertexStorageLevel: StorageLevel = StorageLevel.MEMORY_ONLY
+ )
+ : Graph[String, String] =
+ {
+ val startTime = System.currentTimeMillis()
+
+ val lines = sc.textFile(path)
+
+ val vertices = lines
+ .flatMap(line => {
+ line match {
+ case relregex(subj, rel, obj) => Set(subj, obj)
+ case propregex(subj, rel, value) => Set(subj, "<" + subj + "-" + rel + "> " + value)
+ case _ => Set[String]()
+ }
+ })
+ .distinct()
+ // .repartition(numPartitions)
+ .map(name =>
+ name match {
+ case propvalregex(pre, value) => (gethash("<" + pre + "> " + value), value)
+ case _ => (gethash(name), name)
+ }
+ )
+ .persist(vertexStorageLevel) // TODO: set name etc
+
+ val edges: RDD[Edge[String]] = lines
+ .map( line => {
+ line match {
+ case relregex(subj, rel, obj) => Edge(gethash(subj), gethash(obj), rel)
+ case propregex(subj, rel, obj)
+ => Edge(gethash(subj), gethash("<" + subj + "-" + rel + "> " + obj), rel)
+ case _ => Edge(0,0, "null")
+ }
+ })
+ .persist(edgeStorageLevel) // TODO: set name
+
+ val graph = Graph(vertices, edges)
+ return graph // so far
+ } // end of edgeListFile
+
+
+ /**
+ * Gets dictionary for entities (not properties) from the provided graph.
+ */
+ def getdictionary(graph:Graph[String,String]): RDD[(Long,String)] = {
+ val vertices = graph.vertices.map(x => (x._1.toLong, x._2))
+ return vertices
+ }
+
+
+ /**
+ * Implements a simple hashing function for Strings
+ */
+ def gethash(in:String):Long = {
+ var h = 1125899906842597L
+ for (x <- in) {
+ h = 31 * h + x;
+ }
+ return h
+ }
+
+}
+
diff --git a/graphx/src/test/resources/graph_nt.data b/graphx/src/test/resources/graph_nt.data
new file mode 100644
index 0000000000000..93c63a1c3041d
--- /dev/null
+++ b/graphx/src/test/resources/graph_nt.data
@@ -0,0 +1,17 @@
+ "1142"@en .
+ "299.00"@en .
+ "209850"^^ .
+ "001526"@en .
+ "med"@en .
+ "3202"@en .
+ "D001321"@en .
+ "Autism"@en .
+ "Aristotle"@en .
+ "-0384"^^ .
+ "-0322"^^ .
+ .
+ .
+ .
+ .
+ .
+ .
\ No newline at end of file
diff --git a/graphx/src/test/scala/org/apache/spark/graphx/loaders/RDFLoaderSuite.scala b/graphx/src/test/scala/org/apache/spark/graphx/loaders/RDFLoaderSuite.scala
new file mode 100644
index 0000000000000..e3659fc550fa5
--- /dev/null
+++ b/graphx/src/test/scala/org/apache/spark/graphx/loaders/RDFLoaderSuite.scala
@@ -0,0 +1,62 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You 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 org.apache.spark.graphx.loaders
+
+import org.scalatest.FunSuite
+import org.apache.spark.graphx.LocalSparkContext
+import scala.io.Source
+
+class RDFLoaderSuite extends FunSuite with LocalSparkContext {
+ test("RDFLoader.loadNTriples") {
+ withSpark { sc =>
+ val file = getClass.getResource("/graph_nt.data").getFile
+ val graph = RDFLoader.loadNTriples(sc, file)
+ val edges = graph.edges.collect()
+ assert(edges.length == 17)
+ val vertices = graph.vertices.collect()
+ assert(vertices.length == 19)
+ val triples = graph.mapTriplets(triplet => {
+ triplet.srcAttr + "<" + triplet.attr + ">" + triplet.dstAttr
+ }).edges.collect
+ val propregex = "<([^>]+)>\\s<([^>]+)>\\s(.+)\\s\\.".r
+ val relregex = "<([^>]+)>\\s<([^>]+)>\\s<([^>]+)>\\s\\.".r
+
+ // for (triple <- triples) Console.println(triple.attr)
+
+ def assertline(a: String, b: String, c: String) = {
+ // find corresponding line from triples from graph
+ var found = false;
+ for (triple <- triples) {
+ if (triple.attr == a + "<" + b + ">" + c) {
+ found = true
+ }
+ }
+ if (!found) Console.println(a, b, c)
+ assert(found)
+ }
+ val original = Source.fromFile(file)
+ for (line <- original.getLines) {
+ line match {
+ case relregex(a, b, c) => assertline(a, b, c)
+ case propregex(a, b, c) => assertline(a, b, c)
+ case _ =>
+ }
+ }
+ original.close
+ }
+ }
+}
\ No newline at end of file