1

I'm writing a neo4j demo code in java. Following is a part of code where I'm creating nodes, indexing them and printing their properties.

String NODE_KEY = "UserID";
String NODE_PROPERTIES = "UserProperties";

createAndIndexUser(String userID){
    Node node = graphDB.createNode();
    node.setProperty(NODE_KEY, nodeID);
    neo4jGraph.nodeIndex.add(node, NODE_KEY, userID);
    return node;
}

for(int i=0 ; i<100 ; i++){
    String userID = "userID_"+i;
    Node node = createAndIndexUser(userID);
    node.setProperty(NODE_PROPERTIES, strNodeProperties);
}

Iterable<Node> allNodes = GlobalGraphOperations.at(graphDB).getAllNodes();

for(Node n: allNodes){
    System.out.println("n.getPropertyKeys: "+n.getPropertyKeys());
    System.out.println(n.getProperty(NODE_KEY));
}

When I execute this code, the output of first println is:

n.getPropertyKeys: []

whereas for second println I'm getting an error:

Exception in thread "main" org.neo4j.graphdb.NotFoundException: 'UserID' property not found for NodeImpl#0.

Where and what am I doing wrong? Why is it not printing all propert Keys on n.getProperty(NODE_KEY)?

Is there any other way for getting all nodes and printing their properties?

N D Thokare
  • 1,703
  • 6
  • 35
  • 57

2 Answers2

5

What other nodes are in your graph, besides the ones that you added in createAndIndexUser? Remember that neo4j graphs always have a dummy node with ID 0. Try modifying your loop to something like:

for(Node n: allNodes){
    System.out.println("n.getPropertyKeys: "+n.getPropertyKeys());
    if(n.hasProperty(NODE_KEY))
        System.out.println(n.getProperty(NODE_KEY));
    else
        System.out.println("Node " + n.getId() + " does not contain property " + NODE_KEY);
}

In fact, if you really want to be sure, you could keep a List of the Nodes that were created above, and check that against the ones that you get from getAllNodes().

Kricket
  • 4,049
  • 8
  • 33
  • 46
  • Thanks @KelseyRider . It worked. But what is that dummy nodes created for? Is it possible to avoid it? – N D Thokare Mar 14 '13 at 10:04
  • 1
    I think it's created automatically as a "reference node." You can delete it, but not re-create it. There's a lot of discussion around the net about it, e.g. http://stackoverflow.com/questions/7186832/recreate-reference-node-in-a-neo4j-database – Kricket Mar 14 '13 at 10:24
0

It's the reference node, which you can delete if you're not using it.

Also n.getProperty(NODE_KEY,null) is quite usable.

Mattias Finné
  • 3,034
  • 1
  • 15
  • 7