Skip to content
This repository was archived by the owner on Oct 8, 2020. It is now read-only.

Commit d0cd91d

Browse files
committed
Add RDF Graph Kernel example for Spark
1 parent 180869a commit d0cd91d

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package net.sansa_stack.examples.spark.ml.kernel
2+
3+
import net.sansa_stack.ml.spark.kernel._
4+
import net.sansa_stack.rdf.spark.io._
5+
import org.apache.jena.riot.Lang
6+
import org.apache.spark.sql.SparkSession
7+
8+
/**
9+
* RDF Graph Kernel example.
10+
*/
11+
object RDFGraphKernel {
12+
def main(args: Array[String]) {
13+
parser.parse(args, Config()) match {
14+
case Some(config) =>
15+
run(config.in, config.iteration)
16+
case None =>
17+
println(parser.usage)
18+
}
19+
}
20+
21+
def run(input: String, iteration: Int = 5): Unit = {
22+
23+
println("======================================")
24+
println("| RDF Graph Kernel example |")
25+
println("======================================")
26+
27+
val spark = SparkSession.builder
28+
.appName(s" RDF Graph Kernel example ( $input )")
29+
.master("local[*]")
30+
.config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
31+
.getOrCreate()
32+
33+
val t0 = System.nanoTime
34+
val lang = Lang.NTRIPLES
35+
36+
val triples = spark.rdf(lang)(input)
37+
.filter(_.getPredicate.getURI != "http://swrc.ontoware.org/ontology#employs")
38+
39+
val rdfFastGraphKernel = RDFFastGraphKernel(spark, triples, "http://swrc.ontoware.org/ontology#affiliation")
40+
val data = rdfFastGraphKernel.getMLLibLabeledPoints
41+
42+
val t1 = System.nanoTime
43+
RDFFastTreeGraphKernelUtil.printTime("Initialization", t0, t1)
44+
45+
RDFFastTreeGraphKernelUtil.predictLogisticRegressionMLLIB(data, 4, iteration)
46+
47+
val t2 = System.nanoTime
48+
RDFFastTreeGraphKernelUtil.printTime("Run Prediction", t1, t2)
49+
50+
}
51+
52+
case class Config(
53+
in: String = "",
54+
iteration: Int = 5)
55+
56+
val parser = new scopt.OptionParser[Config]("Mines the Rules example") {
57+
58+
head("Mines the Rules example")
59+
60+
opt[String]('i', "input").required().valueName("<path>").
61+
action((x, c) => c.copy(in = x)).
62+
text("path to file that contains the data")
63+
64+
opt[Int]('k', "iteration").required().valueName("<iteration>").
65+
action((x, c) => c.copy(iteration = x)).
66+
text("the iteration or folding on validation")
67+
68+
help("help").text("prints this usage text")
69+
}
70+
}
71+

0 commit comments

Comments
 (0)