Neo4j Java REST绑定–第2部分(批处理)
在第1部分中 ,我們討論了使用Java REST綁定建立與Neo4j Server的連接。 現在讓我們詳細了解事務,批處理以及REST請求的實際情況。確保org.neo4j.rest.logging_filter to true) as described in Part 1打開日志記錄(將系統屬性org.neo4j.rest.logging_filter to true) as described in Part 1設置org.neo4j.rest.logging_filter to true) as described in Part 1 。
我們將更改代碼以執行這些Neo4j API調用。
范例1:
檢查日志(對我來說,它們默認顯示在Tomcat控制臺上),然后查找REST調用。 上面的代碼產生了:
INFO: 1 * Client out-bound request 1 > POST http://localhost:7474/db/data/node 1 > Accept: application/json; stream=true 1 > X-Stream: true 1 > Content-Type: application/json 1 > {"id":100,"name":"firstNode"}INFO: 1 * Client in-bound response 1 < 201 1 < Access-Control-Allow-Origin: * 1 < Transfer-Encoding: chunked 1 < Content-Encoding: UTF-8 1 < Location: http://localhost:7474/db/data/node/1 1 < Content-Type: application/json; stream=true 1 < Server: Jetty(6.1.25) 1 < {"extensions":{},"paged_traverse":"http://localhost:7474/db/data/node/1/paged/traverse/{returnType}{?pageSize,leaseTime}","outgoing_relationships":"http://localhost:7474/db/data/node/1/relationships/out","traverse":"http://localhost:7474/db/data/node/1/traverse/{returnType}","all_typed_relationships":"http://localhost:7474/db/data/node/1/relationships/all/{-list|&|types}","property":"http://localhost:7474/db/data/node/1/properties/{key}","all_relationships":"http://localhost:7474/db/data/node/1/relationships/all","self":"http://localhost:7474/db/data/node/1","properties":"http://localhost:7474/db/data/node/1/properties","outgoing_typed_relationships":"http://localhost:7474/db/data/node/1/relationships/out/{-list|&|types}","incoming_relationships":"http://localhost:7474/db/data/node/1/relationships/in","incoming_typed_relationships":"http://localhost:7474/db/data/node/1/relationships/in/{-list|&|types}","create_relationship":"http://localhost:7474/db/data/node/1/relationships","data":{"name":"firstNode","id":100}}INFO: 2 * Client out-bound request 2 > POST http://localhost:7474/db/data/node 2 > Accept: application/json; stream=true 2 > X-Stream: true 2 > Content-Type: application/json 2 > {"id":200,"name":"secondNode"}INFO: 2 * Client in-bound response 2 < 201 2 < Access-Control-Allow-Origin: * 2 < Transfer-Encoding: chunked 2 < Content-Encoding: UTF-8 2 < Location: http://localhost:7474/db/data/node/2 2 < Content-Type: application/json; stream=true 2 < Server: Jetty(6.1.25) 2 < {"extensions":{},"paged_traverse":"http://localhost:7474/db/data/node/2/paged/traverse/{returnType}{?pageSize,leaseTime}","outgoing_relationships":"http://localhost:7474/db/data/node/2/relationships/out","traverse":"http://localhost:7474/db/data/node/2/traverse/{returnType}","all_typed_relationships":"http://localhost:7474/db/data/node/2/relationships/all/{-list|&|types}","property":"http://localhost:7474/db/data/node/2/properties/{key}","all_relationships":"http://localhost:7474/db/data/node/2/relationships/all","self":"http://localhost:7474/db/data/node/2","properties":"http://localhost:7474/db/data/node/2/properties","outgoing_typed_relationships":"http://localhost:7474/db/data/node/2/relationships/out/{-list|&|types}","incoming_relationships":"http://localhost:7474/db/data/node/2/relationships/in","incoming_typed_relationships":"http://localhost:7474/db/data/node/2/relationships/in/{-list|&|types}","create_relationship":"http://localhost:7474/db/data/node/2/relationships","data":{"name":"secondNode","id":200}}INFO: 3 * Client out-bound request 3 > POST http://localhost:7474/db/data/node/1/relationships 3 > Accept: application/json; stream=true 3 > X-Stream: true 3 > Content-Type: application/json 3 > {"to":"http://localhost:7474/db/data/node/2","type":"KNOWS"}INFO: 3 * Client in-bound response 3 < 201 3 < Access-Control-Allow-Origin: * 3 < Transfer-Encoding: chunked 3 < Content-Encoding: UTF-8 3 < Location: http://localhost:7474/db/data/relationship/0 3 < Content-Type: application/json; stream=true 3 < Server: Jetty(6.1.25) 3 < {"extensions":{},"start":"http://localhost:7474/db/data/node/1","property":"http://localhost:7474/db/data/relationship/0/properties/{key}","self":"http://localhost:7474/db/data/relationship/0","properties":"http://localhost:7474/db/data/relationship/0/properties","type":"KNOWS","end":"http://localhost:7474/db/data/node/2","data":{}}INFO: 4 * Client out-bound request 4 > POST http://localhost:7474/db/data/cypher 4 > Accept: application/json; stream=true 4 > X-Stream: true 4 > Content-Type: application/json 4 > {"query":"start n=node(*) return count(n) as total","params":{}}INFO: 4 * Client in-bound response 4 < 200 4 < Access-Control-Allow-Origin: * 4 < Transfer-Encoding: chunked 4 < Content-Encoding: UTF-8 4 < Content-Type: application/json; stream=true 4 < Server: Jetty(6.1.25) 4 < {"columns":["total"],"data":[[3]]}網上總共有4個REST調用,用于那段很小的代碼。 您絕對希望盡可能避免這種情況。 選項1是盡可能使用Cypher。 通過不使用嵌入式樣式API并切換到Cypher,我們可以將前三個REST調用轉換為一個。
范例2:
Map<String,Object> props=new HashMap<String, Object>();props.put("id", 100);props.put("name","firstNode");Map<String,Object> props2=new HashMap<String, Object>();props2.put("id",200);props2.put("name","secondNode");Map<String,Object> params=new HashMap<String, Object>();params.put("props1",props);params.put("props2",props2);engine.query("create (n1 {props1})-[:KNOWS]->(n2 {props2})", params);這將產生:
1 > POST http://localhost:7474/db/data/cypher {"query":"create (n1 {props1})-[:KNOWS]->(n2 {props2})","params":{"props1":{"id":100,"name":"firstNode"},"props2":{"id":100,"name":"firstNode"}}}Jul 24, 2013 10:38:47 PM com.sun.jersey.api.client.filter.LoggingFilter log INFO: 1 * Client in-bound response 1 < 200 1 < Access-Control-Allow-Origin: * 1 < Transfer-Encoding: chunked 1 < Content-Encoding: UTF-8 1 < Content-Type: application/json; stream=true 1 < Server: Jetty(6.1.25) 1 < {"columns":[],"data":[]}批處理事務中的所有操作
https://github.com/neo4j/java-rest-binding上的文檔指出:
“在1.8中,它嘗試將tx中的所有操作收集為批處理操作,然后將在服務器上執行該批處理操作。 這暗示著在“ tx”中檢索到的結果不是立即可用的,而是僅在調用tx.success和tx.finish之后才可用。
但是,請注意,這不是從示例1中看到的默認行為。要啟用此功能,您需要設置以下系統屬性: org.neo4j.rest.batch_transaction=true
設置系統屬性并重新運行示例1后,REST調用將如下所示(僅請求):
INFO: 1 * Client out-bound request 1 > POST http://localhost:7474/db/data/batch 1 > Accept: application/json; stream=true 1 > X-Stream: true 1 > Content-Type: application/json 1 > [{"id":1,"to":"node","body":{"id":200,"name":"secondNode"},"method":"POST"},{"id":2,"to":"node","body":{"id":200,"name":"secondNode"},"method":"POST"},{"id":3,"to":"{1}/relationships","body":{"to":"{2}","type":"KNOWS"},"method":"POST"}]INFO: 2 * Client out-bound request 2 > POST http://localhost:7474/db/data/cypher 2 > Accept: application/json; stream=true 2 > X-Stream: true 2 > Content-Type: application/json 2 > {"query":"start n=node(*) return count(n) as total","params":{}}您還可以顯式創建批處理操作,如下所示:
List<Node> response =graphDb.executeBatch(new BatchCallback<List<Node>>() {@Overridepublic List<Node> recordBatch(RestAPI batchRestApi) {List<Node> nodes=new ArrayList<Node>();Map props=new HashMap<String, Object>();props.put("id",600);nodes.add(batchRestApi.createNode(props));Map props2=new HashMap<String, Object>();props2.put("id",500);nodes.add(batchRestApi.createNode(props2));return nodes;}});轉換為:
INFO: 1 * Client out-bound request 1 > POST http://localhost:7474/db/data/batch 1 > Accept: application/json; stream=true 1 > X-Stream: true 1 > Content-Type: application/json 1 > [{"id":1,"to":"node","body":{"id":600},"method":"POST"},{"id":2,"to":"node","body":{"id":500},"method":"POST"}] 強烈建議在細粒度的Neo4j Java API上使用任何Cypher / Batching方法。 在最后一篇文章中,我們將研究事務在REST綁定的上下文中的行為。
翻譯自: https://www.javacodegeeks.com/2013/08/neo4j-java-rest-binding-part-2-batching.html
總結
以上是生活随笔為你收集整理的Neo4j Java REST绑定–第2部分(批处理)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 上卿是什么职位啊 关于上卿职位的介绍
- 下一篇: 三个敬畏是指什么 到底什么是三个敬畏