Neo4j的服務除了提供了基於java的客戶端驅動包,同時也支持咱們經過rest服務訪問它,這一點很是便捷,意味着任何支持http訪問的編程語言均可以使用cypher的rest來訪問neo4j,同時支持http報文以streaming的形式的返回數據,以得到更好的性能,並大幅度節省內存,固然前提是須要咱們在每一個request請求中在header中加入:java
X-Stream: true
默認想要訪問neo4j服務,是須要受權認證的,第一次安裝的 neo4j在登陸(http://localhost:7474)時是須要改密碼的,默認是neo4j:neo4j,改完密碼以後,每次登陸須要驗證用戶名和密碼的。node
查詢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請求中。編程語言
咱們在同一個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" : [ ] }
DELETE http://localhost:7474/db/data/transaction/36 Accept: application/json; charset=UTF-8
響應的結果:
{ "results" : [ ], "errors" : [ ] }
注意上面是一個delete的rest請求。
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/