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:


Transaction tx = graphDb.beginTx();
    Map props=new HashMap();
    props.put("id", 100);
    props.put("name","firstNode");
    Node node=graphDb.createNode(props);

    props.put("id",200);
    props.put("name","secondNode");
    Node node2=graphDb.createNode(props);

    node.createRelationshipTo(node2, DynamicRelationshipType.withName("KNOWS"));

    tx.success();
    tx.finish();
    
    result=engine.query("start n=node(*) return count(n) as total", Collections.EMPTY_MAP);
    Iterator iterator=result.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; 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>>() {

            @Override
            public 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绑定的上下文中的行为。

参考: Neo4j Java REST绑定–我们JCG合作伙伴 Luanne Misquitta的第2部分(批处理) ,位于Thought Bytes博客上。

翻译自: https://www.javacodegeeks.com/2013/08/neo4j-java-rest-binding-part-2-batching.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值