gpt4 book ai didi

java - Neo4j SPARQL 插件 REST 使用

转载 作者:行者123 更新时间:2023-12-01 14:04:21 26 4
gpt4 key购买 nike

我尝试使用 REST API 在我的 neo4j 数据库上运行 SPARQL 查询。为了获取 SPARQL 端点,我安装了 Noe4j SPARQL 插件 ( https://github.com/neo4j-contrib/sparql-plugin )。我的系统现在公开这些 sparql 端点:

http://localhost:7474/db/data/ext/SPARQLPlugin/graphdb/insert_quad
http://localhost:7474/db/data/ext/SPARQLPlugin/graphdb/execute_sparql

我正在尝试使用第二个来执行查询。 HTTP POST 请求应该类似于 http://neo4j-contrib.github.io/sparql-plugin/ 中的“示例 2”有一个稍微简单一点的查询。这是源代码:

private static final String ENDPOINT = "http://localhost:7474/db/data/ext/SPARQLPlugin/graphdb/execute_sparql";


private static String query = "SELECT ?x WHERE {?x ?y ?z} LIMIT 5";


public static void main(String[] args) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(ENDPOINT);

// Request parameters and other properties.
httppost.addHeader(new BasicHeader("Method", "POST"));
httppost.addHeader(new BasicHeader("Accept", "application/json"));
httppost.addHeader(new BasicHeader("Content-Type", "application/json"));

JSONObject holder = new JSONObject();
holder.put("query", query);
System.out.println(holder.toString());
httppost.setEntity(new StringEntity(holder.toString()));

//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);

System.out.println("HEADER:");
System.out.println("Status code : " + response.getStatusLine().getStatusCode());
Header[] hlist = response.getAllHeaders();
for (int i = 0; i < hlist.length; i++) {
System.out.print(hlist[i].getName() + " ");
System.out.println(hlist[i].getValue());
}
System.out.println("-----");
HttpEntity entity = response.getEntity();
if (entity != null) {
BufferedReader rd = new BufferedReader(
new InputStreamReader(entity.getContent()));

String line = "";
while ((line = rd.readLine()) != null) {
System.out.println("x " + line);
}
} else {
System.out.println("Entity is null");
}
} catch (Exception e) {
e.printStackTrace(System.out);
}
}

不幸的是,我没有得到想要的输出。输出为:

{"query":"SELECT ?x WHERE {?x ?y ?z} LIMIT 5"}
HEADER:
Status code : 200
Content-Length 4
Content-Type application/json; charset=UTF-8
Access-Control-Allow-Origin *
Server Jetty(6.1.25)
-----
x null

我非常确定这个 SPARQL 查询是正确的;我在安装 sparqpl-plugin 时也没有注意到任何问题,所以我不知道为什么结果是“null”。我尝试通过谷歌寻找解决方案,但没有找到任何有用的东西。我怎样才能解决这个问题 ?我使用的是在 Windows 7 上运行的 Neo4j 1.9.2 社区版安装的sparql插件的版本是“0.2 SNAPSHOT”


编辑1:如果我运行此查询:

SELECT ?x WHERE {?x ?y ?z} LIMIT 0

输出为:

{"query":"SELECT ?x WHERE {?x ?y ?z} LIMIT 0"}
HEADER:
Status code : 200: OK
Content-Length 3
Content-Type application/json; charset=UTF-8
Access-Control-Allow-Origin *
Server Jetty(6.1.25)
-----
x [ ]

也许这有助于查找错误。

我还尝试从 Neo4j 的 HTTP 控制台运行查询:

http> POST http://localhost:7474/db/data/ext/SPARQLPlugin/graphdb/execute_sparql {"query":"select ?x where { graph ?g { ?x ?y ?z } }"}
==> 200 OK
==> null

http> POST http://localhost:7474/db/data/ext/SPARQLPlugin/graphdb/execute_sparql {"query":"select ?x where { ?x ?y ?z }"}
==> 200 OK
==> null



编辑2:我尝试重新下载并构建插件。之后我通过将其解压缩到 NEO4J_HOME/plugins/sparql 来安装它。然后,在启动 Neo4j.bat 后,我​​执行了 java 代码。控制台出现以下错误:

x {
x "message" : "com/tinkerpop/blueprints/impls/neo4j/Neo4jGraph",
x "exception" : "NoClassDefFoundError",
x "fullname" : "java.lang.NoClassDefFoundError",
x "stacktrace" : [ "org.neo4j.server.plugin.sparql.SPARQLPlugin.initSail(SPARQLPlugin.java:90)", "org.neo4j.server.plugin.sparql.SPARQLPlugin.executeSPARQL(SPARQLPlugin.java:61)", "java.lang.reflect.Method.invoke(Unknown Source)", "org.neo4j.server.plugins.PluginMethod.invoke(PluginMethod.java:57)", "org.neo4j.server.plugins.PluginManager.invoke(PluginManager.java:168)", "org.neo4j.server.rest.web.ExtensionService.invokeGraphDatabaseExtension(ExtensionService.java:300)", "org.neo4j.server.rest.web.ExtensionService.invokeGraphDatabaseExtension(ExtensionService.java:122)", "java.lang.reflect.Method.invoke(Unknown Source)", "org.neo4j.server.rest.security.SecurityFilter.doFilter(SecurityFilter.java:112)" ],
x "cause" : {
x "message" : "com.tinkerpop.blueprints.impls.neo4j.Neo4jGraph",
x "exception" : "ClassNotFoundException",
x "stacktrace" : [ "java.net.URLClassLoader$1.run(Unknown Source)", "java.net.URLClassLoader$1.run(Unknown Source)", "java.security.AccessController.doPrivileged(Native Method)", "java.net.URLClassLoader.findClass(Unknown Source)", "java.lang.ClassLoader.loadClass(Unknown Source)", "sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)", "java.lang.ClassLoader.loadClass(Unknown Source)", "org.neo4j.server.plugin.sparql.SPARQLPlugin.initSail(SPARQLPlugin.java:90)", "org.neo4j.server.plugin.sparql.SPARQLPlugin.executeSPARQL(SPARQLPlugin.java:61)", "java.lang.reflect.Method.invoke(Unknown Source)", "org.neo4j.server.plugins.PluginMethod.invoke(PluginMethod.java:57)", "org.neo4j.server.plugins.PluginManager.invoke(PluginManager.java:168)", "org.neo4j.server.rest.web.ExtensionService.invokeGraphDatabaseExtension(ExtensionService.java:300)", "org.neo4j.server.rest.web.ExtensionService.invokeGraphDatabaseExtension(ExtensionService.java:122)", "java.lang.reflect.Method.invoke(Unknown Source)", "org.neo4j.server.rest.security.SecurityFilter.doFilter(SecurityFilter.java:112)" ],
x "fullname" : "java.lang.ClassNotFoundException"
x }
x }

