参考:https://github.com/orientechnologies/orientdb-gremlin/tree/3.0.x/server/src/test/java/com/orientechnologies/tinkerpop
orientdb会基于gremlin框架风格进行功能扩展开发,但是这并不能用在移植上,所以一般应该会有使用纯gremlin开发和orintdb-gremlin两种。
一.构建图
使用orientdb-gremlin
import org.apache.tinkerpop.gremlin.orientdb.OrientGraph;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
public class TP3Test
{
public static void main(String[] args)
{
OrientGraph graph = OrientGraph.open("remote:192.168.1.32/test","root","xxx");
graph.createVertexClass("Person");
graph.createVertexClass("Animal");
graph.createEdgeClass("HasFriend");
graph.createEdgeClass("HasAnimal");
Vertex v1 = graph.addVertex(T.label, "Person", "name", "Jon");
Vertex v2 = graph.addVertex(T.label, "Person", "name", "Frank");
v1.addEdge("HasFriend", v2);
Vertex v3 = graph.addVertex(T.label, "Animal", "name", "Foo");
Vertex v4 = graph.addVertex(T.label, "Animal", "name", "Bar");
v1.addEdge("HasAnimal", v3, "marker", 10);
v2.addEdge("HasAnimal", v4);
}
}
使用纯gremlin
gremlin是不会有创建createVertexClass这样的方法提供的,因为底层的数据库可以随意切换,在orientdb中叫class,但在其它数据库中就未必了,所以构建图只会增加节点Vertex,边Edge,所以需要事先创建好class。
import org.apache.tinkerpop.gremlin.orientdb.OrientGraph;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Vertex;
public class TP3Test2
{
public static void main(String[] args)
{
OrientGraph graph = OrientGraph.open("remote:192.168.1.32/test","root","xxx");
//遍历对象
GraphTraversalSource gts = graph.traversal();
//构建图
Vertex v1 = gts.addV("person").property("name", "marko").property("age", 29).next();
Vertex v2 = gts.addV("software").property("name", "lop").property("lang", "java").next();
gts.addE("created").from(v1).to(v2).property("weight", 0.4);
}
}
二.遍历图
使用纯gremlin
import java.util.List;
import org.apache.tinkerpop.gremlin.orientdb.OrientGraphFactory;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
public class TP3Test4
{
public static void main(String[] args)
{
//创建图工厂
OrientGraphFactory factory = new OrientGraphFactory("remote:192.168.1.32/test","root","xxx");
//返回非事务图
Graph graph = factory.getNoTx();
//遍历对象
GraphTraversalSource g = graph.traversal();
try
{
//outE拿到边
// GraphTraversal<Vertex, Edge> gt = g.V().has("name","marko").outE("created");
//拿到顶点
// GraphTraversal<Vertex, Edge> gt = g.V().has("name","marko").outE("created").inV();
//直接拿created的对端顶点
GraphTraversal<Vertex, Vertex> gt = g.V().has("name","marko").out("created");
List<Vertex> vs = gt.toList();
for(Vertex v:vs)
{
// v.toString()
String name = v.value("name");
System.out.println(name);
System.out.println(v.toString());
}
//获取顶点的所有
// GraphTraversal<Vertex, Object> gt = g.V().has("name","marko").out("created").values("name");
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if(graph != null)
{
try {
graph.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
factory.close();
}
}
使用纯gremlin查询语句
import java.util.Iterator;
import org.apache.tinkerpop.gremlin.orientdb.OrientGraph;
import org.apache.tinkerpop.gremlin.orientdb.OrientGraphFactory;
import org.apache.tinkerpop.gremlin.orientdb.executor.OGremlinResult;
import org.apache.tinkerpop.gremlin.orientdb.executor.OGremlinResultSet;
import com.orientechnologies.orient.core.record.OVertex;
import com.orientechnologies.orient.core.sql.executor.OResult;
public class TP3Test3
{
public static void main(String[] args)
{
test3();
}
public static void test3()
{
//创建图工厂
OrientGraphFactory factory = new OrientGraphFactory("remote:192.168.1.32/test2","root","xxx");
// //返回事务图
// OrientGraph graph = factory.getTx();
//返回非事务图
OrientGraph graph = factory.getNoTx();
try
{
// OGremlinResultSet gremlinResultSet = graph.executeSql("select from Person");
OGremlinResultSet gremlinResultSet = graph.execute("gremlin", "g.V()", null);
Iterator<OGremlinResult> iterator = gremlinResultSet.iterator();
while(iterator.hasNext())
{
OGremlinResult gremlinResult = iterator.next();
OResult oresult = gremlinResult.getRawResult();
OVertex v = oresult.getVertex().get();
System.out.println(v.toJSON());
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if(graph != null)
{
graph.close();
}
}
factory.close();
}
}
三.相关pom
<!-- orientdb -->
<dependency>
<groupId>com.orientechnologies</groupId>
<artifactId>orientdb-server</artifactId>
<version>3.0.30</version>
</dependency>
<dependency>
<groupId>com.orientechnologies</groupId>
<artifactId>orientdb-graphdb</artifactId>
<version>3.0.30</version>
</dependency>
<dependency>
<groupId>com.orientechnologies</groupId>
<artifactId>orientdb-lucene</artifactId>
<version>3.0.30</version>
</dependency>
<dependency>
<groupId>com.orientechnologies</groupId>
<artifactId>orientdb-studio</artifactId>
<version>3.0.30</version>
<type>zip</type>
</dependency>
<dependency>
<groupId>com.orientechnologies</groupId>
<artifactId>orientdb-core</artifactId>
<version>3.0.30</version>
</dependency>
<dependency>
<groupId>com.orientechnologies</groupId>
<artifactId>orientdb-client</artifactId>
<version>3.0.30</version>
</dependency>
<dependency>
<groupId>com.orientechnologies</groupId>
<artifactId>orientdb-distributed</artifactId>
<version>3.0.30</version>
</dependency>
<dependency>
<groupId>com.orientechnologies</groupId>
<artifactId>orientdb-gremlin</artifactId>
<version>3.0.30</version>
</dependency>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-core</artifactId>
<version>3.3.10</version>
</dependency>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-groovy</artifactId>
<version>3.3.10</version>
</dependency>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-console</artifactId>
<version>3.3.10</version>
</dependency>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-driver</artifactId>
<version>3.3.10</version>
</dependency>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>tinkergraph-gremlin</artifactId>
<version>3.3.10</version>
</dependency>