简体   繁体   中英

Java SPARQL Select query

I wrote a SELECT statement that returns me all data from the fuseki-server . My question is how I can get a list with all names available below the o column (see the attached image)?

在此处输入图片说明

For example, I need an array that will contain the following items: [Pavel Ionut, Cirstea Aurel, Test1]

This is my SELECT statement, which should be changed to return only the name property ... but I did not use SPARQL before and any solution would help me a lot.

QueryExecution qe = QueryExecutionFactory.sparqlService(
        "http://localhost:3030/Date/query", "SELECT * WHERE {?s ?p ?o}");
ResultSet results = qe.execSelect();
ResultSetFormatter.out(System.out, results);
qe.close();
System.out.println(results);

To return the set of objects from all existing spo triples use this SPARQL query:

SELECT DISTINCT ?o WHERE { ?s ?p ?o . }

To only get the names, try:

PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT DISTINCT ?n WHERE { ?s dc:name ?n . }

or just:

SELECT DISTINCT ?n WHERE { ?s <http://purl.org/dc/elements/1.1/name> ?n . }

The specification contains some good examples:

To iterate over the result set of the query above with Apache Jena Fuseki in Java, this might work:

while (results.hasNext()) {
    QuerySolution querySolution = results.next();
    RDFNode node = querySolution.get("n");
    String name = node.asLiteral().getString();
    System.out.println(name);
}

You need to identify what the same you queried in your query. More detail you can find here:

exploratory SPARQL queries?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM