use of com.baidu.hugegraph.schema.EdgeLabel in project incubator-hugegraph by apache.
the class EdgeLabelAPI method get.
@GET
@Timed
@Path("{name}")
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=edge_label_read" })
public String get(@Context GraphManager manager, @PathParam("graph") String graph, @PathParam("name") String name) {
LOG.debug("Graph [{}] get edge label by name '{}'", graph, name);
HugeGraph g = graph(manager, graph);
EdgeLabel edgeLabel = g.schema().getEdgeLabel(name);
return manager.serializer(g).writeEdgeLabel(edgeLabel);
}
use of com.baidu.hugegraph.schema.EdgeLabel in project incubator-hugegraph by apache.
the class EdgeLabelAPI method update.
@PUT
@Timed
@Path("{name}")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=edge_label_write" })
public String update(@Context GraphManager manager, @PathParam("graph") String graph, @PathParam("name") String name, @QueryParam("action") String action, JsonEdgeLabel jsonEdgeLabel) {
LOG.debug("Graph [{}] {} edge label: {}", graph, action, jsonEdgeLabel);
checkUpdatingBody(jsonEdgeLabel);
E.checkArgument(name.equals(jsonEdgeLabel.name), "The name in url(%s) and body(%s) are different", name, jsonEdgeLabel.name);
// Parse action param
boolean append = checkAndParseAction(action);
HugeGraph g = graph(manager, graph);
EdgeLabel.Builder builder = jsonEdgeLabel.convert2Builder(g);
EdgeLabel edgeLabel = append ? builder.append() : builder.eliminate();
return manager.serializer(g).writeEdgeLabel(edgeLabel);
}
use of com.baidu.hugegraph.schema.EdgeLabel 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);
}
use of com.baidu.hugegraph.schema.EdgeLabel in project incubator-hugegraph by apache.
the class JsonUtilTest method testSerializeEdgeLabel.
@Test
public void testSerializeEdgeLabel() {
FakeObjects fakeObject = new FakeObjects();
PropertyKey name = fakeObject.newPropertyKey(IdGenerator.of(1), "name");
PropertyKey age = fakeObject.newPropertyKey(IdGenerator.of(2), "age", DataType.INT, Cardinality.SINGLE);
PropertyKey city = fakeObject.newPropertyKey(IdGenerator.of(3), "city");
PropertyKey date = fakeObject.newPropertyKey(IdGenerator.of(4), "date", DataType.DATE);
PropertyKey weight = fakeObject.newPropertyKey(IdGenerator.of(5), "weight", DataType.DOUBLE);
VertexLabel vl = fakeObject.newVertexLabel(IdGenerator.of(1), "person", IdStrategy.CUSTOMIZE_NUMBER, name.id(), age.id(), city.id());
EdgeLabel el = fakeObject.newEdgeLabel(IdGenerator.of(1), "knows", Frequency.SINGLE, vl.id(), vl.id(), date.id(), weight.id());
Mockito.when(fakeObject.graph().vertexLabel(vl.id())).thenReturn(vl);
Mockito.when(fakeObject.graph().mapPkId2Name(el.properties())).thenReturn(Arrays.asList(date.name(), weight.name()));
String json = JsonUtil.toJson(el);
Assert.assertEquals("{\"id\":1,\"name\":\"knows\"," + "\"source_label\":\"person\"," + "\"target_label\":\"person\"," + "\"frequency\":\"SINGLE\",\"sort_keys\":[]," + "\"nullable_keys\":[],\"index_labels\":[]," + "\"properties\":[\"date\",\"weight\"]," + "\"status\":\"CREATED\"," + "\"ttl\":0,\"enable_label_index\":true," + "\"user_data\":{}}", json);
}
use of com.baidu.hugegraph.schema.EdgeLabel in project incubator-hugegraph by apache.
the class SchemaTransaction method removePropertyKey.
@Watched(prefix = "schema")
public Id removePropertyKey(Id id) {
LOG.debug("SchemaTransaction remove property key '{}'", id);
PropertyKey propertyKey = this.getPropertyKey(id);
// If the property key does not exist, return directly
if (propertyKey == null) {
return null;
}
List<VertexLabel> vertexLabels = this.getVertexLabels();
for (VertexLabel vertexLabel : vertexLabels) {
if (vertexLabel.properties().contains(id)) {
throw new NotAllowException("Not allowed to remove property key: '%s' " + "because the vertex label '%s' is still using it.", propertyKey, vertexLabel.name());
}
}
List<EdgeLabel> edgeLabels = this.getEdgeLabels();
for (EdgeLabel edgeLabel : edgeLabels) {
if (edgeLabel.properties().contains(id)) {
throw new NotAllowException("Not allowed to remove property key: '%s' " + "because the edge label '%s' is still using it.", propertyKey, edgeLabel.name());
}
}
if (propertyKey.oltp()) {
this.removeSchema(propertyKey);
return IdGenerator.ZERO;
} else {
return this.removeOlapPk(propertyKey);
}
}
Aggregations