Create Neo4j Unique nodes in java class -


i want create nodes , relatioships based on column values in db. node1 values come column "app-name",node2 -> "corresponding_app" & relationship->"interface_name" based on column values "interface_type_name"->incoming/outgoing arrows drawn particular nodes.

nodes should unique , if created should not create duplicate one.

transaction tx=graphdb.begintx();            connection conn = null;     try{{            conn=connectionfactory.getconnection();     statement stat=conn.createstatement();     string sql="select * fade app_int_id < 22";     resultset rs=stat.executequery(sql);             string n1 = "",n2="",rel="",type="";     while(rs.next()){         n1=rs.getstring(1);                       n2=rs.getstring(7);         rel=rs.getstring(3);             type=rs.getstring(4);                 node first=graphdb.createnode();     first.setproperty("name", n1);     first.addlabel(dynamiclabel.label(n1));      node sec=graphdb.createnode();     sec.setproperty("name", n2);     sec.addlabel(dynamiclabel.label(n2));             relationshiptype knows = dynamicrelationshiptype.withname(rel);      if(type.equalsignorecase("incoming")){         sec.createrelationshipto(first, knows);          }     else if(type.equalsignorecase("outgoing")){         first.createrelationshipto(sec, knows);          } 

for every single row creating new nodes.so how create unique nodes in above code?

if nodes unique respect names, can track them in simple hashmap reuse nodes instead of creating new. (warning, code not tested, should give idea).

hashmap<string,node> nodes = new hashmap<string,node>();  transaction tx=graphdb.begintx();            connection conn = null;     try{{            conn=connectionfactory.getconnection();     statement stat=conn.createstatement();     string sql="select * fade app_int_id < 22";     resultset rs=stat.executequery(sql);             string n1 = "",n2="",rel="",type="";     while(rs.next()){         n1=rs.getstring(1);                       n2=rs.getstring(7);         rel=rs.getstring(3);             type=rs.getstring(4);                 node first=nodes.get(n1);      // create if didn't exist.     if(first == null) {         first = graphdb.createnode();        first.setproperty("name", n1);        first.addlabel(dynamiclabel.label(n1));         // put in map ensure doesn't created again.        nodes.put(n1, first);     }       node sec=nodes.get(n2);       if(sec == null) {         sec = graphdb.createnode();        sec.setproperty("name", n2);        sec.addlabel(dynamiclabel.label(n2));                // put in map ensure doesn't created again.        nodes.put(n2, sec);     }       relationshiptype knows = dynamicrelationshiptype.withname(rel);      if(type.equalsignorecase("incoming")){         sec.createrelationshipto(first, knows);          }     else if(type.equalsignorecase("outgoing")){         first.createrelationshipto(sec, knows);          } 

finally - don't try if have massive number of nodes, since hashmap outgrow memory. should workable few thousand @ least. if have huge number, use lru cache, , make sure order results you're processing name.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -