图数据库OrientDB图Graph的Java增删改查操作

package com.zgd.orientdb;

import com.orientechnologies.orient.client.remote.OServerAdmin;
import com.orientechnologies.orient.core.command.script.OCommandScript;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.tinkerpop.blueprints.*;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory;
import com.tinkerpop.gremlin.groovy.Gremlin;
import com.tinkerpop.gremlin.java.GremlinPipeline;
import com.tinkerpop.pipes.Pipe;
import com.tinkerpop.pipes.util.iterators.SingleIterator;
import java.io.IOException;
import java.util.*;

/**
 * Created by zgd on 2015/3/20.
 */
public class TestTreeGraph {
    private static  OrientGraphFactory factory = new OrientGraphFactory("remote:localhost/mygraph
        getVertexEdge();
        excuteSql();
        createIndex();
        removeEdge();
        removeVertex();
    }

    private static void getVertexEdge(){

        OrientGraph graph = factory.getTx();
        //Pipe写法
        /*Pipe pipe = Gremlin.compile("_().out('haoyou').out('tongshi')");
        pipe.setStarts(new SingleIterator<Vertex>(graph.getVertexByKey("Person.pid","12321")));*/
        
        GremlinPipeline pipe = new GremlinPipeline();
        pipe.start(graph.getVertexByKey("Person.pid", "12319")).in("haoyou").property("name").order();//has("name", "person720").out("tongshi").range(1,9)
        
        System.out.println("数量为:"+pipe.count());
        Iterator iterator=pipe.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next().toString());
        }
        graph.shutdown();
    }

    private static void createNode(int time){
        final int everycount=100;
        Random random=new Random();
        OrientGraph graph = factory.getTx();

        Vertex [] persons=new Vertex[everycount];
        for(int i=0;i<everycount;i++){
            persons[i] = graph.addVertex("class:Person","pid",(time*everycount+i),"name","person"+(time*everycount+i),"age",new Random().nextInt(20),"city","BeiJing");
            if(i>0){
                for(int j=0;j<100;j++){
                    persons[i].addEdge("haoyou",persons[random.nextInt(i)]);
                }
            }
        }
        graph.commit();
        graph.shutdown();
    }
 
    private static void removeVertex(){
        OrientGraph graph = factory.getTx();
        Vertex luca = graph.addVertex(null);
        luca.setProperty("name","Luca");
        Vertex marko = graph.addVertex(null);
        luca.setProperty("name","Marko");
        graph.removeVertex(luca);
        graph.removeVertex(marko);
        graph.commit();
        graph.shutdown();
    }
    private static void excuteSql(){
        OrientGraph graph = factory.getTx();
        int modified = graph.command(new OCommandSQL("UPDATE Person SET name = 'lily' WHERE pid='12321'")).execute();

        System.out.println(modified);
        graph.commit();
        graph.getRawGraph().getMetadata().getSchema().reload();
        graph.shutdown();
    }
    private static void graphBatch(){
        OrientGraph graph = factory.getTx();
        String cmd = "begin\n";
        cmd += "let a = create vertex set script = true\n";
        cmd += "let b = select from v limit 1\n";
        cmd += "let e = create edge from $a to $b retry 100\n";
        cmd += "commit\n";
        cmd += "return $e";

        OIdentifiable edge = graph.command(new OCommandScript("sql", cmd)).execute();
        graph.shutdown();
    }

    public static void createIndex(){
        OrientGraph graph = factory.getTx();
        graph.createKeyIndex("pid", Vertex.class, new Parameter("type", "UNIQUE"), new Parameter("class", "Person"));
        graph.commit();
        graph.shutdown();
    }
    private static void createGraphDatabase(){
        OServerAdmin oServerAdmin = null;
        try {
            oServerAdmin = new OServerAdmin("remote:localhost");
            oServerAdmin.connect("root", "chinadog");
            oServerAdmin.createDatabase("medtree","graph","plocal");
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            oServerAdmin.close();
        }
    }
    private static void removeEdge(){
        OrientGraph graph = factory.getTx();
        Vertex luca = graph.addVertex(null);
        luca.setProperty("name", "Luca");
        Vertex marko = graph.addVertex(null);
        marko.setProperty("name", "Marko");
        Edge lucaKnowsMarko = graph.addEdge(null, luca, marko, "knows");
        graph.removeEdge(lucaKnowsMarko);
        graph.commit();
        graph.shutdown();
    }
}


你可能感兴趣的:(NoSQL,图数据库,orientdb)