use of com.baidu.hugegraph.structure.gremlin.ResultSet in project incubator-hugegraph-toolchain by apache.
the class GremlinApiTest method testAsyncRemoveAllEdges.
@Test
public void testAsyncRemoveAllEdges() {
GremlinRequest request = new GremlinRequest("g.E()");
ResultSet resultSet = gremlin().execute(request);
Assert.assertEquals(6, resultSet.size());
String gremlin = "g.E().drop()";
request = new GremlinRequest(gremlin);
long id = gremlin().executeAsTask(request);
waitUntilTaskCompleted(id);
request = new GremlinRequest("g.E()");
resultSet = gremlin().execute(request);
Assert.assertEquals(0, resultSet.size());
}
use of com.baidu.hugegraph.structure.gremlin.ResultSet in project incubator-hugegraph-toolchain by apache.
the class SingleExample method main.
public static void main(String[] args) throws IOException {
// If connect failed will throw a exception.
HugeClient hugeClient = HugeClient.builder("http://localhost:8080", "hugegraph").build();
SchemaManager schema = hugeClient.schema();
schema.propertyKey("name").asText().ifNotExist().create();
schema.propertyKey("age").asInt().ifNotExist().create();
schema.propertyKey("city").asText().ifNotExist().create();
schema.propertyKey("weight").asDouble().ifNotExist().create();
schema.propertyKey("lang").asText().ifNotExist().create();
schema.propertyKey("date").asDate().ifNotExist().create();
schema.propertyKey("price").asInt().ifNotExist().create();
schema.vertexLabel("person").properties("name", "age", "city").primaryKeys("name").ifNotExist().create();
schema.vertexLabel("software").properties("name", "lang", "price").primaryKeys("name").ifNotExist().create();
schema.indexLabel("personByCity").onV("person").by("city").secondary().ifNotExist().create();
schema.indexLabel("personByAgeAndCity").onV("person").by("age", "city").secondary().ifNotExist().create();
schema.indexLabel("softwareByPrice").onV("software").by("price").range().ifNotExist().create();
schema.edgeLabel("knows").sourceLabel("person").targetLabel("person").properties("date", "weight").ifNotExist().create();
schema.edgeLabel("created").sourceLabel("person").targetLabel("software").properties("date", "weight").ifNotExist().create();
schema.indexLabel("createdByDate").onE("created").by("date").secondary().ifNotExist().create();
schema.indexLabel("createdByWeight").onE("created").by("weight").range().ifNotExist().create();
schema.indexLabel("knowsByWeight").onE("knows").by("weight").range().ifNotExist().create();
GraphManager graph = hugeClient.graph();
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", "age", 35, "city", "Shanghai");
marko.addEdge("knows", vadas, "date", "2016-01-10", "weight", 0.5);
marko.addEdge("knows", josh, "date", "2013-02-20", "weight", 1.0);
marko.addEdge("created", lop, "date", "2017-12-10", "weight", 0.4);
josh.addEdge("created", lop, "date", "2009-11-11", "weight", 0.4);
josh.addEdge("created", ripple, "date", "2017-12-10", "weight", 1.0);
peter.addEdge("created", lop, "date", "2017-03-24", "weight", 0.2);
GremlinManager gremlin = hugeClient.gremlin();
System.out.println("==== Path ====");
ResultSet resultSet = gremlin.gremlin("g.V().outE().path()").execute();
Iterator<Result> results = resultSet.iterator();
results.forEachRemaining(result -> {
System.out.println(result.getObject().getClass());
Object object = result.getObject();
if (object instanceof Vertex) {
System.out.println(((Vertex) object).id());
} else if (object instanceof Edge) {
System.out.println(((Edge) object).id());
} else if (object instanceof Path) {
List<Object> elements = ((Path) object).objects();
elements.forEach(element -> {
System.out.println(element.getClass());
System.out.println(element);
});
} else {
System.out.println(object);
}
});
hugeClient.close();
}
use of com.baidu.hugegraph.structure.gremlin.ResultSet in project incubator-hugegraph-toolchain by apache.
the class GremlinQueryService method expandVertex.
public GremlinResult expandVertex(int connId, AdjacentQuery query) {
HugeClient client = this.getClient(connId);
// Build gremlin query
String gremlin = this.buildGremlinQuery(connId, query);
log.debug("expand vertex gremlin ==> {}", gremlin);
// Execute gremlin query
ResultSet resultSet = this.executeGremlin(gremlin, client);
List<Vertex> vertices = new ArrayList<>(resultSet.size());
List<Edge> edges = new ArrayList<>(resultSet.size());
for (Iterator<Result> iter = resultSet.iterator(); iter.hasNext(); ) {
Path path = iter.next().getPath();
List<Object> objects = path.objects();
assert objects.size() == 3;
Edge edge = (Edge) objects.get(1);
Vertex vertex = (Vertex) objects.get(2);
// Filter vertices and edges that existed in query
if (query.retainEdge(edge)) {
edges.add(edge);
}
if (query.retainVertex(vertex)) {
vertices.add(vertex);
}
}
// Build graph view
GraphView graphView = new GraphView(vertices, edges);
return GremlinResult.builder().type(Type.PATH).graphView(graphView).build();
}
use of com.baidu.hugegraph.structure.gremlin.ResultSet in project incubator-hugegraph-toolchain by apache.
the class GremlinQueryService method verticesOfEdge.
private Map<Object, Vertex> verticesOfEdge(TypedResult result, Map<String, Edge> edges, HugeClient client) {
int batchSize = this.config.get(HubbleOptions.GREMLIN_BATCH_QUERY_IDS);
Set<Object> vertexIds = new HashSet<>(edges.size() * 2);
edges.values().forEach(edge -> {
vertexIds.add(edge.sourceId());
vertexIds.add(edge.targetId());
});
Map<Object, Vertex> vertices = new HashMap<>(vertexIds.size());
Iterables.partition(vertexIds, batchSize).forEach(batch -> {
List<String> escapedIds = batch.stream().map(GremlinUtil::escapeId).collect(Collectors.toList());
String ids = StringUtils.join(escapedIds, ",");
String gremlin = String.format("g.V(%s)", ids);
ResultSet resultSet = client.gremlin().gremlin(gremlin).execute();
for (Iterator<Result> iter = resultSet.iterator(); iter.hasNext(); ) {
Vertex vertex = iter.next().getVertex();
vertices.put(vertex.id(), vertex);
}
});
return vertices;
}
use of com.baidu.hugegraph.structure.gremlin.ResultSet in project incubator-hugegraph-toolchain by apache.
the class GremlinQueryService method edgesOfVertex.
private Map<String, Edge> edgesOfVertex(TypedResult result, Map<Object, Vertex> vertices, HugeClient client) {
final HugeConfig config = this.config;
int batchSize = config.get(HubbleOptions.GREMLIN_BATCH_QUERY_IDS);
int edgeLimit = config.get(HubbleOptions.GREMLIN_EDGES_TOTAL_LIMIT);
int degreeLimit = config.get(HubbleOptions.GREMLIN_VERTEX_DEGREE_LIMIT);
Set<Object> vertexIds = vertices.keySet();
Map<String, Edge> edges = new HashMap<>(vertexIds.size());
Iterables.partition(vertexIds, batchSize).forEach(batch -> {
List<String> escapedIds = batch.stream().map(GremlinUtil::escapeId).collect(Collectors.toList());
String ids = StringUtils.join(escapedIds, ",");
// Any better way to find two vertices has linked?
String gremlin;
if (result.getType().isPath()) {
// If result type is path, the vertices count not too much in theory
gremlin = String.format("g.V(%s).bothE().local(limit(%s)).dedup()", ids, degreeLimit);
} else {
gremlin = String.format("g.V(%s).bothE().dedup().limit(%s)", ids, edgeLimit);
}
ResultSet resultSet = client.gremlin().gremlin(gremlin).execute();
// The edges count for per vertex
Map<Object, Integer> degrees = new HashMap<>(resultSet.size());
for (Iterator<Result> iter = resultSet.iterator(); iter.hasNext(); ) {
Edge edge = iter.next().getEdge();
Object source = edge.sourceId();
Object target = edge.targetId();
// only add the interconnected edges of the found vertices
if (!vertexIds.contains(source) || !vertexIds.contains(target)) {
continue;
}
edges.put(edge.id(), edge);
if (edges.size() >= edgeLimit) {
break;
}
int deg = degrees.compute(source, (k, v) -> v == null ? 1 : v + 1);
if (deg >= degreeLimit) {
break;
}
deg = degrees.compute(target, (k, v) -> v == null ? 1 : v + 1);
if (deg >= degreeLimit) {
break;
}
}
});
return edges;
}
Aggregations