forked from miciek/grokkingfp-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch11_QueryingWikidata.java
More file actions
53 lines (46 loc) · 2.27 KB
/
Copy pathch11_QueryingWikidata.java
File metadata and controls
53 lines (46 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.rdfconnection.RDFConnection;
import org.apache.jena.rdfconnection.RDFConnectionRemote;
import java.util.Iterator;
public class ch11_QueryingWikidata {
public static void main(String[] args) {
String query =
"PREFIX wd: <http://www.wikidata.org/entity/>\n" +
"PREFIX wdt: <http://www.wikidata.org/prop/direct/>\n" +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"SELECT DISTINCT ?attraction ?label WHERE {\n" +
" ?attraction wdt:P31 wd:Q570116;\n" +
" rdfs:label ?label.\n" +
" FILTER(LANG(?label) = \"en\").\n" +
"} LIMIT 3";
RDFConnection connection = RDFConnectionRemote.create()
.destination("https://query.wikidata.org/")
.queryEndpoint("sparql")
.build();
QueryExecution execution = connection.query(QueryFactory.create(query));
Iterator<QuerySolution> solutions = execution.execSelect();
solutions.forEachRemaining(solution -> {
String id = solution.getResource("attraction").getLocalName();
String label = solution.getLiteral("label").getString();
System.out.printf("Got attraction %s (id = %s)%n", label, id);
});
execution.close();
connection.close();
// or:
// try(RDFConnection connection = RDFConnectionRemote.create()
// .destination("https://query.wikidata.org/")
// .queryEndpoint("sparql")
// .build()) {
// try(QueryExecution execution = connection.query(QueryFactory.create(query))) {
// Iterator<QuerySolution> solutions = execution.execSelect();
// solutions.forEachRemaining(solution -> {
// String id = solution.getResource("attraction").getLocalName();
// String label = solution.getLiteral("label").getString();
// System.out.printf("Got attraction %s (id = %s)%n", label, id);
// });
// }
// }
}
}