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
|
|
|
|
|
* License for the specific language governing permissions and limitations under
|
|
|
|
|
* the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-04-18 17:09:14 +08:00
|
|
|
package com.baidu.hugegraph.example;
|
2017-02-24 18:01:51 +08:00
|
|
|
|
2017-04-24 21:23:30 +08:00
|
|
|
import java.util.Iterator;
|
2017-05-11 14:06:29 +08:00
|
|
|
import java.util.List;
|
2017-04-24 21:23:30 +08:00
|
|
|
|
2017-04-27 10:41:17 +08:00
|
|
|
import org.apache.tinkerpop.gremlin.process.traversal.P;
|
2017-03-31 14:36:52 +08:00
|
|
|
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
|
2017-04-24 21:23:30 +08:00
|
|
|
import org.apache.tinkerpop.gremlin.structure.Direction;
|
2017-03-31 20:48:13 +08:00
|
|
|
import org.apache.tinkerpop.gremlin.structure.Edge;
|
2017-03-21 19:56:21 +08:00
|
|
|
import org.apache.tinkerpop.gremlin.structure.T;
|
2017-03-28 18:32:13 +08:00
|
|
|
import org.apache.tinkerpop.gremlin.structure.Vertex;
|
2017-02-24 18:01:51 +08:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2017-04-18 17:09:14 +08:00
|
|
|
import com.baidu.hugegraph.HugeGraph;
|
|
|
|
|
import com.baidu.hugegraph.backend.BackendException;
|
2017-08-04 19:48:57 +08:00
|
|
|
import com.baidu.hugegraph.backend.id.IdGenerator;
|
2017-04-24 21:23:30 +08:00
|
|
|
import com.baidu.hugegraph.backend.query.ConditionQuery;
|
2017-04-18 17:09:14 +08:00
|
|
|
import com.baidu.hugegraph.backend.tx.GraphTransaction;
|
2017-06-08 19:41:59 +08:00
|
|
|
import com.baidu.hugegraph.exception.NotFoundException;
|
2017-07-31 21:26:38 +08:00
|
|
|
import com.baidu.hugegraph.schema.IndexLabel;
|
2017-04-18 17:09:14 +08:00
|
|
|
import com.baidu.hugegraph.schema.SchemaManager;
|
2017-07-31 21:26:38 +08:00
|
|
|
import com.baidu.hugegraph.schema.VertexLabel;
|
2017-05-09 11:36:13 +08:00
|
|
|
import com.baidu.hugegraph.type.HugeType;
|
2017-04-24 21:23:30 +08:00
|
|
|
import com.baidu.hugegraph.type.define.HugeKeys;
|
2017-02-24 18:01:51 +08:00
|
|
|
|
2017-04-28 15:29:54 +08:00
|
|
|
public class Example1 {
|
2017-02-24 18:01:51 +08:00
|
|
|
|
2017-04-28 15:29:54 +08:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(Example1.class);
|
2017-02-24 18:01:51 +08:00
|
|
|
|
2017-06-01 18:04:08 +08:00
|
|
|
public static void main(String[] args) throws InterruptedException {
|
2017-04-28 15:29:54 +08:00
|
|
|
logger.info("Example1 start!");
|
2017-02-24 18:01:51 +08:00
|
|
|
|
2017-06-13 13:47:15 +08:00
|
|
|
HugeGraph graph = ExampleUtil.loadGraph();
|
2017-03-21 19:56:21 +08:00
|
|
|
|
2017-04-28 15:29:54 +08:00
|
|
|
Example1.showFeatures(graph);
|
|
|
|
|
Example1.load(graph);
|
2017-06-01 18:04:08 +08:00
|
|
|
|
|
|
|
|
Example1.thread(graph);
|
|
|
|
|
|
|
|
|
|
graph.close();
|
2017-04-28 15:29:54 +08:00
|
|
|
System.exit(0);
|
2017-02-24 18:01:51 +08:00
|
|
|
}
|
|
|
|
|
|
2017-06-01 18:04:08 +08:00
|
|
|
private static void thread(HugeGraph graph) throws InterruptedException {
|
|
|
|
|
Thread t = new Thread(() -> {
|
|
|
|
|
// TODO: add graph.initTx()
|
|
|
|
|
graph.addVertex(T.label, "book", "name", "java-11");
|
|
|
|
|
graph.addVertex(T.label, "book", "name", "java-12");
|
|
|
|
|
// TODO: add graph.destroyTx()
|
|
|
|
|
graph.close(); // close current thread tx/store
|
|
|
|
|
|
|
|
|
|
GraphTransaction tx = graph.openTransaction();
|
|
|
|
|
tx.addVertex(T.label, "book", "name", "java-21");
|
|
|
|
|
tx.addVertex(T.label, "book", "name", "java-22");
|
|
|
|
|
tx.close(); // this will cause the schema tx not to be closed!
|
|
|
|
|
graph.close(); // this will close the schema tx
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
t.start();
|
|
|
|
|
t.join();
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-21 21:37:49 +08:00
|
|
|
public static void showFeatures(final HugeGraph graph) {
|
2017-07-31 20:17:00 +08:00
|
|
|
logger.info("SupportsPersistence: {}",
|
|
|
|
|
graph.features().graph().supportsPersistence());
|
2017-03-21 21:35:47 +08:00
|
|
|
}
|
2017-03-21 21:37:49 +08:00
|
|
|
|
2017-03-23 13:55:35 +08:00
|
|
|
public static void load(final HugeGraph graph) {
|
2017-03-21 19:56:21 +08:00
|
|
|
|
2017-07-31 20:17:00 +08:00
|
|
|
/*********************** schemaManager operating *********************/
|
2017-04-27 10:41:17 +08:00
|
|
|
SchemaManager schema = graph.schema();
|
2017-03-28 11:30:27 +08:00
|
|
|
logger.info("=============== propertyKey ================");
|
2017-07-31 21:26:38 +08:00
|
|
|
schema.propertyKey("id").asInt().create();
|
|
|
|
|
schema.propertyKey("~exist").asText().create();
|
|
|
|
|
schema.propertyKey("name").asText().create();
|
|
|
|
|
schema.propertyKey("gender").asText().create();
|
|
|
|
|
schema.propertyKey("instructions").asText().create();
|
|
|
|
|
schema.propertyKey("category").asText().create();
|
|
|
|
|
schema.propertyKey("year").asInt().create();
|
|
|
|
|
schema.propertyKey("time").asText().create();
|
|
|
|
|
schema.propertyKey("timestamp").asTimestamp().create();
|
|
|
|
|
schema.propertyKey("ISBN").asText().create();
|
|
|
|
|
schema.propertyKey("calories").asInt().create();
|
|
|
|
|
schema.propertyKey("amount").asText().create();
|
|
|
|
|
schema.propertyKey("stars").asInt().create();
|
|
|
|
|
schema.propertyKey("age").asInt().valueSingle().create();
|
|
|
|
|
schema.propertyKey("comment").asText().valueSet().create();
|
|
|
|
|
schema.propertyKey("contribution").asText().valueSet().create();
|
|
|
|
|
schema.propertyKey("nickname").asText().valueList().create();
|
|
|
|
|
schema.propertyKey("lived").asText().create();
|
|
|
|
|
schema.propertyKey("country").asText().valueSet().create();
|
|
|
|
|
schema.propertyKey("country").asText().valueSet().create();
|
|
|
|
|
schema.propertyKey("city").asText().create();
|
|
|
|
|
schema.propertyKey("sensor_id").asUuid().create();
|
2017-03-21 11:09:22 +08:00
|
|
|
|
2017-03-28 11:30:27 +08:00
|
|
|
logger.info("=============== vertexLabel ================");
|
2017-03-21 16:26:37 +08:00
|
|
|
|
2017-07-31 21:26:38 +08:00
|
|
|
VertexLabel person = schema.vertexLabel("person")
|
2017-05-09 18:22:32 +08:00
|
|
|
.properties("name", "age", "city")
|
|
|
|
|
.primaryKeys("name")
|
|
|
|
|
.create();
|
2017-07-31 21:26:38 +08:00
|
|
|
schema.vertexLabel("author")
|
2017-07-31 20:17:00 +08:00
|
|
|
.properties("id", "name", "age", "lived")
|
|
|
|
|
.primaryKeys("id").create();
|
2017-07-31 21:26:38 +08:00
|
|
|
schema.vertexLabel("language").properties("name")
|
2017-07-31 20:17:00 +08:00
|
|
|
.primaryKeys("name").create();
|
2017-07-31 21:26:38 +08:00
|
|
|
schema.vertexLabel("recipe").properties("name", "instructions")
|
2017-07-31 20:17:00 +08:00
|
|
|
.primaryKeys("name").create();
|
2017-07-31 21:26:38 +08:00
|
|
|
schema.vertexLabel("book").properties("name")
|
2017-07-31 20:17:00 +08:00
|
|
|
.primaryKeys("name").create();
|
2017-07-31 21:26:38 +08:00
|
|
|
schema.vertexLabel("reviewer").properties("name")
|
2017-07-31 20:17:00 +08:00
|
|
|
.primaryKeys("name").create();
|
2017-03-28 11:30:27 +08:00
|
|
|
// vertex label must have the properties that specified in primary key
|
2017-07-31 21:26:38 +08:00
|
|
|
schema.vertexLabel("FridgeSensor").properties("city")
|
2017-07-31 20:17:00 +08:00
|
|
|
.primaryKeys("city").create();
|
2017-03-21 19:56:21 +08:00
|
|
|
|
2017-03-28 11:30:27 +08:00
|
|
|
logger.info("=============== vertexLabel & index ================");
|
2017-07-31 21:26:38 +08:00
|
|
|
IndexLabel personByCity = schema.indexLabel("personByCity")
|
|
|
|
|
.onV("person").secondary().by("city").create();
|
|
|
|
|
IndexLabel personByAge = schema.indexLabel("personByAge")
|
|
|
|
|
.onV("person").search().by("age").create();
|
|
|
|
|
// schemaManager.getVertexLabel("author").index("byName").secondary().by("name").add();
|
|
|
|
|
// schemaManager.getVertexLabel("recipe").index("byRecipe").materialized().by("name").add();
|
|
|
|
|
// schemaManager.getVertexLabel("meal").index("byMeal").materialized().by("name").add();
|
|
|
|
|
// schemaManager.getVertexLabel("ingredient").index("byIngredient").materialized().by("name").add();
|
|
|
|
|
// schemaManager.getVertexLabel("reviewer").index("byReviewer").materialized().by("name").add();
|
2017-03-21 11:09:22 +08:00
|
|
|
|
2017-03-28 11:30:27 +08:00
|
|
|
logger.info("=============== edgeLabel ================");
|
2017-03-21 19:56:21 +08:00
|
|
|
|
2017-07-31 21:26:38 +08:00
|
|
|
schema.edgeLabel("authored").singleTime()
|
|
|
|
|
.sourceLabel("author").targetLabel("book")
|
|
|
|
|
.properties("contribution", "comment")
|
2017-05-15 16:27:13 +08:00
|
|
|
.create();
|
|
|
|
|
|
2017-07-31 21:26:38 +08:00
|
|
|
schema.edgeLabel("look").multiTimes().properties("time")
|
|
|
|
|
.sourceLabel("author").targetLabel("book")
|
2017-05-15 16:27:13 +08:00
|
|
|
.sortKeys("time")
|
|
|
|
|
.create();
|
|
|
|
|
|
2017-07-31 21:26:38 +08:00
|
|
|
schema.edgeLabel("created").singleTime()
|
|
|
|
|
.sourceLabel("author").targetLabel("language")
|
2017-05-15 16:27:13 +08:00
|
|
|
.create();
|
|
|
|
|
|
2017-07-31 21:26:38 +08:00
|
|
|
schema.edgeLabel("rated")
|
|
|
|
|
.sourceLabel("reviewer").targetLabel("recipe")
|
|
|
|
|
.create();
|
2017-03-21 19:56:21 +08:00
|
|
|
|
2017-03-28 15:47:52 +08:00
|
|
|
logger.info("=============== schemaManager desc ================");
|
2017-05-15 16:27:13 +08:00
|
|
|
schema.desc().forEach(element -> System.out.println(element.schema()));
|
2017-03-22 21:19:56 +08:00
|
|
|
|
2017-03-21 19:56:21 +08:00
|
|
|
/************************* data operating *************************/
|
|
|
|
|
|
2017-03-28 15:47:52 +08:00
|
|
|
// Directly into the back-end
|
2017-04-26 14:10:42 +08:00
|
|
|
graph.addVertex(T.label, "book", "name", "java-3");
|
2017-04-27 10:41:17 +08:00
|
|
|
|
2017-05-15 19:46:35 +08:00
|
|
|
graph.addVertex(T.label, "person", "name", "Baby",
|
|
|
|
|
"city", "Hongkong", "age", 3);
|
2017-04-27 10:41:17 +08:00
|
|
|
graph.addVertex(T.label, "person", "name", "James",
|
2017-05-15 19:46:35 +08:00
|
|
|
"city", "Beijing", "age", 19);
|
|
|
|
|
graph.addVertex(T.label, "person", "name", "Tom Cat",
|
|
|
|
|
"city", "Beijing", "age", 20);
|
2017-04-27 10:41:17 +08:00
|
|
|
graph.addVertex(T.label, "person", "name", "Lisa",
|
2017-05-15 19:46:35 +08:00
|
|
|
"city", "Beijing", "age", 20);
|
2017-04-27 10:41:17 +08:00
|
|
|
graph.addVertex(T.label, "person", "name", "Hebe",
|
2017-05-15 19:46:35 +08:00
|
|
|
"city", "Taipei", "age", 21);
|
2017-03-28 15:47:52 +08:00
|
|
|
|
|
|
|
|
// Must commit manually
|
2017-04-26 14:10:42 +08:00
|
|
|
GraphTransaction tx = graph.openTransaction();
|
2017-03-21 19:56:21 +08:00
|
|
|
|
2017-03-28 11:30:27 +08:00
|
|
|
logger.info("=============== addVertex ================");
|
2017-05-09 18:22:32 +08:00
|
|
|
Vertex james = tx.addVertex(T.label, "author",
|
2017-07-31 20:17:00 +08:00
|
|
|
"id", 1, "name", "James Gosling",
|
|
|
|
|
"age", 62, "lived", "Canadian");
|
2017-03-28 18:32:13 +08:00
|
|
|
|
2017-03-31 20:48:13 +08:00
|
|
|
Vertex java = tx.addVertex(T.label, "language", "name", "java");
|
2017-03-28 18:32:13 +08:00
|
|
|
Vertex book1 = tx.addVertex(T.label, "book", "name", "java-1");
|
|
|
|
|
Vertex book2 = tx.addVertex(T.label, "book", "name", "java-2");
|
2017-04-28 20:26:19 +08:00
|
|
|
Vertex book3 = tx.addVertex(T.label, "book", "name", "java-3");
|
2017-03-28 18:32:13 +08:00
|
|
|
|
2017-05-09 18:22:32 +08:00
|
|
|
james.addEdge("created", java);
|
|
|
|
|
james.addEdge("authored", book1,
|
2017-07-31 20:17:00 +08:00
|
|
|
"contribution", "1990-1-1",
|
|
|
|
|
"comment", "it's a good book",
|
|
|
|
|
"comment", "it's a good book",
|
|
|
|
|
"comment", "it's a good book too");
|
2017-05-09 18:22:32 +08:00
|
|
|
james.addEdge("authored", book2, "contribution", "2017-4-28");
|
2017-03-21 19:56:21 +08:00
|
|
|
|
2017-05-09 18:22:32 +08:00
|
|
|
james.addEdge("look", book2, "time", "2017-4-28");
|
|
|
|
|
james.addEdge("look", book3, "time", "2016-1-1");
|
|
|
|
|
james.addEdge("look", book3, "time", "2017-4-28");
|
2017-04-28 20:26:19 +08:00
|
|
|
|
2017-03-21 19:56:21 +08:00
|
|
|
// commit data changes
|
|
|
|
|
try {
|
|
|
|
|
tx.commit();
|
|
|
|
|
} catch (BackendException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
try {
|
|
|
|
|
tx.rollback();
|
2017-03-23 13:55:35 +08:00
|
|
|
} catch (BackendException e2) {
|
2017-03-21 19:56:21 +08:00
|
|
|
// TODO Auto-generated catch block
|
2017-03-23 13:55:35 +08:00
|
|
|
e2.printStackTrace();
|
2017-03-21 19:56:21 +08:00
|
|
|
}
|
2017-07-26 11:26:41 +08:00
|
|
|
} finally {
|
|
|
|
|
tx.close();
|
2017-03-21 19:56:21 +08:00
|
|
|
}
|
2017-03-23 14:29:55 +08:00
|
|
|
|
|
|
|
|
// use the default Transaction to commit
|
2017-03-25 02:46:20 +08:00
|
|
|
graph.addVertex(T.label, "book", "name", "java-3");
|
2017-04-26 14:10:42 +08:00
|
|
|
|
|
|
|
|
// tinkerpop tx
|
|
|
|
|
graph.tx().open();
|
|
|
|
|
graph.addVertex(T.label, "book", "name", "java-4");
|
|
|
|
|
graph.addVertex(T.label, "book", "name", "java-5");
|
2017-03-23 14:29:55 +08:00
|
|
|
graph.tx().commit();
|
2017-05-02 20:12:57 +08:00
|
|
|
graph.tx().close();
|
2017-03-31 14:36:52 +08:00
|
|
|
|
2017-04-25 18:52:12 +08:00
|
|
|
// query all
|
2017-05-02 20:12:57 +08:00
|
|
|
GraphTraversal<Vertex, Vertex> vertexes = graph.traversal().V();
|
2017-05-11 14:06:29 +08:00
|
|
|
int size = vertexes.toList().size();
|
|
|
|
|
assert size == 12;
|
|
|
|
|
System.out.println(">>>> query all vertices: size=" + size);
|
2017-04-25 18:52:12 +08:00
|
|
|
|
2017-04-28 21:50:32 +08:00
|
|
|
// query vertex by primary-values
|
2017-05-02 20:12:57 +08:00
|
|
|
vertexes = graph.traversal().V().hasLabel("author").has("id", "1");
|
2017-05-11 14:06:29 +08:00
|
|
|
List<Vertex> vertexList = vertexes.toList();
|
2017-05-11 19:17:42 +08:00
|
|
|
assert vertexList.size() == 1;
|
2017-07-31 20:17:00 +08:00
|
|
|
System.out.println(">>>> query vertices by primary-values: " +
|
|
|
|
|
vertexList);
|
2017-04-28 21:50:32 +08:00
|
|
|
|
2017-05-11 14:06:29 +08:00
|
|
|
// query vertex by id and query out edges
|
2017-07-21 19:38:33 +08:00
|
|
|
vertexes = graph.traversal().V("author:1");
|
2017-05-02 20:12:57 +08:00
|
|
|
GraphTraversal<Vertex, Edge> edgesOfVertex = vertexes.outE("created");
|
2017-05-11 14:06:29 +08:00
|
|
|
List<Edge> edgeList = edgesOfVertex.toList();
|
2017-05-11 19:17:42 +08:00
|
|
|
assert edgeList.size() == 1;
|
2017-05-11 14:06:29 +08:00
|
|
|
System.out.println(">>>> query edges of vertex: " + edgeList);
|
2017-04-25 18:52:12 +08:00
|
|
|
|
2017-07-21 19:38:33 +08:00
|
|
|
vertexes = graph.traversal().V("author:1");
|
2017-05-02 20:12:57 +08:00
|
|
|
GraphTraversal<Vertex, Vertex> verticesOfVertex = vertexes.out("created");
|
2017-05-11 14:06:29 +08:00
|
|
|
vertexList = verticesOfVertex.toList();
|
2017-05-11 19:17:42 +08:00
|
|
|
assert vertexList.size() == 1;
|
2017-05-11 14:06:29 +08:00
|
|
|
System.out.println(">>>> query vertices of vertex: " + vertexList);
|
2017-04-24 15:46:08 +08:00
|
|
|
|
2017-04-28 21:50:32 +08:00
|
|
|
// query edge by sort-values
|
2017-07-21 19:38:33 +08:00
|
|
|
vertexes = graph.traversal().V("author:1");
|
2017-05-02 20:12:57 +08:00
|
|
|
edgesOfVertex = vertexes.outE("look").has("time", "2017-4-28");
|
2017-05-11 14:06:29 +08:00
|
|
|
edgeList = edgesOfVertex.toList();
|
2017-05-11 19:17:42 +08:00
|
|
|
assert edgeList.size() == 2;
|
2017-07-31 20:17:00 +08:00
|
|
|
System.out.println(">>>> query edges of vertex by sort-values: " +
|
|
|
|
|
edgeList);
|
2017-04-28 20:26:19 +08:00
|
|
|
|
2017-05-12 19:06:55 +08:00
|
|
|
// query vertex by condition (filter by property name)
|
2017-05-09 11:36:13 +08:00
|
|
|
ConditionQuery q = new ConditionQuery(HugeType.VERTEX);
|
2017-08-04 19:48:57 +08:00
|
|
|
q.query(IdGenerator.of("author:1"));
|
2017-05-12 19:06:55 +08:00
|
|
|
// TODO: remove the PROPERTIES which may just be used by Cassandra
|
2017-06-07 10:20:47 +08:00
|
|
|
q.key(HugeKeys.PROPERTIES, "age");
|
2017-04-24 21:23:30 +08:00
|
|
|
|
2017-04-25 18:52:12 +08:00
|
|
|
Iterator<Vertex> vertices = graph.vertices(q);
|
2017-05-12 19:06:55 +08:00
|
|
|
assert vertices.hasNext();
|
2017-05-11 14:06:29 +08:00
|
|
|
System.out.println(">>>> queryVertices(age): " + vertices.hasNext());
|
2017-04-24 21:23:30 +08:00
|
|
|
while (vertices.hasNext()) {
|
2017-05-11 14:06:29 +08:00
|
|
|
System.out.println(">>>> queryVertices(age): " + vertices.next());
|
2017-04-24 21:23:30 +08:00
|
|
|
}
|
|
|
|
|
|
2017-04-27 18:44:02 +08:00
|
|
|
// query all edges
|
2017-04-28 13:03:46 +08:00
|
|
|
GraphTraversal<Edge, Edge> edges = graph.traversal().E().limit(2);
|
2017-05-11 14:06:29 +08:00
|
|
|
size = edges.toList().size();
|
|
|
|
|
assert size == 2;
|
|
|
|
|
System.out.println(">>>> query all edges with limit 2: size=" + size);
|
2017-04-27 18:44:02 +08:00
|
|
|
|
2017-04-24 21:23:30 +08:00
|
|
|
// query edge by id
|
2017-07-21 19:38:33 +08:00
|
|
|
String id = "author:1>authored>>book:java-2";
|
2017-04-27 18:44:02 +08:00
|
|
|
edges = graph.traversal().E(id);
|
2017-05-11 14:06:29 +08:00
|
|
|
edgeList = edges.toList();
|
|
|
|
|
assert edgeList.size() == 1;
|
|
|
|
|
System.out.println(">>>> query edge by id: " + edgeList);
|
2017-04-24 21:23:30 +08:00
|
|
|
|
2017-05-09 19:22:35 +08:00
|
|
|
Edge edge = graph.traversal().E(id).toList().get(0);
|
|
|
|
|
edges = graph.traversal().E(edge.id());
|
2017-05-11 14:06:29 +08:00
|
|
|
edgeList = edges.toList();
|
|
|
|
|
assert edgeList.size() == 1;
|
|
|
|
|
System.out.println(">>>> query edge by id: " + edgeList);
|
2017-05-09 19:22:35 +08:00
|
|
|
|
2017-04-24 21:23:30 +08:00
|
|
|
// query edge by condition
|
2017-05-09 11:36:13 +08:00
|
|
|
q = new ConditionQuery(HugeType.EDGE);
|
2017-07-21 19:38:33 +08:00
|
|
|
q.eq(HugeKeys.SOURCE_VERTEX, "author:1");
|
2017-05-12 19:06:55 +08:00
|
|
|
q.eq(HugeKeys.DIRECTION, Direction.OUT);
|
2017-04-24 21:23:30 +08:00
|
|
|
q.eq(HugeKeys.LABEL, "authored");
|
|
|
|
|
q.eq(HugeKeys.SORT_VALUES, "");
|
2017-07-21 19:38:33 +08:00
|
|
|
q.eq(HugeKeys.TARGET_VERTEX, "book:java-1");
|
2017-05-12 19:06:55 +08:00
|
|
|
// NOTE: query edge by has-key just supported by Cassandra
|
|
|
|
|
// q.hasKey(HugeKeys.PROPERTIES, "contribution");
|
2017-04-24 21:23:30 +08:00
|
|
|
|
2017-04-25 18:52:12 +08:00
|
|
|
Iterator<Edge> edges2 = graph.edges(q);
|
2017-05-12 19:06:55 +08:00
|
|
|
assert edges2.hasNext();
|
2017-05-11 14:06:29 +08:00
|
|
|
System.out.println(">>>> queryEdges(contribution): " + edges2.hasNext());
|
2017-04-24 21:23:30 +08:00
|
|
|
while (edges2.hasNext()) {
|
2017-05-11 14:06:29 +08:00
|
|
|
System.out.println(">>>> queryEdges(contribution): " + edges2.next());
|
2017-04-24 21:23:30 +08:00
|
|
|
}
|
2017-04-25 18:52:12 +08:00
|
|
|
|
|
|
|
|
// query by vertex label
|
2017-05-02 20:12:57 +08:00
|
|
|
vertexes = graph.traversal().V().hasLabel("book");
|
2017-05-11 14:06:29 +08:00
|
|
|
size = vertexes.toList().size();
|
|
|
|
|
assert size == 5;
|
|
|
|
|
System.out.println(">>>> query all books: size=" + size);
|
|
|
|
|
|
|
|
|
|
// query by vertex label and key-name
|
|
|
|
|
vertexes = graph.traversal().V().hasLabel("person").has("age");
|
|
|
|
|
size = vertexes.toList().size();
|
|
|
|
|
assert size == 5;
|
2017-05-12 19:06:55 +08:00
|
|
|
System.out.println(">>>> query all persons with age: size=" + size);
|
2017-04-27 10:41:17 +08:00
|
|
|
|
|
|
|
|
// query by vertex props
|
2017-05-02 20:12:57 +08:00
|
|
|
vertexes = graph.traversal().V().hasLabel("person").has("city", "Taipei");
|
2017-05-11 14:06:29 +08:00
|
|
|
vertexList = vertexes.toList();
|
|
|
|
|
assert vertexList.size() == 1;
|
|
|
|
|
System.out.println(">>>> query all persons in Taipei: " + vertexList);
|
2017-04-27 10:41:17 +08:00
|
|
|
|
2017-05-15 19:46:35 +08:00
|
|
|
vertexes = graph.traversal().V().hasLabel("person").has("age", 19);
|
2017-05-11 14:06:29 +08:00
|
|
|
vertexList = vertexes.toList();
|
|
|
|
|
assert vertexList.size() == 1;
|
|
|
|
|
System.out.println(">>>> query all persons age==19: " + vertexList);
|
2017-04-27 10:41:17 +08:00
|
|
|
|
2017-05-15 19:46:35 +08:00
|
|
|
vertexes = graph.traversal().V().hasLabel("person").has("age", P.lt(19));
|
2017-05-11 14:06:29 +08:00
|
|
|
vertexList = vertexes.toList();
|
|
|
|
|
assert vertexList.size() == 1;
|
2017-05-15 19:46:35 +08:00
|
|
|
assert vertexList.get(0).property("age").value().equals(3);
|
2017-05-11 14:06:29 +08:00
|
|
|
System.out.println(">>>> query all persons age<19: " + vertexList);
|
2017-05-02 20:12:57 +08:00
|
|
|
|
|
|
|
|
// remove vertex (and its edges)
|
2017-05-15 19:46:35 +08:00
|
|
|
vertexes = graph.traversal().V().hasLabel("person").has("age", 19);
|
2017-05-02 20:12:57 +08:00
|
|
|
Vertex vertex = vertexes.toList().get(0);
|
|
|
|
|
vertex.addEdge("look", book3, "time", "2017-5-3");
|
|
|
|
|
System.out.println(">>>> remove vertex: " + vertex);
|
|
|
|
|
vertex.remove();
|
2017-05-11 14:06:29 +08:00
|
|
|
try {
|
|
|
|
|
graph.traversal().V(vertex.id()).toList();
|
|
|
|
|
assert false;
|
2017-06-08 19:41:59 +08:00
|
|
|
} catch (NotFoundException e) {
|
|
|
|
|
assert e.getMessage().contains("Not found the VERTEX entry");
|
2017-05-11 14:06:29 +08:00
|
|
|
}
|
2017-05-02 20:12:57 +08:00
|
|
|
|
|
|
|
|
// remove edge
|
2017-07-21 19:38:33 +08:00
|
|
|
id = "author:1>authored>>book:java-2";
|
2017-05-02 20:12:57 +08:00
|
|
|
edges = graph.traversal().E(id);
|
2017-05-09 19:22:35 +08:00
|
|
|
edge = edges.toList().get(0);
|
2017-05-02 20:12:57 +08:00
|
|
|
System.out.println(">>>> remove edge: " + edge);
|
|
|
|
|
edge.remove();
|
2017-05-11 14:06:29 +08:00
|
|
|
try {
|
|
|
|
|
graph.traversal().E(id).toList();
|
|
|
|
|
assert false;
|
2017-06-08 19:41:59 +08:00
|
|
|
} catch (NotFoundException e) {
|
|
|
|
|
assert e.getMessage().contains("Not found the EDGE entry");
|
2017-05-11 14:06:29 +08:00
|
|
|
}
|
2017-07-17 21:17:57 +08:00
|
|
|
|
|
|
|
|
// test index remove and index rebuild
|
2017-07-31 21:26:38 +08:00
|
|
|
// personByCity.remove();
|
|
|
|
|
// personByAge.remove();
|
2017-07-17 21:17:57 +08:00
|
|
|
graph.addVertex(T.label, "person", "name", "Bright",
|
2017-07-31 20:17:00 +08:00
|
|
|
"city", "LuAn", "age", 27);
|
2017-07-31 21:26:38 +08:00
|
|
|
personByCity = schema.indexLabel("personByCity").onV("person")
|
|
|
|
|
.secondary().by("city").create();
|
|
|
|
|
personByAge = schema.indexLabel("personByAge").onV("person")
|
|
|
|
|
.search().by("age").create();
|
2017-07-17 21:17:57 +08:00
|
|
|
// insert one person here manually
|
2017-07-31 21:26:38 +08:00
|
|
|
schema.indexLabel("personByCity").rebuild();
|
|
|
|
|
schema.indexLabel("personByAge").rebuild();
|
|
|
|
|
schema.vertexLabel("person").rebuildIndex();
|
2017-07-17 21:17:57 +08:00
|
|
|
// insert one person here manually
|
2017-02-24 18:01:51 +08:00
|
|
|
}
|
|
|
|
|
}
|