Created
May 7, 2019 23:21
-
-
Save maqboolkhan/bdd8f913dc3c7a3da723dbdcfad7ec0d to your computer and use it in GitHub Desktop.
SPARQL CRUD (USING APACHE JENA FUSEKI)
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 test.fusekitest.controllers; | |
import org.apache.jena.query.QueryExecution; | |
import org.apache.jena.query.ResultSet; | |
import org.apache.jena.query.ResultSetFormatter; | |
import org.apache.jena.rdfconnection.*; | |
import org.apache.jena.update.UpdateFactory; | |
import org.apache.jena.update.UpdateRequest; | |
import org.springframework.web.bind.annotation.*; | |
import java.io.ByteArrayOutputStream; | |
@RestController | |
public class helloController { | |
private RDFConnectionRemoteBuilder builder = RDFConnectionFuseki.create() | |
.destination("http://localhost:3030/users"); | |
@GetMapping("/insert") | |
public Boolean insert() { | |
// In this variation, a connection is built each time. | |
try ( RDFConnectionFuseki conn = (RDFConnectionFuseki)builder.build() ) { | |
UpdateRequest request = UpdateFactory.create(); | |
request.add("PREFIX example: <http://example/> INSERT DATA {example:anne example:age 30 . example:peter example:birthyear 1982 } ;"); | |
conn.update(request); | |
} | |
return true; | |
} | |
@GetMapping("/delete") | |
public Boolean delete() { | |
// In this variation, a connection is built each time. | |
try ( RDFConnectionFuseki conn = (RDFConnectionFuseki)builder.build() ) { | |
UpdateRequest request = UpdateFactory.create(); | |
request.add("PREFIX example: <http://example/> DELETE DATA {example:anne example:age 30 . example:peter example:birthyear 1982 } ;"); | |
conn.update(request); | |
} | |
return true; | |
} | |
@GetMapping("/update") | |
public Boolean update() { | |
// In this variation, a connection is built each time. | |
try ( RDFConnectionFuseki conn = (RDFConnectionFuseki)builder.build() ) { | |
UpdateRequest request = UpdateFactory.create(); | |
// The idea is SPARQL is not for relational data! Its for graph data | |
// So here we are just deleting the old record and inserting new one | |
request.add("PREFIX example: <http://example/> DELETE DATA { example:anne example:age 30 }; INSERT DATA { example:anne example:age 12 . };"); | |
conn.update(request); | |
} | |
return true; | |
} | |
@GetMapping("/select") | |
public String select() { | |
// In this variation, a connection is built each time. | |
try ( RDFConnectionFuseki conn = (RDFConnectionFuseki)builder.build() ) { | |
QueryExecution qExec = conn.query("prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> prefix owl: <http://www.w3.org/2002/07/owl#> SELECT ?subject ?predicate ?object WHERE {?subject ?predicate ?object }"); | |
ResultSet rs = qExec.execSelect() ; | |
// Converting results into JSON | |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | |
ResultSetFormatter.outputAsJSON(outputStream, rs); | |
return new String(outputStream.toByteArray()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment