2
votes

I want to create nodes and relatioships based on column values in DB. Node1 values will come from column "App-Name",Node2 -> "Corresponding_App" & relationship->"Interface_Name" Based on column values "Interface_Type_Name"->Incoming/Outgoing arrows will be drawn to particular nodes.

nodes should be unique and if created already then should not create duplicate one.

Transaction tx=graphDb.beginTx();       
    Connection conn = null;
    try{{       
    conn=ConnectionFactory.getConnection();
    Statement stat=conn.createStatement();
    String sql="select * from Fade where 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 its creating new nodes.so How to create unique nodes in the above code?

1

1 Answers

1
votes

If your nodes are unique with respect to their names, you can track them in a simple HashMap to reuse nodes instead of creating new. Something like this (warning, code not tested, but this should give you the 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 * from Fade where 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);

    // Only create it if it didn't already exist.
    if(first == null) { 
       first = graphDb.createNode();
       first.setProperty("name", n1);
       first.addLabel(DynamicLabel.label(n1));

       // Now put it in the map to ensure it doesn't get 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));       

       // Now put it in the map to ensure it doesn't get 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 to do this if you have a massive number of nodes, since your HashMap will outgrow memory. But this should be workable for a few thousand at least. If you have a really huge number, use an LRU cache, and then make sure that you order to the results you're processing by name.