免费建设音乐网站,电站建设招聘网站,站群优化公司,搜索引擎优化的工具在第1部分中 #xff0c;我们讨论了使用Java REST绑定建立与Neo4j Server的连接。 现在让我们详细了解事务#xff0c;批处理以及REST请求的实际情况。确保org.neo4j.rest.logging_filter to true) as described in Part 1打开日志记录#xff08;将系统属性org.neo4j.rest.… 在第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 Transaction tx graphDb.beginTx();Map propsnew HashMap();props.put(id, 100);props.put(name,firstNode);Node nodegraphDb.createNode(props);props.put(id,200);props.put(name,secondNode);Node node2graphDb.createNode(props);node.createRelationshipTo(node2, DynamicRelationshipType.withName(KNOWS));tx.success();tx.finish();resultengine.query(start nnode(*) return count(n) as total, Collections.EMPTY_MAP);Iterator iteratorresult.iterator();if(iterator.hasNext()) {Map row iterator.next();out.print(Total nodes: row.get(total));} 检查日志对我来说它们默认显示在Tomcat控制台上然后查找REST调用。 上面的代码产生了 INFO: 1 * Client out-bound request
1 POST http://localhost:7474/db/data/node
1 Accept: application/json; streamtrue
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; streamtrue
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; streamtrue
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; streamtrue
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; streamtrue
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; streamtrue
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; streamtrue
4 X-Stream: true
4 Content-Type: application/json
4
{query:start nnode(*) 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; streamtrue
4 Server: Jetty(6.1.25)
4
{columns:[total],data:[[3]]} 网上总共有4个REST调用用于那段很小的代码。 您绝对希望尽可能避免这种情况。 选项1是尽可能使用Cypher。 通过不使用嵌入式样式API并切换到Cypher我们可以将前三个REST调用转换为一个。 范例2 MapString,Object propsnew HashMapString, Object();props.put(id, 100);props.put(name,firstNode);MapString,Object props2new HashMapString, Object();props2.put(id,200);props2.put(name,secondNode);MapString,Object paramsnew HashMapString, 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; streamtrue
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_transactiontrue 设置系统属性并重新运行示例1后REST调用将如下所示仅请求 INFO: 1 * Client out-bound request
1 POST http://localhost:7474/db/data/batch
1 Accept: application/json; streamtrue
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; streamtrue
2 X-Stream: true
2 Content-Type: application/json
2
{query:start nnode(*) return count(n) as total,params:{}} 您还可以显式创建批处理操作如下所示 ListNode response graphDb.executeBatch(new BatchCallbackListNode() {Overridepublic ListNode recordBatch(RestAPI batchRestApi) {ListNode nodesnew ArrayListNode();Map propsnew HashMapString, Object();props.put(id,600);nodes.add(batchRestApi.createNode(props));Map props2new HashMapString, 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; streamtrue
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绑定的上下文中的行为。 参考 Neo4j Java REST绑定–我们JCG合作伙伴 Luanne Misquitta的第2部分批处理 位于Thought Bytes博客上。 翻译自: https://www.javacodegeeks.com/2013/08/neo4j-java-rest-binding-part-2-batching.html