2017-07-25 19:10:31 +08:00
|
|
|
/*
|
|
|
|
|
* Copyright 2017 HugeGraph Authors
|
|
|
|
|
*
|
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with this
|
|
|
|
|
* work for additional information regarding copyright ownership. The ASF
|
|
|
|
|
* licenses this file to You under the Apache License, Version 2.0 (the
|
|
|
|
|
* "License"); you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2017-08-16 16:13:20 +08:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
|
* under the License.
|
2017-07-25 19:10:31 +08:00
|
|
|
*/
|
2017-08-17 16:26:01 +08:00
|
|
|
|
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-07-03 16:51:51 +08:00
|
|
|
import org.apache.tinkerpop.gremlin.process.traversal.P;
|
|
|
|
|
import org.apache.tinkerpop.gremlin.process.traversal.Path;
|
2017-04-21 21:07:21 +08:00
|
|
|
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
|
2017-08-31 22:28:45 +08:00
|
|
|
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
|
2017-07-03 16:51:51 +08:00
|
|
|
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
|
2017-04-21 21:07:21 +08:00
|
|
|
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 com.baidu.hugegraph.HugeGraph;
|
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-08-31 22:28:45 +08:00
|
|
|
import com.baidu.hugegraph.util.Log;
|
2017-04-21 21:07:21 +08:00
|
|
|
|
|
|
|
|
public class Example2 {
|
2017-07-31 20:17:00 +08:00
|
|
|
|
2017-08-17 13:12:04 +08:00
|
|
|
private static final Logger LOG = Log.logger(Example2.class);
|
2017-04-21 21:07:21 +08:00
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2017-08-17 13:12:04 +08:00
|
|
|
LOG.info("Example2 start!");
|
2017-06-13 13:47:15 +08:00
|
|
|
|
|
|
|
|
HugeGraph graph = ExampleUtil.loadGraph();
|
|
|
|
|
|
2017-04-21 21:07:21 +08:00
|
|
|
Example2.load(graph);
|
2017-05-09 10:59:50 +08:00
|
|
|
showSchema(graph);
|
2017-04-27 16:47:20 +08:00
|
|
|
traversal(graph);
|
2017-04-21 21:07:21 +08:00
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void traversal(final HugeGraph graph) {
|
|
|
|
|
|
2017-08-31 22:28:45 +08:00
|
|
|
GraphTraversalSource g = graph.traversal();
|
|
|
|
|
|
|
|
|
|
GraphTraversal<Vertex, Vertex> vertexs = g.V();
|
2017-07-31 20:17:00 +08:00
|
|
|
System.out.println(">>>> query all vertices: size=" +
|
|
|
|
|
vertexs.toList().size());
|
2017-04-27 16:47:20 +08:00
|
|
|
|
2017-08-31 22:28:45 +08:00
|
|
|
List<Edge> edges = g.E().toList();
|
2017-07-31 20:17:00 +08:00
|
|
|
System.out.println(">>>> query all edges: size=" +
|
2017-08-31 22:28:45 +08:00
|
|
|
edges.size());
|
2017-06-08 19:41:59 +08:00
|
|
|
|
2017-08-31 22:28:45 +08:00
|
|
|
List<Object> names = g.V().inE("knows").limit(2)
|
|
|
|
|
.outV().values("name").toList();
|
2017-07-31 20:17:00 +08:00
|
|
|
System.out.println(">>>> query vertex(with props) of edges: " + names);
|
2017-06-09 20:20:53 +08:00
|
|
|
assert names.size() == 2 : names.size();
|
|
|
|
|
|
2017-08-31 22:28:45 +08:00
|
|
|
names = g.V().as("a")
|
|
|
|
|
.out("knows")
|
|
|
|
|
.and()
|
|
|
|
|
.out("created").in("created").as("a").values("name")
|
|
|
|
|
.toList();
|
2017-06-09 20:20:53 +08:00
|
|
|
System.out.println(">>>> query with AND: " + names);
|
|
|
|
|
assert names.size() == 1 : names.size();
|
2017-07-03 16:51:51 +08:00
|
|
|
|
2017-08-31 22:28:45 +08:00
|
|
|
List<Vertex> vertex = g.V().has("age", 29).toList();
|
|
|
|
|
System.out.println(">>>> age = 29: " + vertex);
|
|
|
|
|
assert vertex.size() == 1 &&
|
|
|
|
|
vertex.get(0).value("name").equals("marko");
|
|
|
|
|
|
|
|
|
|
vertex = g.V()
|
|
|
|
|
.has("age", 29)
|
|
|
|
|
.has("city", "Beijing")
|
|
|
|
|
.toList();
|
|
|
|
|
System.out.println(">>>> age = 29 and city is Beijing: " + vertex);
|
|
|
|
|
assert vertex.size() == 1 &&
|
|
|
|
|
vertex.get(0).value("name").equals("marko");
|
|
|
|
|
|
|
|
|
|
edges = g.E().has("weight", P.lt(1.0)).toList();
|
|
|
|
|
System.out.println(">>>> edges with weight < 1.0: " + edges);
|
|
|
|
|
assert edges.size() == 4;
|
|
|
|
|
|
|
|
|
|
vertex = g.V("person:josh")
|
|
|
|
|
.bothE("created")
|
|
|
|
|
.has("weight", P.lt(1.0))
|
|
|
|
|
.otherV()
|
|
|
|
|
.toList();
|
|
|
|
|
System.out.println(">>>> josh's both edges with weight < 1.0: " +
|
|
|
|
|
vertex);
|
|
|
|
|
assert vertex.size() == 1 && vertex.get(0).value("name").equals("lop");
|
|
|
|
|
|
|
|
|
|
List<Path> paths = g.V("person:marko")
|
|
|
|
|
.out().out().path().by("name").toList();
|
2017-07-03 16:51:51 +08:00
|
|
|
System.out.println(">>>> test out path: " + paths);
|
|
|
|
|
assert paths.size() == 2;
|
|
|
|
|
assert paths.get(0).get(0).equals("marko");
|
|
|
|
|
assert paths.get(0).get(1).equals("josh");
|
|
|
|
|
assert paths.get(0).get(2).equals("lop");
|
|
|
|
|
assert paths.get(1).get(0).equals("marko");
|
|
|
|
|
assert paths.get(1).get(1).equals("josh");
|
|
|
|
|
assert paths.get(1).get(2).equals("ripple");
|
|
|
|
|
|
2017-07-21 19:38:33 +08:00
|
|
|
paths = shortestPath(graph, "person:marko", "software:lop", 5);
|
2017-07-03 16:51:51 +08:00
|
|
|
System.out.println(">>>> test shortest path: " + paths.get(0));
|
|
|
|
|
assert paths.get(0).get(0).equals("marko");
|
|
|
|
|
assert paths.get(0).get(1).equals("lop");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<Path> shortestPath(final HugeGraph graph,
|
2017-07-31 20:17:00 +08:00
|
|
|
Object from, Object to,
|
|
|
|
|
int maxDepth) {
|
2017-07-03 16:51:51 +08:00
|
|
|
GraphTraversal<Vertex, Path> t = graph.traversal()
|
|
|
|
|
.V(from)
|
|
|
|
|
.repeat(__.out().simplePath())
|
|
|
|
|
.until(__.hasId(to).or().loops().is(P.gt(maxDepth)))
|
|
|
|
|
.hasId(to)
|
|
|
|
|
.path().by("name")
|
|
|
|
|
.limit(1);
|
|
|
|
|
return t.toList();
|
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
|
|
|
|
2017-08-17 13:12:04 +08:00
|
|
|
LOG.info("=============== show schema ================");
|
2017-04-21 21:07:21 +08:00
|
|
|
|
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-07-31 20:17:00 +08:00
|
|
|
/*
|
2017-11-15 15:10:25 +08:00
|
|
|
* NOTE:
|
2017-07-31 21:26:38 +08:00
|
|
|
* Use schema.propertyKey interface to create propertyKey.
|
2017-04-27 16:47:20 +08:00
|
|
|
* Use schema.propertyKey interface to query propertyKey.
|
|
|
|
|
*/
|
2017-07-31 21:26:38 +08:00
|
|
|
schema.propertyKey("name").asText().ifNotExist().create();
|
|
|
|
|
schema.propertyKey("age").asInt().ifNotExist().create();
|
|
|
|
|
schema.propertyKey("city").asText().ifNotExist().create();
|
2017-08-31 22:28:45 +08:00
|
|
|
schema.propertyKey("weight").asDouble().ifNotExist().create();
|
2017-07-31 21:26:38 +08:00
|
|
|
schema.propertyKey("lang").asText().ifNotExist().create();
|
|
|
|
|
schema.propertyKey("date").asText().ifNotExist().create();
|
|
|
|
|
schema.propertyKey("price").asInt().ifNotExist().create();
|
|
|
|
|
|
2017-08-11 10:58:22 +08:00
|
|
|
schema.vertexLabel("person")
|
|
|
|
|
.properties("name", "age", "city")
|
|
|
|
|
.primaryKeys("name")
|
|
|
|
|
.ifNotExist()
|
|
|
|
|
.create();
|
|
|
|
|
|
|
|
|
|
schema.vertexLabel("software")
|
|
|
|
|
.properties("name", "lang", "price")
|
|
|
|
|
.primaryKeys("name")
|
|
|
|
|
.ifNotExist()
|
|
|
|
|
.create();
|
|
|
|
|
|
|
|
|
|
schema.indexLabel("personByName")
|
|
|
|
|
.onV("person")
|
|
|
|
|
.by("name")
|
|
|
|
|
.secondary()
|
|
|
|
|
.ifNotExist()
|
|
|
|
|
.create();
|
|
|
|
|
|
|
|
|
|
schema.indexLabel("personByCity")
|
|
|
|
|
.onV("person")
|
|
|
|
|
.by("city")
|
|
|
|
|
.secondary()
|
|
|
|
|
.ifNotExist()
|
|
|
|
|
.create();
|
|
|
|
|
|
2017-08-31 22:28:45 +08:00
|
|
|
schema.indexLabel("personByAgeAndCity")
|
2017-08-11 10:58:22 +08:00
|
|
|
.onV("person")
|
2017-08-31 22:28:45 +08:00
|
|
|
.by("age", "city")
|
2017-08-11 10:58:22 +08:00
|
|
|
.secondary()
|
|
|
|
|
.ifNotExist().create();
|
|
|
|
|
|
|
|
|
|
schema.indexLabel("softwareByPrice")
|
|
|
|
|
.onV("software")
|
|
|
|
|
.by("price")
|
|
|
|
|
.search()
|
|
|
|
|
.ifNotExist()
|
|
|
|
|
.create();
|
2017-05-09 18:22:32 +08:00
|
|
|
|
2017-07-31 21:26:38 +08:00
|
|
|
schema.edgeLabel("knows")
|
2017-08-11 10:58:22 +08:00
|
|
|
.sourceLabel("person")
|
|
|
|
|
.targetLabel("person")
|
2017-08-31 22:28:45 +08:00
|
|
|
.properties("date", "weight")
|
2017-08-11 10:58:22 +08:00
|
|
|
.ifNotExist()
|
|
|
|
|
.create();
|
2017-05-09 18:22:32 +08:00
|
|
|
|
2017-08-11 10:58:22 +08:00
|
|
|
schema.edgeLabel("created")
|
|
|
|
|
.sourceLabel("person").targetLabel("software")
|
2017-08-31 22:28:45 +08:00
|
|
|
.properties("date", "weight")
|
2017-08-11 10:58:22 +08:00
|
|
|
.ifNotExist()
|
|
|
|
|
.create();
|
|
|
|
|
|
|
|
|
|
schema.indexLabel("createdByDate")
|
|
|
|
|
.onE("created")
|
|
|
|
|
.by("date")
|
|
|
|
|
.secondary()
|
|
|
|
|
.ifNotExist()
|
|
|
|
|
.create();
|
|
|
|
|
|
2017-08-31 22:28:45 +08:00
|
|
|
schema.indexLabel("createdByWeight")
|
2017-08-11 10:58:22 +08:00
|
|
|
.onE("created")
|
2017-08-31 22:28:45 +08:00
|
|
|
.by("weight")
|
|
|
|
|
.search()
|
|
|
|
|
.ifNotExist()
|
|
|
|
|
.create();
|
|
|
|
|
|
|
|
|
|
schema.indexLabel("knowsByWeight")
|
|
|
|
|
.onE("knows")
|
|
|
|
|
.by("weight")
|
|
|
|
|
.search()
|
2017-08-11 10:58:22 +08:00
|
|
|
.ifNotExist()
|
|
|
|
|
.create();
|
2017-07-04 20:05:11 +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-08-11 10:58:22 +08:00
|
|
|
Vertex marko = graph.addVertex(T.label, "person", "name", "marko",
|
|
|
|
|
"age", 29, "city", "Beijing");
|
|
|
|
|
Vertex vadas = graph.addVertex(T.label, "person", "name", "vadas",
|
|
|
|
|
"age", 27, "city", "Hongkong");
|
|
|
|
|
Vertex lop = graph.addVertex(T.label, "software", "name", "lop",
|
|
|
|
|
"lang", "java", "price", 328);
|
|
|
|
|
Vertex josh = graph.addVertex(T.label, "person", "name", "josh",
|
|
|
|
|
"age", 32, "city", "Beijing");
|
|
|
|
|
Vertex ripple = graph.addVertex(T.label, "software", "name", "ripple",
|
|
|
|
|
"lang", "java", "price", 199);
|
|
|
|
|
Vertex peter = graph.addVertex(T.label, "person", "name", "peter",
|
2017-08-31 22:28:45 +08:00
|
|
|
"age", 35, "city", "Shanghai");
|
|
|
|
|
|
|
|
|
|
marko.addEdge("knows", vadas, "date", "20160110", "weight", 0.5);
|
|
|
|
|
marko.addEdge("knows", josh, "date", "20130220", "weight", 1.0);
|
|
|
|
|
marko.addEdge("created", lop, "date", "20171210", "weight", 0.4);
|
|
|
|
|
josh.addEdge("created", lop, "date", "20091111", "weight", 0.4);
|
|
|
|
|
josh.addEdge("created", ripple, "date", "20171210", "weight", 1.0);
|
|
|
|
|
peter.addEdge("created", lop, "date", "20170324", "weight", 0.2);
|
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
|
|
|
}
|
|
|
|
|
}
|