推特 阿里云技术文档正文

HTTP REST_SDK参考_图数据库GDB

admin 阿里云技术文档 2020-02-11 191 0
阿里云服务器优惠

HTTP REST

本文介绍如何使用HTTP REST方式连接和操作图数据库GDB。REST API兼容Gremlin3.3.x和3.4.0版本。

进行以下操作时,请确保图数据库GDB的实例与您的ECS虚拟机处于同一个Virtual Private Cloud(VPC)网络环境。

以下范例中,需要将配置信息替换成您的图数据库实例的相关信息:

  • ${your-gdb-endpoint}改为您的图数据库GDB实例的域名
  • ${username}改为您的图数据库GDB实例的用户名
  • ${password}改为您的图数据库GDB实例的密码

1、图数据库GDB的HTTP接入点为${your_gdb_endpoint}:8182/gremlin

2、连接的时候可以使用GET请求,也可以使用POST请求。但是建议使用POST请求进行操作:

  1. curl -u ${username}:${password} -X POST -d '{"gremlin":"g.V().limit(1)"}' http://${your_gdb_endpoint}:8182/gremlin
  2. //此处注意需要对label值的双引号进行转义
  3. curl -u ${username}:${password} -X POST -d '{"gremlin":"gremlin:g.V().hasLabel(\"movie\").limit(10)"}' http://${your_gdb_endpoint}:8182/gremlin
  4. curl -u ${username}:${password} "http://${your_gdb_endpoint}:8182/gremlin?gremlin=g.V().limit(1)"

3、使用REST请求访问服务器的时候,可以以binding形式携带参数:

  1. curl -u ${username}:${password} -X POST -d '{"gremlin":"g.V().limit(n)","bindings":{"n":1}}' http://${your_gdb_endpoint}:8182/gremlin

4、返回结果范例为:

  1. {
  2. "requestId": "e640005a-f2fd-403e-812c-f61e7a3fb7cb",
  3. "result": {
  4. "data": {
  5. "@type": "g:List",
  6. "@value": [
  7. {
  8. "@type": "g:Vertex",
  9. "@value": {
  10. "id": "3a63cc90-d957-4324-9ffc-16a8e4c1c1f4",
  11. "label": "label",
  12. "properties": {
  13. "pint": [
  14. {
  15. "@type": "g:VertexProperty",
  16. "@value": {
  17. "id": "3a63cc90-d957-4324-9ffc-16a8e4c1c1f4",
  18. "label": "label",
  19. "value": {
  20. "@type": "g:Int32",
  21. "@value": 3
  22. }
  23. }
  24. }
  25. ]
  26. }
  27. }
  28. }
  29. ]
  30. },
  31. "meta": {
  32. "@type": "g:Map",
  33. "@value": []
  34. }
  35. },
  36. "status": {
  37. "attributes": {
  38. "@type": "g:Map",
  39. "@value": []
  40. },
  41. "code": 200,
  42. "message": ""
  43. }
  44. }

更多dsl示例

  • 上面的例子是使用g.V().limit(n)遍历返回GDB中的部分点。要查询其他内容,可以替换成其他的DSL
  • 其他dsl范例如下,图的点、边结构链接 http://tinkerpop.apache.org/docs/current/reference/#traversal
  • 注意下面dsl需要改造成参数化的调用方式, 利用 gremlin + bindings来构造
    1. DSL硬编码
    2. g.V().hasLabel('gdb_sample_person').drop()
    3. |
    4. |
    5. V
    6. 参数化调用
    7. g.V().hasLabel(label).drop()
    8. bindings:{"label":"gdb_sample_person"}

  1. 删除指定label的点、边

    1. g.E().hasLabel('gdb_sample_knows').drop()
    2. g.E().hasLabel('gdb_sample_created').drop()
    3. g.V().hasLabel('gdb_sample_person').drop()
    4. g.V().hasLabel('gdb_sample_software').drop()
  2. 添加顶点,为其设置id、property

    1. g.addV('gdb_sample_person').property(id, 'gdb_sample_marko').property('age', 28).property('name', 'marko')
    2. g.addV('gdb_sample_person').property(id, 'gdb_sample_vadas').property('age', 27).property('name', 'vadas')
    3. g.addV('gdb_sample_person').property(id, 'gdb_sample_josh').property('age', 32).property('name', 'josh')
    4. g.addV('gdb_sample_person').property(id, 'gdb_sample_peter').property('age', 35).property('name', 'peter')
    5. g.addV('gdb_sample_software').property(id, 'gdb_sample_lop').property('lang', 'java').property('name', 'lop')
    6. g.addV('gdb_sample_software').property(id, 'gdb_sample_ripple').property('lang', 'java').property('name', 'ripple')
  3. 修改(或新增) age 属性

    1. g.V('gdb_sample_marko').property('age', 29)
  4. 建立关系,设置属性 weight

    1. g.addE('gdb_sample_knows').from(V('gdb_sample_marko')).to(V('gdb_sample_vadas')).property('weight', 0.5f)
    2. g.addE('gdb_sample_knows').from(V('gdb_sample_marko')).to(V('gdb_sample_josh')).property('weight', 1.0f)
    3. g.addE('gdb_sample_created').from(V('gdb_sample_marko')).to(V('gdb_sample_lop')).property('weight', 0.4f)
    4. g.addE('gdb_sample_created').from(V('gdb_sample_josh')).to(V('gdb_sample_lop')).property('weight', 0.4f)
    5. g.addE('gdb_sample_created').from(V('gdb_sample_josh')).to(V('gdb_sample_ripple')).property('weight', 1.0f)
    6. g.addE('gdb_sample_created').from(V('gdb_sample_peter')).to(V('gdb_sample_lop')).property('weight', 0.2f)
  5. 查询所有点/指定label的点数量

    1. g.V().count()
    2. g.V().hasLabel('gdb_sample_person').count()
  6. 查询指定条件的顶点 (>29岁的人, 按name降序排列所有人)

    1. g.V().hasLabel('gdb_sample_person').has('age', gt(29))
    2. g.V().hasLabel('gdb_sample_person').order().by('name', decr)
  7. 关联查询(获取 marko 认识的人, marko认识的人created的software)

    1. g.V('gdb_sample_marko').outE('gdb_sample_knows').inV().hasLabel('gdb_sample_person')
    2. g.V('gdb_sample_marko').outE('gdb_sample_knows').inV().hasLabel('gdb_sample_person').outE('gdb_sample_created').inV().hasLabel('gdb_sample_software')
  8. 删除关系、顶点

    1. g.V('gdb_sample_marko').outE('gdb_sample_knows').where(inV().has(id, 'gdb_sample_josh')).drop()
    2. g.V('gdb_sample_marko').drop()

有关Gremlin REST接口的更多信息,请参阅Apache TinkerPop3文档中的Connecting via HTTP

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论

-----