use of com.baidu.hugegraph.structure.graph.Vertex 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.graph.Vertex 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.graph.Vertex in project incubator-hugegraph-toolchain by apache.
the class GremlinQueryService method parseResults.
private TypedResult parseResults(ResultSet resultSet) {
if (resultSet == null) {
return new TypedResult(Type.EMPTY, null);
}
Iterator<Result> iter = resultSet.iterator();
if (!iter.hasNext()) {
return new TypedResult(Type.EMPTY, null);
}
Map<Type, Integer> typeVotes = new HashMap<>();
List<Object> typedData = new ArrayList<>(resultSet.size());
while (iter.hasNext()) {
Result result = iter.next();
if (result == null) {
// NOTE: null value doesn't vote
continue;
}
Object object = result.getObject();
Type type;
if (object instanceof Vertex) {
type = Type.VERTEX;
} else if (object instanceof Edge) {
type = Type.EDGE;
} else if (object instanceof Path) {
type = Type.PATH;
} else {
type = Type.GENERAL;
}
typeVotes.compute(type, (k, v) -> v == null ? 1 : v + 1);
typedData.add(object);
}
Type type;
if (typeVotes.isEmpty()) {
type = Type.EMPTY;
} else {
// Find the key with max value
type = Collections.max(typeVotes.entrySet(), Comparator.comparingInt(Map.Entry::getValue)).getKey();
}
return new TypedResult(type, typedData);
}
use of com.baidu.hugegraph.structure.graph.Vertex in project incubator-hugegraph-toolchain by apache.
the class GremlinQueryService method buildTableView.
private TableView buildTableView(TypedResult typedResult) {
List<Object> data = typedResult.getData();
if (CollectionUtils.isEmpty(data)) {
return TableView.EMPTY;
}
switch(typedResult.getType()) {
case EMPTY:
return TableView.EMPTY;
case GENERAL:
// result
List<Object> results = new ArrayList<>(data.size());
data.forEach(object -> {
results.add(ImmutableMap.of("result", object));
});
return new TableView(TableView.GENERAL_HEADER, results);
case VERTEX:
// id, label, properties
List<Object> vertices = new ArrayList<>(data.size());
data.forEach(object -> {
if (object instanceof Vertex) {
vertices.add(object);
}
});
return new TableView(TableView.VERTEX_HEADER, vertices);
case EDGE:
// id, label, source, target, properties
List<Object> edges = new ArrayList<>(data.size());
data.forEach(object -> {
if (object instanceof Edge) {
edges.add(object);
}
});
return new TableView(TableView.EDGE_HEADER, edges);
case PATH:
// path, only fill vertex/edge id
List<Object> paths = new ArrayList<>(data.size());
data.forEach(object -> {
if (object instanceof Path) {
Path path = (Path) object;
List<Object> ids = new ArrayList<>();
path.objects().forEach(element -> {
if (element instanceof Vertex) {
ids.add(((Vertex) element).id());
} else if (element instanceof Edge) {
ids.add(((Edge) element).id());
} else {
ids.add(element);
}
});
paths.add(ImmutableMap.of("path", ids));
}
});
return new TableView(TableView.PATH_HEADER, paths);
default:
throw new AssertionError(String.format("Unknown result type '%s'", typedResult.getType()));
}
}
use of com.baidu.hugegraph.structure.graph.Vertex 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;
}
Aggregations