Here's an example of how to do this in Java.  The link method has the loop 
to create the edges.  I think the javascript will look very similar to the 
link method.


package orientdb.sql;

import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory;
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
import org.junit.Before;
import org.junit.Test;

public class LoopEdge {
    private OrientGraphFactory oGF;
    private OrientGraphNoTx graphNoTx;
    
    @Before
    public void before() {
        resetConnection();
    }
    
    @Test
    public void all() {
        dropDatabase();
        resetConnection();
        schema();
        data();
        link();
        select();
    }

    public void resetConnection() {
        if (oGF != null) {
            oGF.close();
        }
        oGF = new OrientGraphFactory("plocal:orientdb/loopedge");
        graphNoTx = oGF.getNoTx();
    }
    @Test
    public void schema() {
        graphNoTx.createVertexType("person");
        graphNoTx.createVertexType("post");
        graphNoTx.createEdgeType("Wrote");        
    }
    @Test
    public void dropDatabase() {
        graphNoTx.drop();
    }
    @Test
    public void data() {
        graphNoTx.addVertex("class:person", "name","John", "person_id", 1);
        graphNoTx.addVertex("class:post", "person_id", 1, "text", "hello");
        graphNoTx.addVertex("class:post", "person_id", 1, "text", "there");
        graphNoTx.addVertex("class:post", "person_id", 3, "text", "goodbye"
);
    }
    @Test
    public void link() {
        String sqlSelect = "select from person";
        Iterable<Vertex> persons = graphNoTx.command(new OCommandSQL(
sqlSelect)).execute();
        for (Vertex person : persons) {
            String rid = person.getId().toString();
            Integer id = person.getProperty("person_id");
            String sqlCreate = "create edge Wrote from " + rid + " to ( 
select from post where person_id = " + id + " )";
            System.out.format("SQL:%s%n", sqlCreate);
            graphNoTx.command(new OCommandSQL(sqlCreate)).execute("person", 
rid, "id", id);
        }
    }
    @Test
    public void select() {
        String sqlSelect = "select *, out('Wrote').size() as postCount from 
person";
        Iterable<Vertex> persons = graphNoTx.command(new OCommandSQL(
sqlSelect)).execute();
        for (Vertex person : persons) {
            System.out.format("name:%s person_id:%s postCount:%s%n", 
                    person.getProperty("name"), 
                    person.getProperty("person_id"),
                    person.getProperty("postCount"));
        }
    }
    
    public static void main(String[] args) {
        LoopEdge loopEdge = new LoopEdge();
        loopEdge.before();
        loopEdge.all();
    }
}



-- 

--- 
You received this message because you are subscribed to the Google Groups 
"OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to