Neo4j的Rest接口使用

Neo4j的服务除了提供了基于java的客户端驱动包,同时也支持咱们经过rest服务访问它,这一点很是便捷,意味着任何支持http访问的编程语言均可以使用cypher的rest来访问neo4j,同时支持http报文以streaming的形式的返回数据,以得到更好的性能,并大幅度节省内存,固然前提是须要咱们在每一个request请求中在header中加入:java

X-Stream: true

默认想要访问neo4j服务,是须要受权认证的,第一次安装的 neo4j在登陆(http://localhost:7474)时是须要改密码的,默认是neo4j:neo4j,改完密码以后,每次登陆须要验证用户名和密码的。node

1,执行单个cpyher语句事务的http请求

查询neo4j节点总数的例子编程

curl http://192.168.10.31:7474/db/data/transaction/commit -u neo4j:dong -H "Content-Type: application/json"  -d '{"statements" : [ {    "statement" : "match (n) return count(n)" } ]}'

返回的结果以下:json

{
    "results": [
        {
            "columns": [
                "count(n)"
            ], 
            "data": [
                {
                    "row": [], 
                    "meta": []
                }
            ]
        }
    ], 
    "errors": [ ]
}

注意上面的curl语句里面:api

(1)须要加上用户名和密码,若是开启了权限认证app

(2)要设置内容类型为json数据,同时采用了post请求curl

此外,若是不须要保持打开的事务横跨多个http请求,咱们可使用打开单个事务,而后执行cypher语句,最后提交仅仅单个http请求中。编程语言

2,执行多个cpyher语句事务的http请求

咱们在同一个http请求中也能够发送多个cpyher语句,响应的结果体中会包含每一个cpyher语句结果。post

一个例子以下:性能

POST http://localhost:7474/db/data/transaction/commit
Accept: application/json; charset=UTF-8
Content-Type: application/json

post请求体

{
  "statements" : [ {
    "statement" : "CREATE (n) RETURN id(n)"
  }, {
    "statement" : "CREATE (n {props}) RETURN n",
    "parameters" : {
      "props" : {
        "name" : "My Node"
      }
    }
  } ]
}

响应:

{
  "results" : [ {
    "columns" : [ "id(n)" ],
    "data" : [ {
      "row" : [ 56 ],
      "meta" : [ null ]
    } ]
  }, {
    "columns" : [ "n" ],
    "data" : [ {
      "row" : [ {
        "name" : "My Node"
      } ],
      "meta" : [ {
        "id" : 57,
        "type" : "node",
        "deleted" : false
      } ]
    } ]
  } ],
  "errors" : [ ]
}

3,回滚打开的事务

DELETE http://localhost:7474/db/data/transaction/36
Accept: application/json; charset=UTF-8

响应的结果:

{
  "results" : [ ],
  "errors" : [ ]
}

注意上面是一个delete的rest请求。

4,开启查询的系通通计信息

POST http://localhost:7474/db/data/transaction/commit
Accept: application/json; charset=UTF-8
Content-Type: application/json

请求体以下:

{
  "statements" : [ {
    "statement" : "CREATE (n) RETURN id(n)",
    "includeStats" : true
  } ]
}

请求结果以下:

{
  "results" : [ {
    "columns" : [ "id(n)" ],
    "data" : [ {
      "row" : [ 58 ],
      "meta" : [ null ]
    } ],
    "stats" : {
      "contains_updates" : true,
      "nodes_created" : 1,
      "nodes_deleted" : 0,
      "properties_set" : 0,
      "relationships_created" : 0,
      "relationship_deleted" : 0,
      "labels_added" : 0,
      "labels_removed" : 0,
      "indexes_added" : 0,
      "indexes_removed" : 0,
      "constraints_added" : 0,
      "constraints_removed" : 0
    }
  } ],
  "errors" : [ ]
}

总结:

neo4j服务暴露的http rest接口仍是很是不错的,本篇文章简单的介绍了经过curl调用neo4j的方法,若是深刻学习或者研究,能够参考官网文档https://neo4j.com/docs/developer-manual/current/http-api/

相关文章
相关标签/搜索