2017-04-21 21:07:21 +08:00
|
|
|
package com.baidu.hugegraph.example;
|
|
|
|
|
|
2017-05-09 10:59:50 +08:00
|
|
|
import java.util.List;
|
|
|
|
|
|
2017-04-21 21:07:21 +08:00
|
|
|
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
|
|
|
|
|
import org.apache.tinkerpop.gremlin.structure.Edge;
|
|
|
|
|
import org.apache.tinkerpop.gremlin.structure.T;
|
|
|
|
|
import org.apache.tinkerpop.gremlin.structure.Vertex;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import com.baidu.hugegraph.HugeFactory;
|
|
|
|
|
import com.baidu.hugegraph.HugeGraph;
|
2017-05-27 10:29:30 +08:00
|
|
|
import com.baidu.hugegraph.dist.RegisterUtil;
|
2017-05-09 10:59:50 +08:00
|
|
|
import com.baidu.hugegraph.schema.SchemaElement;
|
2017-04-21 21:07:21 +08:00
|
|
|
import com.baidu.hugegraph.schema.SchemaManager;
|
2017-05-09 18:22:32 +08:00
|
|
|
import com.baidu.hugegraph.type.schema.EdgeLabel;
|
|
|
|
|
import com.baidu.hugegraph.type.schema.VertexLabel;
|
2017-04-21 21:07:21 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Created by jishilei on 2017/4/2.
|
|
|
|
|
*/
|
|
|
|
|
public class Example2 {
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(Example2.class);
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
|
|
logger.info("Example2 start!");
|
2017-05-27 10:29:30 +08:00
|
|
|
RegisterUtil.registerCore();
|
2017-05-11 14:34:51 +08:00
|
|
|
RegisterUtil.registerCassandra();
|
|
|
|
|
|
2017-05-27 10:29:30 +08:00
|
|
|
String confFile = Example2.class.getClassLoader()
|
|
|
|
|
.getResource("hugegraph.properties").getPath();
|
2017-04-21 21:07:21 +08:00
|
|
|
HugeGraph graph = HugeFactory.open(confFile);
|
|
|
|
|
graph.clearBackend();
|
|
|
|
|
graph.initBackend();
|
|
|
|
|
Example2.load(graph);
|
2017-05-09 10:59:50 +08:00
|
|
|
showSchema(graph);
|
2017-04-27 16:47:20 +08:00
|
|
|
traversal(graph);
|
2017-05-09 10:59:50 +08:00
|
|
|
|
2017-04-21 21:07:21 +08:00
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void traversal(final HugeGraph graph) {
|
|
|
|
|
|
2017-04-27 16:47:20 +08:00
|
|
|
// query all
|
|
|
|
|
GraphTraversal<Vertex, Vertex> vertexs = graph.traversal().V();
|
2017-05-27 10:29:30 +08:00
|
|
|
System.out.println(
|
|
|
|
|
">>>> query all vertices: size=" + vertexs.toList().size());
|
2017-04-27 16:47:20 +08:00
|
|
|
|
|
|
|
|
GraphTraversal<Edge, Edge> edges = graph.traversal().E();
|
2017-05-27 10:29:30 +08:00
|
|
|
System.out
|
|
|
|
|
.println(">>>> query all edges: size=" + edges.toList().size());
|
2017-04-27 16:47:20 +08:00
|
|
|
|
2017-05-09 10:59:50 +08:00
|
|
|
// // query vertex by id
|
|
|
|
|
// vertex = graph.traversal().V("author\u00021");
|
2017-05-27 10:29:30 +08:00
|
|
|
// GraphTraversal<Vertex, Edge> edgesOfVertex = vertex.outE
|
|
|
|
|
// ("created");
|
|
|
|
|
// System.out.println(">>>> query edges of vertex: " +
|
|
|
|
|
// edgesOfVertex.toList());
|
2017-05-09 10:59:50 +08:00
|
|
|
//
|
|
|
|
|
// vertex = graph.traversal().V("author\u00021");
|
2017-05-27 10:29:30 +08:00
|
|
|
// GraphTraversal<Vertex, Vertex> verticesOfVertex = vertex
|
|
|
|
|
// .out("created");
|
|
|
|
|
// System.out.println(">>>> query vertices of vertex: " +
|
|
|
|
|
// verticesOfVertex.toList());
|
2017-05-09 10:59:50 +08:00
|
|
|
//
|
|
|
|
|
// // query edge by condition
|
|
|
|
|
// ConditionQuery q = new ConditionQuery(HugeTypes.VERTEX);
|
2017-05-27 10:29:30 +08:00
|
|
|
// q.query(IdGeneratorFactory.generator().generate
|
|
|
|
|
// ("author\u00021"));
|
2017-05-09 10:59:50 +08:00
|
|
|
// q.eq(HugeKeys.PROPERTY_KEY, "age");
|
|
|
|
|
//
|
|
|
|
|
// Iterator<Vertex> vertices = graph.vertices(q);
|
2017-05-27 10:29:30 +08:00
|
|
|
// System.out.println(">>>> queryVertices(): " + vertices
|
|
|
|
|
// .hasNext());
|
2017-05-09 10:59:50 +08:00
|
|
|
// while (vertices.hasNext()) {
|
|
|
|
|
// System.out.println(">>>> " + vertices.next().toString());
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// // query edge by id
|
2017-05-27 10:29:30 +08:00
|
|
|
// String id =
|
|
|
|
|
// "author\u00021\u0001OUT\u0001authored\u0001\u0001book\u0002java-2";
|
2017-05-09 10:59:50 +08:00
|
|
|
// GraphTraversal<Edge, Edge> edges = graph.traversal().E(id);
|
|
|
|
|
// System.out.println(">>>> query edge: " + edges.toList());
|
|
|
|
|
//
|
|
|
|
|
// // query edge by condition
|
|
|
|
|
// q = new ConditionQuery(HugeTypes.EDGE);
|
|
|
|
|
// q.eq(HugeKeys.SOURCE_VERTEX, "author\u00021");
|
|
|
|
|
// q.eq(HugeKeys.DIRECTION, Direction.OUT.name());
|
|
|
|
|
// q.eq(HugeKeys.LABEL, "authored");
|
|
|
|
|
// q.eq(HugeKeys.SORT_VALUES, "");
|
|
|
|
|
// q.eq(HugeKeys.TARGET_VERTEX, "book\u0002java-1");
|
|
|
|
|
// q.eq(HugeKeys.PROPERTY_KEY, "contribution");
|
|
|
|
|
//
|
|
|
|
|
// Iterator<Edge> edges2 = graph.edges(q);
|
|
|
|
|
// System.out.println(">>>> queryEdges(): " + edges2.hasNext());
|
|
|
|
|
// while (edges2.hasNext()) {
|
|
|
|
|
// System.out.println(">>>> " + edges2.next().toString());
|
|
|
|
|
// }
|
2017-04-27 16:47:20 +08:00
|
|
|
|
|
|
|
|
// query by vertex label
|
2017-05-09 10:59:50 +08:00
|
|
|
// vertex = graph.traversal().V().hasLabel("book");
|
2017-05-27 10:29:30 +08:00
|
|
|
//System.out.println(">>>> query all books: size=" + vertex.toList()
|
|
|
|
|
// .size());
|
2017-04-21 21:07:21 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void showSchema(final HugeGraph graph) {
|
2017-04-26 14:10:42 +08:00
|
|
|
SchemaManager schemaManager = graph.schema();
|
2017-04-21 21:07:21 +08:00
|
|
|
|
|
|
|
|
logger.info("=============== show schema ================");
|
|
|
|
|
|
2017-05-09 10:59:50 +08:00
|
|
|
List<SchemaElement> elements = schemaManager.desc();
|
|
|
|
|
for (SchemaElement element : elements) {
|
|
|
|
|
System.out.println(element.schema());
|
|
|
|
|
}
|
2017-04-21 21:07:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void load(final HugeGraph graph) {
|
2017-04-26 14:10:42 +08:00
|
|
|
SchemaManager schema = graph.schema();
|
2017-04-21 21:07:21 +08:00
|
|
|
|
2017-04-27 16:47:20 +08:00
|
|
|
/**
|
|
|
|
|
* Note:
|
|
|
|
|
* Use schema.makePropertyKey interface to create propertyKey.
|
|
|
|
|
* Use schema.propertyKey interface to query propertyKey.
|
|
|
|
|
*/
|
2017-05-11 11:23:15 +08:00
|
|
|
schema.makePropertyKey("name").asText().ifNotExist().create();
|
|
|
|
|
schema.makePropertyKey("age").asInt().ifNotExist().create();
|
|
|
|
|
schema.makePropertyKey("lang").asText().ifNotExist().create();
|
|
|
|
|
schema.makePropertyKey("date").asText().ifNotExist().create();
|
2017-05-17 10:53:05 +08:00
|
|
|
schema.makePropertyKey("price").asInt().ifNotExist().create();
|
2017-04-27 16:47:20 +08:00
|
|
|
|
2017-05-09 18:22:32 +08:00
|
|
|
VertexLabel person = schema.makeVertexLabel("person")
|
|
|
|
|
.properties("name", "age")
|
|
|
|
|
.primaryKeys("name")
|
2017-05-11 11:23:15 +08:00
|
|
|
.ifNotExist()
|
2017-05-09 18:22:32 +08:00
|
|
|
.create();
|
2017-04-26 20:25:50 +08:00
|
|
|
|
2017-05-09 18:22:32 +08:00
|
|
|
VertexLabel software = schema.makeVertexLabel("software")
|
2017-05-17 10:53:05 +08:00
|
|
|
.properties("name", "lang", "price")
|
2017-05-09 18:22:32 +08:00
|
|
|
.primaryKeys("name")
|
2017-05-11 11:23:15 +08:00
|
|
|
.ifNotExist()
|
2017-05-09 18:22:32 +08:00
|
|
|
.create();
|
2017-04-21 21:07:21 +08:00
|
|
|
|
2017-05-27 10:29:30 +08:00
|
|
|
schema.makeIndex("personByName").on(person).by("name").secondary()
|
|
|
|
|
.ifNotExist().create();
|
2017-05-09 18:22:32 +08:00
|
|
|
|
2017-05-27 10:29:30 +08:00
|
|
|
schema.makeIndex("softwareByPrice").on(software).by("price").search()
|
|
|
|
|
.ifNotExist().create();
|
|
|
|
|
// schema.makeIndex("softwareByLang").on(software).by("lang")
|
|
|
|
|
// .search().ifNotExist().create();
|
2017-05-09 18:22:32 +08:00
|
|
|
|
2017-05-27 10:29:30 +08:00
|
|
|
schema.makeEdgeLabel("knows").link("person", "person")
|
|
|
|
|
.properties("date").create();
|
2017-05-09 18:22:32 +08:00
|
|
|
|
|
|
|
|
EdgeLabel created = schema.makeEdgeLabel("created")
|
|
|
|
|
.link("person", "software")
|
|
|
|
|
.properties("date")
|
2017-05-11 11:23:15 +08:00
|
|
|
.ifNotExist()
|
2017-05-09 18:22:32 +08:00
|
|
|
.create();
|
|
|
|
|
|
|
|
|
|
schema.makeIndex("createdByDate").on(created).by("date").secondary()
|
2017-05-11 11:23:15 +08:00
|
|
|
.ifNotExist()
|
2017-05-09 18:22:32 +08:00
|
|
|
.create();
|
2017-04-21 21:07:21 +08:00
|
|
|
|
2017-05-03 13:01:21 +08:00
|
|
|
schema.desc();
|
2017-04-27 16:47:20 +08:00
|
|
|
|
2017-04-26 18:31:37 +08:00
|
|
|
graph.tx().open();
|
2017-04-28 15:29:54 +08:00
|
|
|
|
2017-05-27 10:29:30 +08:00
|
|
|
Vertex marko = graph.addVertex(T.label, "person",
|
|
|
|
|
"name", "marko", "age", 29);
|
|
|
|
|
Vertex vadas = graph.addVertex(T.label, "person",
|
|
|
|
|
"name", "vadas", "age", 27);
|
|
|
|
|
Vertex lop = graph.addVertex(T.label, "software",
|
|
|
|
|
"name", "lop", "lang", "java", "price", 328);
|
|
|
|
|
Vertex josh = graph.addVertex(T.label, "person",
|
|
|
|
|
"name", "josh", "age", 32);
|
|
|
|
|
Vertex ripple = graph.addVertex(T.label, "software",
|
|
|
|
|
"name", "ripple", "lang", "java", "price", 199);
|
|
|
|
|
Vertex peter = graph.addVertex(T.label, "person",
|
|
|
|
|
"name", "peter", "age", 35);
|
2017-04-26 14:10:42 +08:00
|
|
|
|
2017-04-26 20:25:50 +08:00
|
|
|
marko.addEdge("knows", vadas, "date", "20160110");
|
|
|
|
|
marko.addEdge("knows", josh, "date", "20130220");
|
|
|
|
|
marko.addEdge("created", lop, "date", "20171210");
|
|
|
|
|
josh.addEdge("created", ripple, "date", "20171210");
|
|
|
|
|
josh.addEdge("created", lop, "date", "20091111");
|
|
|
|
|
peter.addEdge("created", lop, "date", "20170324");
|
2017-04-25 20:08:57 +08:00
|
|
|
|
2017-04-26 14:10:42 +08:00
|
|
|
graph.tx().commit();
|
2017-04-21 21:07:21 +08:00
|
|
|
}
|
|
|
|
|
}
|