neo4j如何设置除setproperty以外的节点ID [英] neo4j how to set the node id other than setproperty

查看:137
本文介绍了neo4j如何设置除setproperty以外的节点ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将节点ID设置为0、1、2、3等.而不是setProperty().

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());
}

它显示以下大ID号:

996660
996661
996662
996663
996656
996657
996658
996659
996668
996669
996670

例如,我想使节点为newno [i]的ID仅为i. 对于大型数据集,如果每次调用getProperty(),程序都将花费很长时间. 谢谢

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

推荐答案

@RaduK指出,neo4j设置节点本身的ID,并且不允许用户覆盖此行为.也许他们将来会做些事情.但是,如果您正在考虑在系统中使用neo4j生成的id,则neo4j会在其页面上发出警告.

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.

节点的ID是唯一的,但请注意以下几点:Neo4j重用其节点 删除节点和关系时的内部ID,这意味着 以这种方式引用它们是一种不好的做法.而是使用应用程序 生成的ID.

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.

因此,请勿在系统中使用neo4j的ID.

So don't use the neo4j's id in your system.

话虽这么说,您最担心的是每次使用属性都会导致延迟.使用Neo4J 2.x,它们使您可以选择在属性上设置索引,以便快速遍历.这是您的操作方式.在您具有GraphDatabaseService对象的代码中,插入以下代码:

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();
        }

此代码将告诉neo4j,从现在开始,您必须在属性"internal_id"上所有类型为用户"的节点上建立并维护索引.

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".

此外,neo4j有一个内部缓存系统,人们通常将其称为热缓存.这意味着您的查询只会在第一次运行时花费时间.从那时起,如果您重复尝试相同的查询,该查询将迅速发展.

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.

这篇关于neo4j如何设置除setproperty以外的节点ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