Reputation: 63
Im trying to run a SPARQL-query on my neo4j database by using the REST API. For getting a SPARQL-endpoint, Ive installed the Noe4j SPARQL-plugin (https://github.com/neo4j-contrib/sparql-plugin). My system now exposes these sparql-endpoints:
http://localhost:7474/db/data/ext/SPARQLPlugin/graphdb/insert_quad
http://localhost:7474/db/data/ext/SPARQLPlugin/graphdb/execute_sparql
Im trying to use the sencond one to execute queries. The HTTP POST Request is supposed to be like "Example 2" in http://neo4j-contrib.github.io/sparql-plugin/ with a slightly easier query. This is the source-code:
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);
}
}
Unfortunately, I dont get the desired output. The output is:
{"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
I´m pretty sure that this SPARQL query is correct; I also didnt notice any problems upon installing the sparqpl-plugin, so I have no idea why I get "null" as result. Ive tried looking for a solution via google, but I didnt find anything helpful. How can I fix this ?
Im using the Community-Edition of Neo4j 1.9.2 running on Windows 7
The version of the installed sparql plugin is "0.2 SNAPSHOT"
EDIT 1: If I run this query:
SELECT ?x WHERE {?x ?y ?z} LIMIT 0
The output is:
{"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 [ ]
Maybe this helps at finding the error.
I´ve also tried running queries from the HTTP Console of Neo4j:
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
EDIT 2:
I´ve tried to re-download and build the plugin. Afterwards I installed it, by unzipping it at NEO4J_HOME/plugins/sparql. Then, after I started Neo4j.bat, I executed my java-code. The following error came up at the console:
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 }
I´ve found, that "com.tinkerpop.blueprints.impls.neo4j.Neo4jGraph" is part of some "blueprints-neo4j-graph.jar". This jar-file was missing in my sparql/ folder, so I searched for the most recent jar file and added "blueprints-neo4j-graph-2.5.0-20130916.151744-5.jar". After restarting the server, the error doesnt come up anymore, but I still get "null" as result (at either of both queries)
EDIT 3: Ive also tried coying some parts of the "BerlinDatasetTest.java" from the github page of the neof4 sparql-plugin and running it manually (ofc after adding all necessary jar files). This is the source code:
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;
}
This is the result I get
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" is the name of the java-class in which I run this function.
EDIT 4: It appears, that the result is only null, if
query.getDataset()
becomes null (which happens at both of the queries) If I add a FROM clause or start using URLs inside the WHERE-part, query.Dataset() isnt null anymore and Im getting an empty result (which isnt null). Those are two of the "working" queries:
select ?x from <localhost:7474> where { graph ?g { ?x ?y ?z } }
select ?x where { graph ?g { ?x <http://localhost:7474#knows> ?z } }
I still dont get any results.
Upvotes: 0
Views: 1479
Reputation: 224
I am running 4 node cluster. The fix for ClassNotFoundException is very simple.
The gremlin plugin folder you copy contains older version (1.2) of blueprints-core and blueprints-neo4j-graph. Delete those old libraries under the gremlin plugin directory.
Update pom.xml of sparql plugin project to include latest blueprint libraries. Trigger a maven build (mvn clean package). Now extract the neo4j-sparql-plugin-0.2-SNAPSHOT-server-plugin.zip into gremlin directory. Make sure you have latest blueprints-neo4j-graph-2.3.0.jar.
Try
Now your APIs should work and you can treat your Neo4j as a full fledged triplestore. Have fun!
Upvotes: 1