全部产品
云市场

快速入门

更新时间:2018-12-29 11:24:38

打开图表

gremlin-server内部已经打开了graph,直接在gremlin-console里面直接使用graph操作

举例来说,有一个现成的图形示例GraphOfTheGods,可以将其加载到图形中,以便为用户提供遍历的内容。下图模型的样子,包括它的所有顶点,边和属性。gods

之后您可以直接加载图库或者step by step 创建示例库

直接加载示例库

在hGraphDB上,您调用GraphOfTheGods.load(graph)方法,将其传递给一个打开的图形,这将为您创建该示例模型。

  • 打开gremlin-console,推荐websocket方式接入
  1. gremlin> :remote connect tinkerpop.server conf/remote.yaml session
  2. ==>Configured localhost/127.0.0.1:8188-[8fdb206d-5768-40e3-8860-81afab8a0e26]
  3. gremlin> :remote console

step by step创建示例库

打开gremlin-console

推荐websocket方式接入

  1. gremlin> :remote connect tinkerpop.server conf/remote.yaml session
  2. ==>Configured localhost/127.0.0.1:8188-[8fdb206d-5768-40e3-8860-81afab8a0e26]
  3. gremlin> :remote console

创建schema

  • 创建vertexLabel在gremlin-console逐条输入这些语句

    1. graph.createLabel(ElementType.VERTEX, "titan", ValueType.STRING, "name",
    2. ValueType.STRING, "age", ValueType.INT);
    3. graph.createLabel(ElementType.VERTEX, "location", ValueType.STRING, "name",
    4. ValueType.STRING);
    5. graph.createLabel(ElementType.VERTEX, "god", ValueType.STRING, "name",
    6. ValueType.STRING, "age", ValueType.INT);
    7. graph.createLabel(ElementType.VERTEX, "demigod", ValueType.STRING, "name",
    8. ValueType.STRING, "age", ValueType.INT);
    9. graph.createLabel(ElementType.VERTEX, "human", ValueType.STRING, "name",
    10. ValueType.STRING, "age", ValueType.INT);
    11. graph.createLabel(ElementType.VERTEX, "monster", ValueType.STRING, "name",
    12. ValueType.STRING, "age", ValueType.INT);
  • 创建索引

  1. graph.createIndex(ElementType.VERTEX, "titan", "name",true);
  2. graph.createIndex(ElementType.VERTEX, "location", "name", true);
  3. graph.createIndex(ElementType.VERTEX, "god", "name", true);
  4. graph.createIndex(ElementType.VERTEX, "demigod", "name", true);
  5. graph.createIndex(ElementType.VERTEX, "human", "name", true);
  6. graph.createIndex(ElementType.VERTEX, "monster", "name", true);
  • 创建边label
  1. graph.createLabel(ElementType.EDGE, "father", ValueType.STRING);
  2. graph.createLabel(ElementType.EDGE, "mother", ValueType.STRING);
  3. graph.createLabel(ElementType.EDGE, "battled", ValueType.STRING, "time", ValueType.INT,"place", ValueType.STRING);
  4. graph.createLabel(ElementType.EDGE, "lives", ValueType.STRING, "reason", ValueType.STRING);
  5. graph.createLabel(ElementType.EDGE, "pet", ValueType.STRING);
  6. graph.createLabel(ElementType.EDGE, "brother", ValueType.STRING);
  7. graph.connectLabels("god", "father", "titan");
  8. graph.connectLabels("demigod", "father", "god");
  9. graph.connectLabels("demigod","mother","human");
  10. graph.connectLabels("demigod","battled","monster");
  11. graph.connectLabels("god","lives","location");
  12. graph.connectLabels("monster","lives","location");
  13. graph.connectLabels("god","pet","monster");
  14. graph.connectLabels("god","brother","god");
  • 创建边的索引
  1. graph.createIndex(ElementType.EDGE, "battled", "time");

