Search in sources :

Example 41 with PropertyKey

use of com.baidu.hugegraph.schema.PropertyKey in project incubator-hugegraph by apache.

the class PropertyKeyAPI method get.

@GET
@Timed
@Path("{name}")
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=property_key_read" })
public String get(@Context GraphManager manager, @PathParam("graph") String graph, @PathParam("name") String name) {
    LOG.debug("Graph [{}] get property key by name '{}'", graph, name);
    HugeGraph g = graph(manager, graph);
    PropertyKey propertyKey = g.schema().getPropertyKey(name);
    return manager.serializer(g).writePropertyKey(propertyKey);
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) Path(jakarta.ws.rs.Path) RolesAllowed(jakarta.annotation.security.RolesAllowed) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(jakarta.ws.rs.GET)

Example 42 with PropertyKey

use of com.baidu.hugegraph.schema.PropertyKey in project incubator-hugegraph by apache.

the class PropertyKeyAPI method update.

@PUT
@Timed
@Status(Status.ACCEPTED)
@Path("{name}")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=property_key_write" })
public String update(@Context GraphManager manager, @PathParam("graph") String graph, @PathParam("name") String name, @QueryParam("action") String action, PropertyKeyAPI.JsonPropertyKey jsonPropertyKey) {
    LOG.debug("Graph [{}] {} property key: {}", graph, action, jsonPropertyKey);
    checkUpdatingBody(jsonPropertyKey);
    E.checkArgument(name.equals(jsonPropertyKey.name), "The name in url(%s) and body(%s) are different", name, jsonPropertyKey.name);
    HugeGraph g = graph(manager, graph);
    if (ACTION_CLEAR.equals(action)) {
        PropertyKey propertyKey = g.propertyKey(name);
        E.checkArgument(propertyKey.olap(), "Only olap property key can do action clear, " + "but got '%s'", propertyKey);
        Id id = g.clearPropertyKey(propertyKey);
        SchemaElement.TaskWithSchema pk = new SchemaElement.TaskWithSchema(propertyKey, id);
        return manager.serializer(g).writeTaskWithSchema(pk);
    }
    // Parse action parameter
    boolean append = checkAndParseAction(action);
    PropertyKey.Builder builder = jsonPropertyKey.convert2Builder(g);
    PropertyKey propertyKey = append ? builder.append() : builder.eliminate();
    SchemaElement.TaskWithSchema pk = new SchemaElement.TaskWithSchema(propertyKey, IdGenerator.ZERO);
    return manager.serializer(g).writeTaskWithSchema(pk);
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) SchemaElement(com.baidu.hugegraph.schema.SchemaElement) Id(com.baidu.hugegraph.backend.id.Id) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) Status(com.baidu.hugegraph.api.filter.StatusFilter.Status) Path(jakarta.ws.rs.Path) RolesAllowed(jakarta.annotation.security.RolesAllowed) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) PUT(jakarta.ws.rs.PUT)

Example 43 with PropertyKey

use of com.baidu.hugegraph.schema.PropertyKey in project incubator-hugegraph by apache.

the class PropertyKeyAPI method list.

@GET
@Timed
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=property_key_read" })
public String list(@Context GraphManager manager, @PathParam("graph") String graph, @QueryParam("names") List<String> names) {
    boolean listAll = CollectionUtils.isEmpty(names);
    if (listAll) {
        LOG.debug("Graph [{}] list property keys", graph);
    } else {
        LOG.debug("Graph [{}] get property keys by names {}", graph, names);
    }
    HugeGraph g = graph(manager, graph);
    List<PropertyKey> propKeys;
    if (listAll) {
        propKeys = g.schema().getPropertyKeys();
    } else {
        propKeys = new ArrayList<>(names.size());
        for (String name : names) {
            propKeys.add(g.schema().getPropertyKey(name));
        }
    }
    return manager.serializer(g).writePropertyKeys(propKeys);
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) RolesAllowed(jakarta.annotation.security.RolesAllowed) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(jakarta.ws.rs.GET)

Example 44 with PropertyKey

use of com.baidu.hugegraph.schema.PropertyKey in project incubator-hugegraph by apache.

the class Example1 method testQuery.