我发现,“com.tinkerpop.blueprints.impls.neo4j.Neo4jGraph”是某些“blueprints-neo4j-graph.jar”的一部分。我的 sparql/文件夹中缺少此 jar 文件,因此我搜索了最新的 jar 文件并添加了“blueprints-neo4j-graph-2.5.0-20130916.151744-5.jar”。重新启动服务器后,错误不再出现,但我仍然得到“null”结果(在两个查询中的任何一个)

编辑3:我还尝试从neof4 sparql-plugin的github页面复制“BerlinDatasetTest.java”的某些部分并手动运行它(添加所有必要的jar文件后的ofc)。这是源代码:

public String runQuery(String raw_query) {
String q_result = "";
Sail sail = new GraphSail( new Neo4jGraph("my_db/") );
try {
sail.initialize();
QueryParser parser = new SPARQLParserFactory().getParser();
ParsedQuery query = null;
CloseableIteration<? extends BindingSet, QueryEvaluationException> sparqlResults;

SailConnection conn = sail.getConnection();

query = parser.parseQuery(raw_query, "http://localhost:7474");
sparqlResults = conn.evaluate( query.getTupleExpr(),


query.getDataset(), new EmptyBindingSet(), false );

while ( sparqlResults.hasNext() ) {
q_result += "; " + sparqlResults.next();
}

conn.close();
sail.shutDown();

} catch (Exception e) {
e.printStackTrace();
}

return q_result;
}

这是我得到的结果

java.lang.NullPointerException
at com.tinkerpop.blueprints.oupls.sail.GraphSailConnection.toSesame(GraphSailConnection.java:729)
at com.tinkerpop.blueprints.oupls.sail.GraphSailConnection.fillStatement(GraphSailConnection.java:636)
at com.tinkerpop.blueprints.oupls.sail.GraphSailConnection.access$800(GraphSailConnection.java:36)
at com.tinkerpop.blueprints.oupls.sail.GraphSailConnection$StableStatementIteration.next(GraphSailConnection.java:624)
at com.tinkerpop.blueprints.oupls.sail.GraphSailConnection$StableStatementIteration.next(GraphSailConnection.java:589)
at info.aduna.iteration.IterationWrapper.next(IterationWrapper.java:71)
at net.fortytwo.sesametools.QueryEvaluationIteration.next(QueryEvaluationIteration.java:45)
at net.fortytwo.sesametools.QueryEvaluationIteration.next(QueryEvaluationIteration.java:16)
at info.aduna.iteration.IterationWrapper.next(IterationWrapper.java:71)

at info.aduna.iteration.FilterIteration.findNextElement(FilterIteration.java:69)
at info.aduna.iteration.FilterIteration.hasNext(FilterIteration.java:43)
at info.aduna.iteration.ConvertingIteration.hasNext(ConvertingIteration.java:62)
at info.aduna.iteration.ConvertingIteration.hasNext(ConvertingIteration.java:62)
at info.aduna.iteration.IterationWrapper.hasNext(IterationWrapper.java:57)
at org.openrdf.sail.helpers.SailBaseIteration.hasNext(SailBaseIteration.java:50)
at src.QueryTest.runQuery(QueryTest.java:113)
at src.QueryTest.main(QueryTest.java:140)

“QueryTest.java”是我在其中运行此函数的java类的名称。

编辑4:看来,结果只是空,如果

query.getDataset()

变为 null(这在两个查询中都会发生)如果我添加 FROM 子句或开始在 WHERE 部分内使用 URL,query.Dataset() 不再为 null,并且我会得到一个空结果(不是 null)。这是两个“有效”查询:

select ?x from <localhost:7474> where { graph ?g { ?x ?y ?z } } 
select ?x where { graph ?g { ?x <http://localhost:7474#knows> ?z } }

我仍然没有得到任何结果。

最佳答案

我正在运行 4 节点集群。ClassNotFoundException 的修复非常简单。

  1. 您复制的 gremlin 插件文件夹包含旧版本 (1.2) 的 blueprints-core 和 blueprints-neo4j-graph。删除gremlin插件目录下那些旧的库。

  2. 更新 sparql 插件项目的 pom.xml 以包含最新的蓝图库。触发 Maven 构建(mvn clean package)。现在将 neo4j-sparql-plugin-0.2-SNAPSHOT-server-plugin.zip 解压到 gremlin 目录中。确保您拥有最新的 blueprints-neo4j-graph-2.3.0.jar。

尝试

现在您的 API 应该可以工作了,您可以将 Neo4j 视为成熟的三元组存储。玩得开心!

关于java - Neo4j SPARQL 插件 REST 使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19039995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com