边必须显示的连接顶点label(connectLabels),之后才可以开始插入数据

  • 导入数据
  1. // vertices data
  2. saturn = graph.addVertex(T.label, "titan", T.id, "saturn","name", "saturn", "age", 10000);
  3. sky = graph.addVertex(T.label, "location", T.id, "sky","name", "sky");
  4. sea = graph.addVertex(T.label, "location", T.id, "sea", "name", "sea");
  5. jupiter = graph.addVertex(T.label, "god", T.id, "jupiter", "name", "jupiter", "age", 5000);
  6. neptune = graph.addVertex(T.label, "god", T.id, "neptune", "name", "neptune", "age", 4500);
  7. hercules = graph.addVertex(T.label, "demigod", T.id, "hercules", "name", "hercules", "age", 30);
  8. alcmene = graph.addVertex(T.label, "human", T.id, "alcmene", "name", "alcmene", "age", 45);
  9. pluto = graph.addVertex(T.label, "god", T.id, "pluto", "name", "pluto", "age", 4000);
  10. nemean = graph.addVertex(T.label, "monster", T.id, "nemean", "name", "nemean");
  11. hydra = graph.addVertex(T.label, "monster", T.id, "hydra", "name", "hydra");
  12. cerberus = graph.addVertex(T.label, "monster", T.id, "cerberus", "name", "cerberus");
  13. tartarus = graph.addVertex(T.label, "location", T.id, "tartarus", "name", "tartarus");
  14. // edges data
  15. jupiter.addEdge("father", saturn);
  16. jupiter.addEdge("lives", sky, "reason", "loves fresh breezes");
  17. jupiter.addEdge("brother", neptune);
  18. jupiter.addEdge("brother", pluto);
  19. neptune.addEdge("lives", sea).property("reason", "loves waves");
  20. neptune.addEdge("brother", jupiter);
  21. neptune.addEdge("brother", pluto);
  22. hercules.addEdge("father", jupiter);
  23. hercules.addEdge("mother", alcmene);
  24. hercules.addEdge("battled", nemean, "time", 1, "place", "nemean's home");
  25. hercules.addEdge("battled", hydra, "time", 2, "place", "hydra's home");
  26. hercules.addEdge("battled", cerberus, "time", 12, "place", "cerberus's home");
  27. pluto.addEdge("brother", jupiter);
  28. pluto.addEdge("brother", neptune);
  29. pluto.addEdge("lives", tartarus, "reason", "no fear of death");
  30. pluto.addEdge("pet", cerberus);
  31. cerberus.addEdge("lives", tartarus);

直接批量加载实例Gods图库

直接执行下面的语句可以达到上述逐条执行语句同样的目的

  1. GraphOfTheGodsFactory.load(graph)

遍历

server端已经内置变量g遍历源,可直接使用

  1. gremlin> g
  2. ==>graphtraversalsource[hbasegraph[hbasegraph], standard]

可直接使用graph图表和g遍历源,现在我们可以开始遍历图表了。

使用全局索引遍历

在Gods图形图中,它创建了一个名称属性的全局索引。这种索引通常是您到达图表中特定点的第一步。例如,假设我们希望在图中找到“saturn”:

gremlin

  1. saturn=g.V().has("titan","name", "saturn").next()

它返回具有该属性的顶点id name: saturn。像这样:v[saturn]。

为了进一步举例,通过在变量’saturn’中定义它来使用“saturn”顶点,你可以询问哪些顶点存储了一个指向它的’father’标签的边。使用该in(“father”)方法选择那些传入的边,并使用values(“name”)返回该顶点的name属性的值。

  1. saturn=g.V("saturn").next();
  2. g.V(saturn).in("father").values("name");

这会返回saturn 孩子的名字,虽然只有唯一的孩子:“jupiter”。

in(“father”)第二次重复这个操作,你正在看着saturn顶点的孙子。

  1. saturn=g.V( "saturn").next();
  2. g.V(saturn).in("father").in("father").values("name");

这将返回“hercules”。

更复杂的查询

gremlin

  1. g.E().has("place", "nemean's home").as("source").inV().as("god2").select("source").outV().as("god1").select("god1", "god2").by("name");

(此查询可以细分为:将.inV用作’god2’作为源,将.outV用作’god1’,然后使用god1,god2并获取其’name’属性的值。)

这将输出:

  1. {god1=hercules, god2=nemean}

遍历顶点

先前我们使用两个in()元素来确定hercules是saturn的孙子。这也可以表示为循环。gremlin可repeat一个谓词:

  1. saturn=g.V().has("name", "saturn").next();
  2. hercules=g.V(saturn).repeat(__.in("father")).times(2).next();

一旦我们拥有了Hercules顶点,我们可以,例如,遍历标记为“父亲”和“母亲”的边,并确定Hercules父母的“类型”,或者遍历所有“战斗”边以查看他与谁战斗:

gremlin

  1. gremlin> def parents=g.V(hercules).out("father", "mother").label();
  2. ==>god
  3. ==>human
  4. gremlin> def fought=g.V(hercules).out("battled").values("name");
  5. ==>nemean
  6. ==>hydra
  7. ==>cerberus
  8. gremlin> def fought_once=g.V(hercules).outE("battled").has("time", gt(1)).inV().values("name");
  9. ==>hydra
  10. ==>cerberus

清空上述测试数据,clear

  1. gremlin> graph.clear()
  2. gremlin> :q