use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.
the class VertexAPI method update.
@PUT
@Timed(name = "single-update")
@Path("{id}")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=vertex_write" })
public String update(@Context GraphManager manager, @PathParam("graph") String graph, @PathParam("id") String idValue, @QueryParam("action") String action, JsonVertex jsonVertex) {
LOG.debug("Graph [{}] update vertex: {}", graph, jsonVertex);
checkUpdatingBody(jsonVertex);
Id id = checkAndParseVertexId(idValue);
// Parse action param
boolean append = checkAndParseAction(action);
HugeGraph g = graph(manager, graph);
HugeVertex vertex = (HugeVertex) g.vertex(id);
VertexLabel vertexLabel = vertex.schemaLabel();
for (String key : jsonVertex.properties.keySet()) {
PropertyKey pkey = g.propertyKey(key);
E.checkArgument(vertexLabel.properties().contains(pkey.id()), "Can't update property for vertex '%s' because " + "there is no property key '%s' in its vertex label", id, key);
}
commit(g, () -> updateProperties(vertex, jsonVertex, append));
return manager.serializer(g).writeVertex(vertex);
}
use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.
the class VertexLabelAPI method create.
@POST
@Timed
@Status(Status.CREATED)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=vertex_label_write" })
public String create(@Context GraphManager manager, @PathParam("graph") String graph, JsonVertexLabel jsonVertexLabel) {
LOG.debug("Graph [{}] create vertex label: {}", graph, jsonVertexLabel);
checkCreatingBody(jsonVertexLabel);
HugeGraph g = graph(manager, graph);
VertexLabel.Builder builder = jsonVertexLabel.convert2Builder(g);
VertexLabel vertexLabel = builder.create();
return manager.serializer(g).writeVertexLabel(vertexLabel);
}
use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.
the class HugeVariables method initSchemaIfNeeded.
public synchronized void initSchemaIfNeeded() {
if (this.params.graph().existsVertexLabel(Hidden.hide(VARIABLES))) {
// Ignore if exist
return;
}
createPropertyKey(Hidden.hide(VARIABLE_KEY), DataType.TEXT, Cardinality.SINGLE);
createPropertyKey(Hidden.hide(VARIABLE_TYPE), DataType.TEXT, Cardinality.SINGLE);
createPropertyKey(Hidden.hide(BYTE_VALUE), DataType.BYTE, Cardinality.SINGLE);
createPropertyKey(Hidden.hide(BOOLEAN_VALUE), DataType.BOOLEAN, Cardinality.SINGLE);
createPropertyKey(Hidden.hide(INTEGER_VALUE), DataType.INT, Cardinality.SINGLE);
createPropertyKey(Hidden.hide(LONG_VALUE), DataType.LONG, Cardinality.SINGLE);
createPropertyKey(Hidden.hide(FLOAT_VALUE), DataType.FLOAT, Cardinality.SINGLE);
createPropertyKey(Hidden.hide(DOUBLE_VALUE), DataType.DOUBLE, Cardinality.SINGLE);
createPropertyKey(Hidden.hide(STRING_VALUE), DataType.TEXT, Cardinality.SINGLE);
createPropertyKey(Hidden.hide(BYTE_VALUE + LIST), DataType.BYTE, Cardinality.LIST);
createPropertyKey(Hidden.hide(BOOLEAN_VALUE + LIST), DataType.BOOLEAN, Cardinality.LIST);
createPropertyKey(Hidden.hide(INTEGER_VALUE + LIST), DataType.INT, Cardinality.LIST);
createPropertyKey(Hidden.hide(LONG_VALUE + LIST), DataType.LONG, Cardinality.LIST);
createPropertyKey(Hidden.hide(FLOAT_VALUE + LIST), DataType.FLOAT, Cardinality.LIST);
createPropertyKey(Hidden.hide(DOUBLE_VALUE + LIST), DataType.DOUBLE, Cardinality.LIST);
createPropertyKey(Hidden.hide(STRING_VALUE + LIST), DataType.TEXT, Cardinality.LIST);
createPropertyKey(Hidden.hide(BYTE_VALUE + SET), DataType.BYTE, Cardinality.SET);
createPropertyKey(Hidden.hide(BOOLEAN_VALUE + SET), DataType.BOOLEAN, Cardinality.SET);
createPropertyKey(Hidden.hide(INTEGER_VALUE + SET), DataType.INT, Cardinality.SET);
createPropertyKey(Hidden.hide(LONG_VALUE + SET), DataType.LONG, Cardinality.SET);
createPropertyKey(Hidden.hide(FLOAT_VALUE + SET), DataType.FLOAT, Cardinality.SET);
createPropertyKey(Hidden.hide(DOUBLE_VALUE + SET), DataType.DOUBLE, Cardinality.SET);
createPropertyKey(Hidden.hide(STRING_VALUE + SET), DataType.TEXT, Cardinality.SET);
String[] properties = { Hidden.hide(VARIABLE_KEY), Hidden.hide(VARIABLE_TYPE) };
properties = ArrayUtils.addAll(properties, TYPES);
SchemaManager schema = this.params.graph().schema();
VertexLabel variables = schema.vertexLabel(Hidden.hide(VARIABLES)).properties(properties).usePrimaryKeyId().primaryKeys(Hidden.hide(VARIABLE_KEY)).nullableKeys(TYPES).build();
this.params.schemaTransaction().addVertexLabel(variables);
LOG.debug("Variables schema created");
}
use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.
the class HugeVariables method createVariableVertex.
private void createVariableVertex(String key, Object value) {
VertexLabel vl = this.variableVertexLabel();
GraphTransaction tx = this.params.graphTransaction();
HugeVertex vertex = HugeVertex.create(tx, null, vl);
try {
this.setProperty(vertex, key, value);
} catch (IllegalArgumentException e) {
throw Graph.Variables.Exceptions.dataTypeOfVariableValueNotSupported(value, e);
}
// PrimaryKey id
vertex.assignId(null);
tx.addVertex(vertex);
}
use of com.baidu.hugegraph.schema.VertexLabel 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);
}
Aggregations