public static void testQuery(final HugeGraph graph) {
    // query all
    GraphTraversal<Vertex, Vertex> vertices = graph.traversal().V();
    int size = vertices.toList().size();
    assert size == 12;
    LOG.info(">>>> query all vertices: size {}", size);
    // query by label
    vertices = graph.traversal().V().hasLabel("person");
    size = vertices.toList().size();
    assert size == 5;
    LOG.info(">>>> query all persons: size {}", size);
    // query vertex by primary-values
    vertices = graph.traversal().V().hasLabel("author").has("id", 1);
    List<Vertex> vertexList = vertices.toList();
    assert vertexList.size() == 1;
    LOG.info(">>>> query vertices by primary-values: {}", vertexList);
    VertexLabel author = graph.schema().getVertexLabel("author");
    String authorId = String.format("%s:%s", author.id().asString(), "11");
    // query vertex by id and query out edges
    vertices = graph.traversal().V(authorId);
    GraphTraversal<Vertex, Edge> edgesOfVertex = vertices.outE("created");
    List<Edge> edgeList = edgesOfVertex.toList();
    assert edgeList.size() == 1;
    LOG.info(">>>> query edges of vertex: {}", edgeList);
    vertices = graph.traversal().V(authorId);
    vertexList = vertices.out("created").toList();
    assert vertexList.size() == 1;
    LOG.info(">>>> query vertices of vertex: {}", vertexList);
    // query edge by sort-values
    vertices = graph.traversal().V(authorId);
    edgesOfVertex = vertices.outE("write").has("time", "2017-4-28");
    edgeList = edgesOfVertex.toList();
    assert edgeList.size() == 2;
    LOG.info(">>>> query edges of vertex by sort-values: {}", edgeList);
    // query vertex by condition (filter by property name)
    ConditionQuery q = new ConditionQuery(HugeType.VERTEX);
    PropertyKey age = graph.propertyKey("age");
    q.key(HugeKeys.PROPERTIES, age.id());
    if (graph.backendStoreFeatures().supportsQueryWithContainsKey()) {
        Iterator<Vertex> iter = graph.vertices(q);
        assert iter.hasNext();
        LOG.info(">>>> queryVertices(age): {}", iter.hasNext());
        while (iter.hasNext()) {
            LOG.info(">>>> queryVertices(age): {}", iter.next());
        }
    }
    // query all edges
    GraphTraversal<Edge, Edge> edges = graph.traversal().E().limit(2);
    size = edges.toList().size();
    assert size == 2;
    LOG.info(">>>> query all edges with limit 2: size {}", size);
    // query edge by id
    EdgeLabel authored = graph.edgeLabel("authored");
    VertexLabel book = graph.schema().getVertexLabel("book");
    String book1Id = String.format("%s:%s", book.id().asString(), "java-1");
    String book2Id = String.format("%s:%s", book.id().asString(), "java-2");
    String edgeId = String.format("S%s>%s>%s>S%s", authorId, authored.id(), "", book2Id);
    edges = graph.traversal().E(edgeId);
    edgeList = edges.toList();
    assert edgeList.size() == 1;
    LOG.info(">>>> query edge by id: {}", edgeList);
    Edge edge = edgeList.get(0);
    edges = graph.traversal().E(edge.id());
    edgeList = edges.toList();
    assert edgeList.size() == 1;
    LOG.info(">>>> query edge by id: {}", edgeList);
    // query edge by condition
    q = new ConditionQuery(HugeType.EDGE);
    q.eq(HugeKeys.OWNER_VERTEX, IdGenerator.of(authorId));
    q.eq(HugeKeys.DIRECTION, Directions.OUT);
    q.eq(HugeKeys.LABEL, authored.id());
    q.eq(HugeKeys.SORT_VALUES, "");
    q.eq(HugeKeys.OTHER_VERTEX, IdGenerator.of(book1Id));
    Iterator<Edge> edges2 = graph.edges(q);
    assert edges2.hasNext();
    LOG.info(">>>> queryEdges(id-condition): {}", edges2.hasNext());
    while (edges2.hasNext()) {
        LOG.info(">>>> queryEdges(id-condition): {}", edges2.next());
    }
    // NOTE: query edge by has-key just supported by Cassandra
    if (graph.backendStoreFeatures().supportsQueryWithContainsKey()) {
        PropertyKey contribution = graph.propertyKey("contribution");
        q.key(HugeKeys.PROPERTIES, contribution.id());
        Iterator<Edge> edges3 = graph.edges(q);
        assert edges3.hasNext();
        LOG.info(">>>> queryEdges(contribution): {}", edges3.hasNext());
        while (edges3.hasNext()) {
            LOG.info(">>>> queryEdges(contribution): {}", edges3.next());
        }
    }
    // query by vertex label
    vertices = graph.traversal().V().hasLabel("book");
    size = vertices.toList().size();
    assert size == 5;
    LOG.info(">>>> query all books: size {}", size);
    // query by vertex label and key-name
    vertices = graph.traversal().V().hasLabel("person").has("age");
    size = vertices.toList().size();
    assert size == 5;
    LOG.info(">>>> query all persons with age: size {}", size);
    // query by vertex props
    vertices = graph.traversal().V().hasLabel("person").has("city", "Taipei");
    vertexList = vertices.toList();
    assert vertexList.size() == 1;
    LOG.info(">>>> query all persons in Taipei: {}", vertexList);
    vertices = graph.traversal().V().hasLabel("person").has("age", 19);
    vertexList = vertices.toList();
    assert vertexList.size() == 1;
    LOG.info(">>>> query all persons age==19: {}", vertexList);
    vertices = graph.traversal().V().hasLabel("person").has("age", P.lt(19));
    vertexList = vertices.toList();
    assert vertexList.size() == 1;
    assert vertexList.get(0).property("age").value().equals(3);
    LOG.info(">>>> query all persons age<19: {}", vertexList);
    String addr = "Bay Area";
    vertices = graph.traversal().V().hasLabel("author").has("lived", Text.contains(addr));
    vertexList = vertices.toList();
    assert vertexList.size() == 1;
    LOG.info(">>>> query all authors lived {}: {}", addr, vertexList);
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) Edge(org.apache.tinkerpop.gremlin.structure.Edge) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Example 45 with PropertyKey

