Search in sources :

Example 81 with Vertex

use of com.baidu.hugegraph.structure.graph.Vertex 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;
}
Also used : Direction(com.baidu.hugegraph.structure.constant.Direction) Iterables(com.google.common.collect.Iterables) Type(com.baidu.hugegraph.entity.query.GremlinResult.Type) ServerException(com.baidu.hugegraph.exception.ServerException) ExternalException(com.baidu.hugegraph.exception.ExternalException) AdjacentQuery(com.baidu.hugegraph.entity.query.AdjacentQuery) IdStrategy(com.baidu.hugegraph.structure.constant.IdStrategy) VertexLabelEntity(com.baidu.hugegraph.entity.schema.VertexLabelEntity) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) Ex(com.baidu.hugegraph.util.Ex) StringUtils(org.apache.commons.lang3.StringUtils) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) ArrayList(java.util.ArrayList) JsonView(com.baidu.hugegraph.entity.query.JsonView) HashSet(java.util.HashSet) GremlinUtil(com.baidu.hugegraph.util.GremlinUtil) TableView(com.baidu.hugegraph.entity.query.TableView) Service(org.springframework.stereotype.Service) Map(java.util.Map) ClientException(com.baidu.hugegraph.rest.ClientException) Path(com.baidu.hugegraph.structure.graph.Path) HugeClient(com.baidu.hugegraph.driver.HugeClient) GremlinRequest(com.baidu.hugegraph.api.gremlin.GremlinRequest) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) ImmutableMap(com.google.common.collect.ImmutableMap) GraphView(com.baidu.hugegraph.entity.query.GraphView) Set(java.util.Set) Result(com.baidu.hugegraph.structure.gremlin.Result) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) TypedResult(com.baidu.hugegraph.entity.query.TypedResult) Edge(com.baidu.hugegraph.structure.graph.Edge) IllegalGremlinException(com.baidu.hugegraph.exception.IllegalGremlinException) Vertex(com.baidu.hugegraph.structure.graph.Vertex) HubbleOptions(com.baidu.hugegraph.options.HubbleOptions) VertexLabelService(com.baidu.hugegraph.service.schema.VertexLabelService) List(java.util.List) CollectionUtils(org.springframework.util.CollectionUtils) HugeClientPoolService(com.baidu.hugegraph.service.HugeClientPoolService) Log4j2(lombok.extern.log4j.Log4j2) HugeConfig(com.baidu.hugegraph.config.HugeConfig) Comparator(java.util.Comparator) InternalException(com.baidu.hugegraph.exception.InternalException) Collections(java.util.Collections) GremlinQuery(com.baidu.hugegraph.entity.query.GremlinQuery) GremlinResult(com.baidu.hugegraph.entity.query.GremlinResult) HashMap(java.util.HashMap) HugeConfig(com.baidu.hugegraph.config.HugeConfig) Result(com.baidu.hugegraph.structure.gremlin.Result) TypedResult(com.baidu.hugegraph.entity.query.TypedResult) GremlinResult(com.baidu.hugegraph.entity.query.GremlinResult) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) Edge(com.baidu.hugegraph.structure.graph.Edge)

Example 82 with Vertex

use of com.baidu.hugegraph.structure.graph.Vertex in project hugegraph-computer by hugegraph.

the class SingleInsertTask method run.

@Override
public void run() {
    for (Vertex vertex : this.batch) {
        try {
            this.insertSingle(vertex);
            this.increaseLoadSuccess();
        } catch (Exception e) {
            this.metrics().increaseInsertFailure();
            this.handleInsertFailure(e);
        }
    }
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex)

Example 83 with Vertex

use of com.baidu.hugegraph.structure.graph.Vertex in project hugegraph-computer by hugegraph.

the class TriangleCountTest method setup.

@BeforeClass
public static void setup() {
    clearAll();
    SchemaManager schema = client().schema();
    schema.propertyKey(PROPERTY_KEY).asInt().ifNotExist().create();
    schema.vertexLabel(VERTX_LABEL).properties(PROPERTY_KEY).useCustomizeStringId().ifNotExist().create();
    schema.edgeLabel(EDGE_LABEL).sourceLabel(VERTX_LABEL).targetLabel(VERTX_LABEL).properties(PROPERTY_KEY).ifNotExist().create();
    GraphManager graph = client().graph();
    Vertex vA = graph.addVertex(T.label, VERTX_LABEL, T.id, "tc_A", PROPERTY_KEY, 1);
    Vertex vB = graph.addVertex(T.label, VERTX_LABEL, T.id, "tc_B", PROPERTY_KEY, 1);
    Vertex vC = graph.addVertex(T.label, VERTX_LABEL, T.id, "tc_C", PROPERTY_KEY, 1);
    Vertex vD = graph.addVertex(T.label, VERTX_LABEL, T.id, "tc_D", PROPERTY_KEY, 1);
    Vertex vE = graph.addVertex(T.label, VERTX_LABEL, T.id, "tc_E", PROPERTY_KEY, 1);
    vA.addEdge(EDGE_LABEL, vB, PROPERTY_KEY, 1);
    vA.addEdge(EDGE_LABEL, vC, PROPERTY_KEY, 1);
    vB.addEdge(EDGE_LABEL, vC, PROPERTY_KEY, 1);
    vC.addEdge(EDGE_LABEL, vD, PROPERTY_KEY, 1);
    vD.addEdge(EDGE_LABEL, vA, PROPERTY_KEY, 1);
    vD.addEdge(EDGE_LABEL, vE, PROPERTY_KEY, 1);
    vE.addEdge(EDGE_LABEL, vD, PROPERTY_KEY, 1);
    vE.addEdge(EDGE_LABEL, vC, PROPERTY_KEY, 1);
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) GraphManager(com.baidu.hugegraph.driver.GraphManager) SchemaManager(com.baidu.hugegraph.driver.SchemaManager) BeforeClass(org.junit.BeforeClass)

