Re: [Neo4j] Unable to fetch data from Cypher qurey in Java (embedded Neo4j)

293 views
Skip to first unread message

Peter Neubauer

unread,
Dec 28, 2012, 8:09:36 PM12/28/12
to Neo4j User
Mohammd,
do you have the reproducable code with the original cypher query and the expected outcome somewhere? Is it just an empty iterator or NullPointerException?

/peter


Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

Neo4j 1.8 GA - http://www.dzone.com/links/neo4j_18_release_fluent_graph_literacy.html


On Sat, Dec 29, 2012 at 1:17 AM, Orion <anas...@gmail.com> wrote:
Hello,

I'm new to Neo4j. I tried this official example "Execute Cypher Queries from Java". Every thing was done as expected including the raw output of the query. However, I couldn't fetch the data, and it seems that the iterator result.columnAs( "n" ) is empty in the following position.
Iterator<Node> n_column = result.columnAs( "n" );
for ( Node node : IteratorUtil.asIterable( n_column ) )
{
    // note: we're grabbing the name property from the node,
    // not from the n.name in this case.
    nodeResult = node + ": " + node.getProperty( "name" );
}
Where is the problem?

Thanks in advance.

--
 
 

Orion

unread,
Dec 30, 2012, 1:00:52 PM12/30/12
to ne...@googlegroups.com
Hello Peter,


I tried to rewrite the code, and discovered that commenting the
    System.out.println(result.toString());

makes the iterator
    result.columnAs("n")

non-empty. Do you have an explanation for this? And, is the best practice to fetch data from query?

Here is the code:

package TestNeo4j1;

import java.util.Iterator;
import java.util.List;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.helpers.collection.IteratorUtil;

public class TestNeo4j1 {

    public static void main(final String[] args) {
        String DB_PATH = "C:/neo4j-community-1.9.M02/data/MyDB";
        GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
        Transaction tx = graphDb.beginTx();
        org.neo4j.cypher.javacompat.ExecutionEngine engine = new org.neo4j.cypher.javacompat.ExecutionEngine(graphDb);
        org.neo4j.cypher.javacompat.ExecutionResult result =
                engine.execute("START n=node:nodes(nodeName = \"Some Node Name\") RETURN n, n.nodeName");

//        System.out.println(result.toString());

        Iterator<Node> n_column = result.columnAs("n");
        String nodeResult = "";
        for (Node node : IteratorUtil.asIterable(n_column)) {
            nodeResult += node + ": " + node.getProperty("nodeName") + " " + node.getId() + "\n";
        }
        System.out.println(nodeResult);

        try {
            tx.success();
        } finally {
            tx.finish();
        }
        graphDb.shutdown();
    }
}

Thanks in advance!

ben

unread,
Dec 30, 2012, 1:21:08 PM12/30/12
to ne...@googlegroups.com
Hi, here's how I did it if it can help you

import org.neo4j.cypher.javacompat.ExecutionEngine;
import org.neo4j.cypher.javacompat.ExecutionResult;

...



ExecutionEngine engine = new ExecutionEngine( graphDb );
ExecutionResult result = engine.execute( "start n=node(*) where has(n.brand) return n" );


Iterator<Node> list_brands = result.columnAs("n");
while(list_brands.hasNext()){
    Node brand = list_brands.next();
    System.out.println(brand.getId()+"   "+brand.getProperty("brand"));
}


If I remember well, I found it somewhere in here : http://stackoverflow.com/questions/8650948/neo4j-cypher-how-to-iterate-over-executionresult-result


Le samedi 29 décembre 2012 01:17:20 UTC+1, Orion a écrit :
Hello,

I'm new to Neo4j. I tried this official example "Execute Cypher Queries from Java". Every thing was done as expected including the raw output of the query. However, I couldn't fetch the data, and it seems that the iterator result.columnAs( "n" ) is empty in the following position.
Iterator<Node> n_column = result.columnAs( "n" );
for ( Node node : IteratorUtil.asIterable( n_column ) )
{
    // note: we're grabbing the name property from the node,
    // not from the n.name in this case.
<div class="container" style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline: 0px !important; overflow: visible !important; position: relative !important; right: aut...
Afficher le message d'origine

Orion

unread,
Dec 30, 2012, 1:48:12 PM12/30/12
to ne...@googlegroups.com

Hi Peter, 

Thank you for your response. My other question is: when I add the line

System.out.println(result.toString());

(for demonstration/debugging purposes only) just before

Iterator<Node> list_brands = result.columnAs("n");

==> the iterator becomes empty. This is strange for me, since the println() should only read, but not alter, the result object. Any explanation? 

And again, thanks in advance! 


Orion

unread,
Dec 30, 2012, 2:12:13 PM12/30/12
to ne...@googlegroups.com

Sorry ben, I didn't attention to your name! Thank you too for your reply!

Michael Hunger

unread,
Dec 30, 2012, 6:47:13 PM12/30/12
to ne...@googlegroups.com
The Cypher result is a lazy iterator, so if you empty it once (via toString()) then it is gone (doesn't keep up the memory).

So if you want to have debug output you have to do output the result-data while iterating yourself. That was changed lately as it caused issues keeping too much memory allocated for large query results.
I think Andres started work on accomodating a "debug" print-buffer but I'm not sure about the current state of that work. Which Neo4j version are you using?

HTH

Michael

--
 
 

Reply all
Reply to author
Forward
0 new messages