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?
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: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()
.