Example 84 with Vertex

use of com.baidu.hugegraph.structure.graph.Vertex in project hugegraph-computer by hugegraph.

the class RingsDetectionWithFilterTest method setup.

@BeforeClass
public static void setup() {
    clearAll();
    HugeClient client = client();
    SchemaManager schema = client.schema();
    schema.propertyKey("weight").asInt().ifNotExist().create();
    schema.vertexLabel("user").properties("weight").useCustomizeStringId().ifNotExist().create();
    schema.edgeLabel("know").sourceLabel("user").targetLabel("user").properties("weight").ifNotExist().create();
    GraphManager graph = client.graph();
    Vertex vA = graph.addVertex(T.label, "user", T.id, "A", "weight", 1);
    Vertex vB = graph.addVertex(T.label, "user", T.id, "B", "weight", 1);
    Vertex vC = graph.addVertex(T.label, "user", T.id, "C", "weight", 1);
    Vertex vD = graph.addVertex(T.label, "user", T.id, "D", "weight", 1);
    Vertex vE = graph.addVertex(T.label, "user", T.id, "E", "weight", 3);
    vA.addEdge("know", vB, "weight", 1);
    vA.addEdge("know", vC, "weight", 1);
    vA.addEdge("know", vD, "weight", 1);
    vB.addEdge("know", vA, "weight", 2);
    vB.addEdge("know", vC, "weight", 1);
    vB.addEdge("know", vE, "weight", 1);
    vC.addEdge("know", vA, "weight", 1);
    vC.addEdge("know", vB, "weight", 1);
    vC.addEdge("know", vD, "weight", 1);
    vD.addEdge("know", vC, "weight", 1);
    vD.addEdge("know", vE, "weight", 1);
    vE.addEdge("know", vC, "weight", 1);
    RingsDetectionTestOutput.EXPECT_RINGS = EXPECT_RINGS;
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) HugeClient(com.baidu.hugegraph.driver.HugeClient) GraphManager(com.baidu.hugegraph.driver.GraphManager) SchemaManager(com.baidu.hugegraph.driver.SchemaManager) BeforeClass(org.junit.BeforeClass)

Example 85 with Vertex

use of com.baidu.hugegraph.structure.graph.Vertex in project hugegraph-computer by hugegraph.

the class ClosenessCentralityTest method setup.

@BeforeClass
public static void setup() {
    clearAll();
    HugeClient client = client();
    SchemaManager schema = client.schema();
    schema.propertyKey("rate").asInt().ifNotExist().create();
    schema.vertexLabel("user").useCustomizeStringId().ifNotExist().create();
    schema.edgeLabel("link").sourceLabel("user").targetLabel("user").properties("rate").ifNotExist().create();
    GraphManager graph = client.graph();
    Vertex vA = graph.addVertex(T.label, "user", T.id, "A");
    Vertex vB = graph.addVertex(T.label, "user", T.id, "B");
    Vertex vC = graph.addVertex(T.label, "user", T.id, "C");
    Vertex vD = graph.addVertex(T.label, "user", T.id, "D");
    Vertex vE = graph.addVertex(T.label, "user", T.id, "E");
    Vertex vF = graph.addVertex(T.label, "user", T.id, "F");
    vA.addEdge("link", vB, "rate", 1);
    vB.addEdge("link", vA, "rate", 1);
    vB.addEdge("link", vC, "rate", 2);
    vC.addEdge("link", vB, "rate", 2);
    vB.addEdge("link", vD, "rate", 2);
    vD.addEdge("link", vB, "rate", 2);
    vC.addEdge("link", vD, "rate", 1);
    vD.addEdge("link", vC, "rate", 1);
    vC.addEdge("link", vE, "rate", 3);
    vE.addEdge("link", vC, "rate", 3);
    vD.addEdge("link", vE, "rate", 1);
    vE.addEdge("link", vD, "rate", 1);
    vD.addEdge("link", vF, "rate", 4);
    vF.addEdge("link", vD, "rate", 4);
    vE.addEdge("link", vF, "rate", 2);
    vF.addEdge("link", vE, "rate", 2);
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) HugeClient(com.baidu.hugegraph.driver.HugeClient) GraphManager(com.baidu.hugegraph.driver.GraphManager) SchemaManager(com.baidu.hugegraph.driver.SchemaManager) BeforeClass(org.junit.BeforeClass)

Aggregations

Vertex (com.baidu.hugegraph.structure.graph.Vertex)165 Test (org.junit.Test)110 Edge (com.baidu.hugegraph.structure.graph.Edge)33 HugeClient (com.baidu.hugegraph.driver.HugeClient)22 ArrayList (java.util.ArrayList)21 SchemaManager (com.baidu.hugegraph.driver.SchemaManager)18 BaseClientTest (com.baidu.hugegraph.BaseClientTest)17 BeforeClass (org.junit.BeforeClass)17 GraphManager (com.baidu.hugegraph.driver.GraphManager)14 BatchVertexRequest (com.baidu.hugegraph.structure.graph.BatchVertexRequest)13 Path (com.baidu.hugegraph.structure.graph.Path)11 Result (com.baidu.hugegraph.structure.gremlin.Result)10 ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)10 List (java.util.List)10 BaseApiTest (com.baidu.hugegraph.api.BaseApiTest)9 ImmutableList (com.google.common.collect.ImmutableList)6 ImmutableMap (com.google.common.collect.ImmutableMap)6 Map (java.util.Map)6 RestResult (com.baidu.hugegraph.rest.RestResult)5 PathsWithVertices (com.baidu.hugegraph.structure.traverser.PathsWithVertices)5