使用 Jena 查询维基数据
Use Jena to query wikidata
目前,维基数据有一个 SPARQL 端点“https://query.wikidata.org/”,我想使用 Jena (3.0.1) 查询此站点,我使用以下代码但收到错误消息“端点返回的内容类型:text/html 当前不支持 SELECT 查询”。有办法解决吗?相同的代码适用于 dbpedia。谢谢
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();
}
根据 the documentation,端点末尾有一个 /sparql。它说
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.
目前,维基数据有一个 SPARQL 端点“https://query.wikidata.org/”,我想使用 Jena (3.0.1) 查询此站点,我使用以下代码但收到错误消息“端点返回的内容类型:text/html 当前不支持 SELECT 查询”。有办法解决吗?相同的代码适用于 dbpedia。谢谢
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();
}
根据 the documentation,端点末尾有一个 /sparql。它说
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.