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>>() {@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绑定的上下文中的行为。

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/367607.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

selector简介

最近在学习java NIO&#xff0c;发现java nio selector 相对 channel ,buffer 这两个概念是比较难理解的 ,把学习理解的东西以文字的东西记录下来&#xff0c;就像从内存落地到硬盘&#xff0c;把内存中内容换成该知识点的索引。 在介绍Selector之前先明确以下3个问题&#…

java随机数排序算法_理解快速排序算法

快速排序在平均状况下&#xff0c;排序n个项目要Ο(n log n)次比较。在最坏状况下则需要Ο(n^2)次比较&#xff0c;但这种状况并不常见。事实上&#xff0c;快速排序通常明显比 其他Ο(n log n)算法更快&#xff0c;因为它的内部循环(inner loop)可以在大部分的架构上很有效率地…

开课吧视频内容汇总

1. 前端读取文件内容&#xff0c; FileReader对象 2. 用户联网状态 3. application/x-www-form-urlencoded 参数序列化 &#xff08;具体借鉴jquery的$.param方法&#xff09;&#xff0c;后端接收到的数据格式是 a[0][a] 1,并不会将其整理成对象或者数组 var nextStr ;funct…

PhantomJS宣布终止开发

由于没有人贡献代码&#xff0c;我很快就要把这个项目归档了。也许未来的某一天&#xff0c;我又想开发了&#xff0c;还会重新启动起来。既然决定停止开发了&#xff0c;那么自然地&#xff0c;PhantomJS 2.5 和原定的 PhantomJS2.1.x 就告吹了。对应的&#xff0c;这两个版本…

Servlet和JSP中的文件上传示例

使用Servlet和JSP将文件上传到服务器是Java Web应用程序中的常见任务。 在对Servlet或JSP进行编码以处理文件上传请求之前&#xff0c;您需要了解一点有关HTML和HTTP协议中文件上传支持的知识。 如果要让用户从文件系统中选择文件并上传到服务器&#xff0c;则需要使用<inpu…

20165312-第4周-课上内容补做以及知识点总结

20165312-第4周-课上内容补做以及知识点总结 1、课上内容补做 教材代码完成情况测试p45这题很快就做完了&#xff0c;然后忘记提交了。。就开始做递归。想起来的时候已经过了时间。 public class Example3_7 {public static void main(String args[]) {int sum0,i,j;for(i1;i&l…

java入门就是死敲代码吗_JAVA入门第二季综合练习(直接思考敲的代码,面向过程,不好)...

package com.imocc;/*author ndh2016年3月27日 21:03:02*/import java.util.Scanner;public class DiDi {public static void main(String[] args){Scanner sc new Scanner(System.in);System.out.println("欢迎使用迪迪租车系统&#xff01;");System.out.println(…

JavaScript实现表单的全选,反选,获取值

构思 通过for循环和for in循环来实现&#xff0c;界面效果如下 步骤 全选&#xff1a; 循环给所有的表单设置checked 反选&#xff1a; 循环内判断checked是否为true&#xff0c;如果为true则改为false否则改为true 获取值&#xff1a; 最开始用for取&#xff0c;但是只打印最后…

jQuery.extend() 使用语法详解

今天在写插件&#xff0c;使用$.extend({}, defaults, options)的时候发现漏写了 {}&#xff0c;浪费了一些时间&#xff0c; 所以详细记录下该方法的 API 和使用。API 如下&#xff1a;jQuery.extend( [ deep ], target, [ object1 ], [ objectN ] )描述&#xff1a;合并两个或…

EJB钝化和激活示例

在本教程中&#xff0c;我们将了解状态Java企业会话Bean中激活和钝化的工作方式。 1.简介 有状态会话Bean通常保存有关特定客户端的信息&#xff0c;并在整个会话中保存该信息。 但是&#xff0c;事实是&#xff0c;客户端会话往往会在相当长的时间内保持活动状态&#xff0c;…

Python进阶_面对对象面对过程

这节主要讲面对对象与面对过程两种编程思想的主要区别。 一. 简单对比 面向过程是一种基础的方法&#xff0c;它考虑的是实际的实现步骤&#xff0c;一般情况下&#xff0c;面向过程是自顶向下逐步求精&#xff0c;其最重要的是模块化的思想方法。 面向对象的方法主要是把事物给…

puppet 安装mysql_Puppet安装dashboard

Puppet安装dashboard安装依赖包[rootmaster ~]# sudo yum install -y mysql mysql-devel mysql-server ruby ruby-devel ruby-irb ruby-mysql ruby-rdoc ruby-ri启动mysql并设置开机启动[rootmaster ~]# service mysqld start [rootmaster ~]# chkconfig mysqld on下载并安装…

命令模式详解

原文链接:https://www.cnblogs.com/java-my-life/archive/2012/06/01/2526972.html 在阎宏博士的《JAVA与模式》一书中开头是这样描述命令&#xff08;Command&#xff09;模式的&#xff1a; 命令模式属于对象的行为模式。命令模式又称为行动(Action)模式或交易(Transaction)模…

JDK 8中几乎命名的方法参数

有时在Java中命名方法参数确实很不错&#xff0c;这看起来可能不会出现很长时间了&#xff0c;但是始终还有其他一些解决方法&#xff0c;例如使用构建器模式来获得类似的行为&#xff0c;这将为一点点。 在我看来&#xff0c;使用JDK 8中的Lambda支持可以使您获得非常接近的效…

深入解析jQuery中的延时对象的概念

首先我们需要明白延时对象有什么用&#xff1f;第一个作用&#xff0c;解决时序以及动态添加执行函数的问题。function a(){alert(1)};function b(){alert(2)};function c(){alert(3)};a();setTimeout(function(){b();},0);c();很明显函数执行顺序是a->c->b,而不是按照函…

c mysql5.7_CentOS7下MySQL5.7的三种安装方式详解

操作系统环境&#xff1a;CentOS 7.4最小化安装[rootnode3 src]# cat /etc/redhat-releaseCentOS Linux release 7.4.1708 (Core)[rootnode3 ~]# uname -r3.10.0-693.5.2.el7.x86_64[rootnode3 ~]#安装版本为&#xff1a;MySQL 5.7.20一、编译安装MySQL5.71、下载源码包[rootno…

Struts2 学习之小白开始

Struts2 基础知识学习总结 Struts2 概述&#xff1a;Struts2 是一个用来开发 MVC 应用程序的框架&#xff0c;他提供了 Web 应用程序开发过程中的一些常见问题的解决方案&#xff0c;比如对于用户输入信息合法性的验证&#xff0c;统一的布局&#xff0c;国际化等&#xff0c;既…

机器学习的数学基础 - 信息论

机器学习的数学基础 - 信息论 信息论 信息论本来是通信中的概念&#xff0c;但是其核心思想“熵”在机器学习中也得到了广泛的应用。比如决策树模型ID3&#xff0c;C4.5中是利用信息增益来划分特征而生成一颗决策树的&#xff0c;而信息增益就是基于这里所说的熵。所以它的重要…

了解ElasticSearch分析器

令人遗憾的是&#xff0c;许多早期的互联网啤酒配方不一定采用易于消化的格式。 也就是说&#xff0c;这些食谱是通常在电子邮件或论坛帖子中最初组成的非结构化的方向和成分混合列表。 因此&#xff0c;尽管很难轻松地将这些配方放入传统的数据存储中&#xff08;表面上看是为…

c++简单程序设计-2

1.验证性实验部分①函数声明和函数定义各自的作用及二者的区别&#xff1a;函数声明就是调用函数之前提示一下有这个函数函数定义就是写一个函数②什么是形参&#xff1f;什么是实参&#xff1f;函数参数和返回值在函数中起到什么作用&#xff1f;函数定义时写的参数叫做形参&a…