我正在尝试从 Wikidata 查询实体的所有实例。我发现目前唯一的方法是使用 SPARQL 端点。
我找到了一个关于我想做的事情的示例查询,并从 Web 界面成功执行了它。不幸的是,我似乎无法从我的 Java 代码中执行它。我正在使用 openRDF SPARQL 库。这是我的相关代码:
SPARQLRepository sparqlRepository = new SPARQLRepository(
"https://query.wikidata.org/");
SPARQLConnection sparqlConnection = new SPARQLConnection(
sparqlRepository);
String query = "SELECT ?s ?desc ?authorlabel (COUNT(DISTINCT ?sitelink) as ?linkcount) WHERE {"
+ "?s wdt:P31 wd:Q571 ."
+ "?sitelink schema:about ?s ."
+ "?s wdt:P50 ?author"
+ "OPTIONAL { ?s rdfs:label ?desc filter (lang(?desc) = \"en\"). }"
+ "OPTIONAL {"
+ "?author rdfs:label ?authorlabel filter (lang(?authorlabel) = \"en\")."
+ "}"
+ "} GROUP BY ?s ?desc ?authorlabel ORDER BY DESC(?linkcount)";
TupleQuery tupleQuery = sparqlConnection.prepareTupleQuery(
QueryLanguage.SPARQL, query);
System.out.println("Result for tupleQuery" + tupleQuery.evaluate());
这是我收到的回复:
Exception in thread "main" org.openrdf.query.QueryEvaluationException: <html>
<head><title>405 Not Allowed</title></head>
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx/1.9.4</center>
</body>
</html>
at org.openrdf.repository.sparql.query.SPARQLTupleQuery.evaluate(SPARQLTupleQuery.java:59)
at main.Test.main(Test.java:72)
Caused by: org.openrdf.repository.RepositoryException: <html>
<head><title>405 Not Allowed</title></head>
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx/1.9.4</center>
</body>
</html>
at org.openrdf.http.client.HTTPClient.handleHTTPError(HTTPClient.java:953)
at org.openrdf.http.client.HTTPClient.sendTupleQueryViaHttp(HTTPClient.java:718)
at org.openrdf.http.client.HTTPClient.getBackgroundTupleQueryResult(HTTPClient.java:602)
at org.openrdf.http.client.HTTPClient.sendTupleQuery(HTTPClient.java:367)
at org.openrdf.repository.sparql.query.SPARQLTupleQuery.evaluate(SPARQLTupleQuery.java:52)
... 1 more
通常我会假设这意味着我需要某种 API 密钥,但 Wikidata API 似乎是完全开放的。我在设置连接时犯了错误吗?