use of com.baidu.hugegraph.schema.PropertyKey in project incubator-hugegraph by apache.

the class JsonUtilTest method testSerializePropertyKey.

@Test
public void testSerializePropertyKey() {
    FakeObjects fakeObject = new FakeObjects();
    PropertyKey name = fakeObject.newPropertyKey(IdGenerator.of(1), "name");
    String json = JsonUtil.toJson(name);
    Assert.assertEquals("{\"id\":1,\"name\":\"name\"," + "\"data_type\":\"TEXT\"," + "\"cardinality\":\"SINGLE\"," + "\"aggregate_type\":\"NONE\"," + "\"write_type\":\"OLTP\"," + "\"properties\":[],\"status\":\"CREATED\"," + "\"user_data\":{}}", json);
    PropertyKey rate = fakeObject.newPropertyKey(IdGenerator.of(2), "rate", DataType.INT, Cardinality.LIST);
    json = JsonUtil.toJson(rate);
    Assert.assertEquals("{\"id\":2,\"name\":\"rate\"," + "\"data_type\":\"INT\",\"cardinality\":\"LIST\"," + "\"aggregate_type\":\"NONE\"," + "\"write_type\":\"OLTP\"," + "\"properties\":[],\"status\":\"CREATED\"," + "\"user_data\":{}}", json);
}
Also used : FakeObjects(com.baidu.hugegraph.unit.FakeObjects) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Aggregations

PropertyKey (com.baidu.hugegraph.schema.PropertyKey)94 Id (com.baidu.hugegraph.backend.id.Id)31 Test (org.junit.Test)24 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)20 SchemaManager (com.baidu.hugegraph.schema.SchemaManager)15 HugeGraph (com.baidu.hugegraph.HugeGraph)13 EdgeLabel (com.baidu.hugegraph.schema.EdgeLabel)11 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)9 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)9 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)9 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)7 FakeObjects (com.baidu.hugegraph.unit.FakeObjects)7 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)6 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)6 DataType (com.baidu.hugegraph.type.define.DataType)6 Map (java.util.Map)6 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)5 Timed (com.codahale.metrics.annotation.Timed)5 RolesAllowed (jakarta.annotation.security.RolesAllowed)5 Collection (java.util.Collection)5