Created
July 6, 2011 03:05
-
-
Save AlBaker/1066472 to your computer and use it in GitHub Desktop.
Spring Jena Example
This file contains 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
package com.example; | |
import org.springframework.data.rdf.sparql.*; | |
import org.springframework.data.rdf.jena.*; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class TestSpringJena { | |
private Model model; | |
private JenaTemplate jena; | |
private SparqlTemplate sparql; | |
@Before | |
public void init() { | |
model = ModelFactory.createDefaultModel(); | |
jena = new JenaTemplate(); | |
sparql = new SparqlTemplate(); | |
jena.setModel(model); | |
sparql.setModel(model); | |
} | |
@Test | |
public void testModel() { | |
jena.add("urn:test", "urn:someProperty", "hello world"); | |
// Select by list | |
String query1 = "SELECT ?x ?y ?z WHERE { ?x ?y ?z }"; | |
List<String> list = sparql.execSelectList(query1, new SolutionMapper<String>() { | |
public String mapSelect(ResultSet rs, int rowNum) { | |
QuerySolution sln = rs.nextSolution(); | |
RDFNode x = sln.get("x"); | |
RDFNode y = sln.get("y"); | |
RDFNode z = sln.get("z"); | |
return x.toString() + "|" + y.toString() + "|" + z.toString(); | |
} }); | |
// result set as a map of strings | |
Map<String, String> map = sparql.execSelectStringMap(query1); | |
// Need one parameter? select a string | |
String query2 = "SELECT ?z WHERE { <urn:test> ?y ?z }"; | |
String result = sparql.execSelectString(query2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment