如何在Arangodb中创建边缘标签?

时间:2017-02-21 06:30:44

标签: arangodb

java-driver 4.1.10我创建了名为mydatabase1的数据库我用Java创建了一个图形,所以我的问题是如何使用Java代码设置边缘标签?

1 个答案:

答案 0 :(得分:2)

如果您的边缘文档中包含“边缘标签”字段,则可以在下面的代码中调用insertEdge时设置该字段。

ArangoDB arangoDB = new ArangoDB.Builder().build();

// create database
arangoDB.createDatabase("myDatabase");

// create named graph
Set<EdgeDefinition> edgeDefinitions = Collections.singleton(
  new EdgeDefinition().collection("myEdgeCollection").from("myVertexCollection").to("myVertexCollection"));
arangoDB.db("myDatabase").createGraph("myGraph", edgeDefinitions);

// create from vertex
BaseDocument from = new BaseDocument("myFromKey");
arangoDB.db("myDatabase").graph("myGraph").vertexCollection("myVertexCollection").insertVertex(from);

// create to vertex
BaseDocument to = new BaseDocument("myToKey");
arangoDB.db("myDatabase").graph("myGraph").vertexCollection("myVertexCollection").insertVertex(to);

// create edge
BaseEdgeDocument edge = new BaseEdgeDocument("myVertexCollection/myFromKey",
    "myVertexCollection/myToKey");
edge.addAttribute("label", "value");
edge.addAttribute("whatever", 42);
arangoDB.db("myDatabase").graph("myGraph").edgeCollection("myEdgeCollection").insertEdge(edge);

您也可以使用BaseEdgeDocument

,而不是使用Map<String, Object>
Map<String, Object> edge = new HashMap<String, Object>();
edge.put("_from", "myVertexCollection/myFromKey");
edge.put("_to", "myVertexCollection/myToKey");
edge.put("label", "value");
edge.put("whatever", 42);
arangoDB.db("myDatabase").graph("myGraph").edgeCollection("myEdgeCollection").in sertEdge(edge);

或创建自己的POJO代表您的优势。边缘至少需要字段_from_to。如果您不想在POJO _from和_to中为字段命名,则可以在两个DocumentField字段中使用值Type.FROMType.TO的注释String你的选择。

public class MyEdge {
  @DocumentField(Type.FROM)
  private String from;

  @DocumentField(Type.TO)
  private String to;

  public MyEdge() {}

  ..setter & getter
}