问题
I want to set the node id like 0,1,2,3,.... instead of setProperty().
Node[] newno=new Node[265214];
for(int i=0; i<newno.length; i++){
newno[i]=db.createNode();
System.out.println(newno[i].getId());
}
it show the following big id number:
996660
996661
996662
996663
996656
996657
996658
996659
996668
996669
996670
while i want to make node for example the id of newno[i] is just i. for the large data set, if every time call getProperty() it will take a long time for the program. Thanks
回答1:
The ids are automatically assigned by Neo4j, you cannot change this. Try using a property on the node if this is what you need.
newno[i].setProperty("internal_id", i);
回答2:
As pointed by @RaduK, neo4j sets the id of the node itself and does not allows the user to override this behaviour. Maybe something that they might do in future. However, if you are considering to use the neo4j generated id in your system, neo4j issued a warning on their page.
A node's id is unique, but note the following: Neo4j reuses its internal ids when nodes and relationships are deleted, which means it's bad practice to refer to them this way. Instead, use application generated ids.
So don't use the neo4j's id in your system.
That being said, you are mostly concerned about the fact that using property every time will result in delays. With Neo4J 2.x, they give you the option to set an index on your property so that the traversal is fast. Here is how you do that. Inside your code where you have the object of GraphDatabaseService, insert this piece of code:
try(Transaction transaction = graphDatabaseService.beginTx())
{
Schema schema = graphDatabaseService.schema();
schema.indexFor(DynamicLabel.label("User")).on("internal_id").create();
transaction.success();
}
This code will tell neo4j that from now on, you have to make and maintain index on all nodes that are of type "User" on the property "internal_id".
Moreover, neo4j has an internal caching system which people usually refer to as warm cache. It means that your query will only take time the first time it runs. From then onwards, if you try the same query repeatedly, the query will be blazing fast.
来源:https://stackoverflow.com/questions/23281888/neo4j-how-to-set-the-node-id-other-than-setproperty