问题
Currently, Wikidata has a SPARQL endpoint "https://query.wikidata.org/", I would like to query this site using Jena (3.0.1), I use the following code but I got an error message "Endpoint returned Content-Type: text/html which is not currently supported for SELECT queries". Is there a way to solve it? the same code works fine with dbpedia. Thanks
queryString = "PREFIX bd: <http://www.bigdata.com/rdf#>\n" +
"PREFIX wikibase: <http://wikiba.se/ontology#>\n" +
"PREFIX wdt: <http://www.wikidata.org/prop/direct/>\n" +
"PREFIX wd: <http://www.wikidata.org/entity/>\n" +
"SELECT DISTINCT ?country ?countryLabel\n" +
"WHERE\n" +
"{\n" +
"\t?country wdt:P31 wd:Q3624078 .\n" +
" ?country wdt:P1622 wd:Q13196750.\n" +
" ?country wdt:P30 wd:Q15\n" +
"\tFILTER NOT EXISTS {?country wdt:P31 wd:Q3024240}\n" +
"\tSERVICE wikibase:label { bd:serviceParam wikibase:language \"en\" }\n" +
"}\n" +
"ORDER BY ?countryLabel";
query = QueryFactory.create(queryString);
qexec = QueryExecutionFactory.sparqlService("https://query.wikidata.org/", queryString);
try {
ResultSet results = qexec.execSelect();
ResultSetFormatter.out(System.out, results, query);
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
qexec.close();
}
回答1:
According to the documentation, the endpoint has a /sparql at the end. It says
SPARQL queries can be submitted directly to the SPARQL endpoint with a GET request to
https://query.wikidata.org/sparql?query={SPARQL}
(POST and other method requests will be denied with a "403 Forbidden"). The result is returned as XML by default, or as JSON if either the query parameter format=json or the header Accept: application/sparql-results+json are provided.
来源:https://stackoverflow.com/questions/36535702/use-jena-to-query-wikidata