0

I am using the following query

     START n=node(*)
     MATCH(n)
     WHERE n.Gender ?="Female"
     RETURN n

which runs perfectly in the server.

My program is as follows

    void query()
      {
      ExecutionResult result,;
       Transaction tx=null;
     ExecutionEngine engine = new ExecutionEngine( graphDb );

    try
    {
        String name="Female";
        tx=graphDb.beginTx();
     result=engine.execute("start n=node(*)match(n)where n.Gender ?={Female} return n");
       System.out.println(result.dumpToString());
       tx.success();
    }

    catch(Exception e)
    {
        try
        {           
            File f=new File("trace.txt");
            BufferedWriter br=new BufferedWriter(new FileWriter(f,true));
            tx.failure();
            k=Arrays.toString(e.getStackTrace());
            System.out.println(k);
            br.write(k);
            br.close();
            throw new IOException();
        }
        catch (Exception ex)
        {
        }
      }
    finally
    {
        tx.finish();
    }

}

when I try to execute it through the program it enters the catch block and prints the following stacktrace

            [org.neo4j.cypher.internal.pipes.QueryState$$anonfun$getParam$1.apply(QueryState.scala:60),
             org.neo4j.cypher.internal.pipes.QueryState$$anonfun$getParam$1.apply(QueryState.scala:60),
             scala.collection.MapLike$class.getOrElse(MapLike.scala:128), 
             scala.collection.AbstractMap.getOrElse(Map.scala:58), 
             org.neo4j.cypher.internal.pipes.QueryState.getParam(QueryState.scala:60), 
             org.neo4j.cypher.internal.commands.expressions.ParameterExpression.apply(ParameterExpression.scala:27), 
             org.neo4j.cypher.internal.commands.Equals.isMatch(ComparablePredicate.scala:59),
             org.neo4j.cypher.internal.commands.NullablePredicate.isMatch(Predicate.scala:59),
             org.neo4j.cypher.internal.pipes.FilterPipe$$anonfun$internalCreateResults$1.apply(FilterPipe.scala:30), 
             org.neo4j.cypher.internal.pipes.FilterPipe$$anonfun$internalCreateResults$1.apply(FilterPipe.scala:30),
             scala.collection.Iterator$$anon$14.hasNext(Iterator.scala:390), 
             org.neo4j.cypher.internal.ClosingIterator$$anonfun$next$1.apply(ClosingIterator.scala:45), 
             org.neo4j.cypher.internal.ClosingIterator.failIfThrows(ClosingIterator.scala:86),
             org.neo4j.cypher.internal.ClosingIterator.next(ClosingIterator.scala:43),
             scala.collection.Iterator$class.foreach(Iterator.scala:727), 
             org.neo4j.cypher.internal.ClosingIterator.foreach(ClosingIterator.scala:31),
             scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:48), 
             scala.collection.mutable.ListBuffer.$plus$plus$eq(ListBuffer.scala:178),
             scala.collection.mutable.ListBuffer.$plus$plus$eq(ListBuffer.scala:45), 
             scala.collection.TraversableOnce$class.to(TraversableOnce.scala:259),
             org.neo4j.cypher.internal.ClosingIterator.to(ClosingIterator.scala:31),
             scala.collection.TraversableOnce$class.toList(TraversableOnce.scala:243), 
             org.neo4j.cypher.internal.ClosingIterator.toList(ClosingIterator.scala:31),
             org.neo4j.cypher.PipeExecutionResult.eagerResult(PipeExecutionResult.scala:100),
             org.neo4j.cypher.PipeExecutionResult.dumpToString(PipeExecutionResult.scala:103),
             org.neo4j.cypher.PipeExecutionResult.dumpToString$lzycompute(PipeExecutionResult.scala:143), 
             org.neo4j.cypher.PipeExecutionResult.dumpToString(PipeExecutionResult.scala:140),
             org.neo4j.cypher.javacompat.ExecutionResult.dumpToString(ExecutionResult.java:102),
             Neo4jQuery.query(Neo4jQuery.java:90), Neo4jQuery.main(Neo4jQuery.java:35),
             __SHELL3.run(__SHELL3.java:6), sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method), 
             sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57), 
             sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43), 
             java.lang.reflect.Method.invoke(Method.java:606), bluej.runtime.ExecServer$3.run(ExecServer.java:725)]

I think I am passing the parameter "female" in the wrong way.

2
  • Your exception handling block is weird. Just use a logging framework (or java.util.logging) for logging error messages to disk. Commented Mar 12, 2014 at 23:50
  • You forgot to pass the parameter-map to cypher's execute method, execute(query,params) Commented Mar 12, 2014 at 23:50

1 Answer 1

2

I should start with saying I am not familiar with the Java API for Neo4j. But from what I can see:

In Cypher

MATCH (n)

is incorrect. You need to describe a full path, like MATCH (n)-->(m) or MATCH (n)-[:friend_of]-(m), or skip the match clause all together. In your particular example you don't need the match clause, so you can just skip it.

The second thing is - yes you're not passing the argument correctly - you can see some examples on how to pass arguments to the ExecutionEgine here: http://docs.neo4j.org/chunked/stable/tutorials-cypher-parameters-java.html

In your example you can do:

void query() {
    ExecutionResult result,;
    Transaction tx=null;
    ExecutionEngine engine = new ExecutionEngine( graphDb );

    try
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("gender", "Female");
        tx=graphDb.beginTx();
        result = engine.execute("start n=node(*) where n.Gender ?= {gender} return n",params);
        System.out.println(result.dumpToString());
        tx.success();
    }
    catch(Exception e)
    {
        try
        {           
            File f=new File("trace.txt");
            BufferedWriter br=new BufferedWriter(new FileWriter(f,true));
            tx.failure();
            k=Arrays.toString(e.getStackTrace());
            System.out.println(k);
            br.write(k);
            br.close();
            throw new IOException();
        }
        catch (Exception ex)
        {
        }
      }
    finally
    {
        tx.finish();
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

P.S. if your query doesn't change at all you can also write it like this: engine.execute("start n=node(*) where n.Gender ?= 'Female' return n"); and skip the Map completely.
You forgot to add the params map to the execute call. And usually you would only use Literals for really fixed values, i.e. constants.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.