一:Neo4j服务器安装(参考:http://docs.neo4j.org.cn/server-installation.html)

1.下载Neo4j数据,我下载的版本是: neo4j-enterprise-1.8.1-windows

2.解压 neo4j-enterprise-1.8.1-windows

3.到Neo4j的bin目录下neo4j-enterprise-1.8.1-windows\neo4j-enterprise-1.8.1\bin

4.运行 neo4j start 命令

5.打开 http://localhost:7474  看到图形化界面则安装成功!

二:测试代码(参考:http://www.neo4j.org.cn/2012/07/30/server-java-rest-client-example/)

测试代码总共有三个类:

CreateSimpleGraph.java  下载地址:https://github.com/neo4j/community/blob/1.8.M06/server-examples/src/main/java/org/neo4j/examples/server/CreateSimpleGraph.java
Relationship.java  下载地址:https://github.com/neo4j/community/blob/1.8.M06/server-examples/src/main/java/org/neo4j/examples/server/Relationship.java
TraversalDescription.java  下载地址:https://github.com/neo4j/community/blob/1.8.M06/server-examples/src/main/java/org/neo4j/examples/server/TraversalDescription.java

三:程序正常运行用到的jar包

1.neo4j-enterprise-1.8.1-windows\neo4j-enterprise-1.8.1\lib下所有jar包

2.自己下载的jar包

com.sun.jersey.jersey-core-1.4.0.jar

javax.ws.rs.jar

jersey-client-1.9.jar

四:程序代码

[java] view plain copy  print?
  1. package com.wzs.linux;
  2. public class Relationship
  3. {
  4. public static final String OUT = "out";
  5. public static final String IN = "in";
  6. public static final String BOTH = "both";
  7. private String type;
  8. private String direction;
  9. public String toJsonCollection()
  10. {
  11. StringBuilder sb = new StringBuilder();
  12. sb.append("{ ");
  13. sb.append(" \"type\" : \"" + type + "\"");
  14. if (direction != null)
  15. {
  16. sb.append(", \"direction\" : \"" + direction + "\"");
  17. }
  18. sb.append(" }");
  19. return sb.toString();
  20. }
  21. public Relationship(String type, String direction)
  22. {
  23. setType(type);
  24. setDirection(direction);
  25. }
  26. public Relationship(String type)
  27. {
  28. this(type, null);
  29. }
  30. public void setType(String type)
  31. {
  32. this.type = type;
  33. }
  34. public void setDirection(String direction)
  35. {
  36. this.direction = direction;
  37. }
  38. }
[java] view plain copy  print?
  1. import java.net.URI;
  2. import java.net.URISyntaxException;
  3. import javax.ws.rs.core.MediaType;
  4. import com.sun.jersey.api.client.Client;
  5. import com.sun.jersey.api.client.ClientResponse;
  6. import com.sun.jersey.api.client.WebResource;
  7. public class CreateSimpleGraph
  8. {
  9. private static final String SERVER_ROOT_URI = "http://localhost:7474/db/data/";
  10. public static void main( String[] args ) throws URISyntaxException
  11. {
  12. checkDatabaseIsRunning();
  13. // START SNIPPET: nodesAndProps
  14. URI firstNode = createNode();
  15. addProperty( firstNode, "name", "Joe Strummer" );
  16. URI secondNode = createNode();
  17. addProperty( secondNode, "band", "The Clash" );
  18. // END SNIPPET: nodesAndProps
  19. // START SNIPPET: addRel
  20. URI relationshipUri = addRelationship( firstNode, secondNode, "singer",
  21. "{ \"from\" : \"1976\", \"until\" : \"1986\" }" );
  22. // END SNIPPET: addRel
  23. // START SNIPPET: addMetaToRel
  24. addMetadataToProperty( relationshipUri, "stars", "5" );
  25. // END SNIPPET: addMetaToRel
  26. // START SNIPPET: queryForSingers
  27. findSingersInBands( firstNode );
  28. // END SNIPPET: queryForSingers
  29. }
  30. private static void findSingersInBands( URI startNode )
  31. throws URISyntaxException
  32. {
  33. // START SNIPPET: traversalDesc
  34. // TraversalDescription turns into JSON to send to the Server
  35. TraversalDescription t = new TraversalDescription();
  36. t.setOrder( TraversalDescription.DEPTH_FIRST );
  37. t.setUniqueness( TraversalDescription.NODE );
  38. t.setMaxDepth( 10 );
  39. t.setReturnFilter( TraversalDescription.ALL );
  40. t.setRelationships( new Relationship( "singer", Relationship.OUT ) );
  41. // END SNIPPET: traversalDesc
  42. // START SNIPPET: traverse
  43. URI traverserUri = new URI( startNode.toString() + "/traverse/node" );
  44. WebResource resource = Client.create()
  45. .resource( traverserUri );
  46. String jsonTraverserPayload = t.toJson();
  47. ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
  48. .type( MediaType.APPLICATION_JSON )
  49. .entity( jsonTraverserPayload )
  50. .post( ClientResponse.class );
  51. System.out.println( String.format(
  52. "POST [%s] to [%s], status code [%d], returned data: "
  53. + System.getProperty( "line.separator" ) + "%s",
  54. jsonTraverserPayload, traverserUri, response.getStatus(),
  55. response.getEntity( String.class ) ) );
  56. response.close();
  57. // END SNIPPET: traverse
  58. }
  59. // START SNIPPET: insideAddMetaToProp
  60. private static void addMetadataToProperty( URI relationshipUri,
  61. String name, String value ) throws URISyntaxException
  62. {
  63. URI propertyUri = new URI( relationshipUri.toString() + "/properties" );
  64. String entity = toJsonNameValuePairCollection( name, value );
  65. WebResource resource = Client.create()
  66. .resource( propertyUri );
  67. ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
  68. .type( MediaType.APPLICATION_JSON )
  69. .entity( entity )
  70. .put( ClientResponse.class );
  71. System.out.println( String.format(
  72. "PUT [%s] to [%s], status code [%d]", entity, propertyUri,
  73. response.getStatus() ) );
  74. response.close();
  75. }
  76. // END SNIPPET: insideAddMetaToProp
  77. private static String toJsonNameValuePairCollection( String name,
  78. String value )
  79. {
  80. return String.format( "{ \"%s\" : \"%s\" }", name, value );
  81. }
  82. private static URI createNode()
  83. {
  84. // START SNIPPET: createNode
  85. final String nodeEntryPointUri = SERVER_ROOT_URI + "node";
  86. // http://localhost:7474/db/data/node
  87. WebResource resource = Client.create()
  88. .resource( nodeEntryPointUri );
  89. // POST {} to the node entry point URI
  90. ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
  91. .type( MediaType.APPLICATION_JSON )
  92. .entity( "{}" )
  93. .post( ClientResponse.class );
  94. final URI location = response.getLocation();
  95. System.out.println( String.format(
  96. "POST to [%s], status code [%d], location header [%s]",
  97. nodeEntryPointUri, response.getStatus(), location.toString() ) );
  98. response.close();
  99. return location;
  100. // END SNIPPET: createNode
  101. }
  102. // START SNIPPET: insideAddRel
  103. private static URI addRelationship( URI startNode, URI endNode,
  104. String relationshipType, String jsonAttributes )
  105. throws URISyntaxException
  106. {
  107. URI fromUri = new URI( startNode.toString() + "/relationships" );
  108. String relationshipJson = generateJsonRelationship( endNode,
  109. relationshipType, jsonAttributes );
  110. WebResource resource = Client.create()
  111. .resource( fromUri );
  112. // POST JSON to the relationships URI
  113. ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
  114. .type( MediaType.APPLICATION_JSON )
  115. .entity( relationshipJson )
  116. .post( ClientResponse.class );
  117. final URI location = response.getLocation();
  118. System.out.println( String.format(
  119. "POST to [%s], status code [%d], location header [%s]",
  120. fromUri, response.getStatus(), location.toString() ) );
  121. response.close();
  122. return location;
  123. }
  124. // END SNIPPET: insideAddRel
  125. private static String generateJsonRelationship( URI endNode,
  126. String relationshipType, String... jsonAttributes )
  127. {
  128. StringBuilder sb = new StringBuilder();
  129. sb.append( "{ \"to\" : \"" );
  130. sb.append( endNode.toString() );
  131. sb.append( "\", " );
  132. sb.append( "\"type\" : \"" );
  133. sb.append( relationshipType );
  134. if ( jsonAttributes == null || jsonAttributes.length < 1 )
  135. {
  136. sb.append( "\"" );
  137. }
  138. else
  139. {
  140. sb.append( "\", \"data\" : " );
  141. for ( int i = 0; i < jsonAttributes.length; i++ )
  142. {
  143. sb.append( jsonAttributes[i] );
  144. if ( i < jsonAttributes.length - 1 )
  145. { // Miss off the final comma
  146. sb.append( ", " );
  147. }
  148. }
  149. }
  150. sb.append( " }" );
  151. return sb.toString();
  152. }
  153. private static void addProperty( URI nodeUri, String propertyName,
  154. String propertyValue )
  155. {
  156. // START SNIPPET: addProp
  157. String propertyUri = nodeUri.toString() + "/properties/" + propertyName;
  158. // http://localhost:7474/db/data/node/{node_id}/properties/{property_name}
  159. WebResource resource = Client.create()
  160. .resource( propertyUri );
  161. ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
  162. .type( MediaType.APPLICATION_JSON )
  163. .entity( "\"" + propertyValue + "\"" )
  164. .put( ClientResponse.class );
  165. System.out.println( String.format( "PUT to [%s], status code [%d]",
  166. propertyUri, response.getStatus() ) );
  167. response.close();
  168. // END SNIPPET: addProp
  169. }
  170. private static void checkDatabaseIsRunning()
  171. {
  172. // START SNIPPET: checkServer
  173. WebResource resource = Client.create()
  174. .resource( SERVER_ROOT_URI );
  175. ClientResponse response = resource.get( ClientResponse.class );
  176. System.out.println( String.format( "GET on [%s], status code [%d]",
  177. SERVER_ROOT_URI, response.getStatus() ) );
  178. response.close();
  179. // END SNIPPET: checkServer
  180. }
  181. }
[java] view plain copy  print?
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. public class TraversalDescription
  5. {
  6. public static final String DEPTH_FIRST = "depth first";
  7. public static final String NODE = "node";
  8. public static final String ALL = "all";
  9. private String uniqueness = NODE;
  10. private int maxDepth = 1;
  11. private String returnFilter = ALL;
  12. private String order = DEPTH_FIRST;
  13. private List<Relationship> relationships = new ArrayList<Relationship>();
  14. public void setOrder( String order )
  15. {
  16. this.order = order;
  17. }
  18. public void setUniqueness( String uniqueness )
  19. {
  20. this.uniqueness = uniqueness;
  21. }
  22. public void setMaxDepth( int maxDepth )
  23. {
  24. this.maxDepth = maxDepth;
  25. }
  26. public void setReturnFilter( String returnFilter )
  27. {
  28. this.returnFilter = returnFilter;
  29. }
  30. public void setRelationships( Relationship... relationships )
  31. {
  32. this.relationships = Arrays.asList( relationships );
  33. }
  34. public String toJson()
  35. {
  36. StringBuilder sb = new StringBuilder();
  37. sb.append( "{ " );
  38. sb.append( " \"order\" : \"" + order + "\"" );
  39. sb.append( ", " );
  40. sb.append( " \"uniqueness\" : \"" + uniqueness + "\"" );
  41. sb.append( ", " );
  42. if ( relationships.size() > 0 )
  43. {
  44. sb.append( "\"relationships\" : [" );
  45. for ( int i = 0; i < relationships.size(); i++ )
  46. {
  47. sb.append( relationships.get( i )
  48. .toJsonCollection() );
  49. if ( i < relationships.size() - 1 )
  50. { // Miss off the final comma
  51. sb.append( ", " );
  52. }
  53. }
  54. sb.append( "], " );
  55. }
  56. sb.append( "\"return filter\" : { " );
  57. sb.append( "\"language\" : \"builtin\", " );
  58. sb.append( "\"name\" : \"" );
  59. sb.append( returnFilter );
  60. sb.append( "\" }, " );
  61. sb.append( "\"max depth\" : " );
  62. sb.append( maxDepth );
  63. sb.append( " }" );
  64. return sb.toString();
  65. }
  66. }

java连接Neo4j服务器相关推荐

  1. java连接linux服务器执行shell命令(框架分析+推荐)

    java连接linux服务器执行shell命令(框架分析+推荐) 一.分类+连接方式 程序打成jar包,在本地服务器上执行shell命令.这种使用MyRuntimeUtil工具类 java程序远程li ...

  2. java连接MQTT服务器(Springboot整合MQTT)

    一.业务场景 硬件采集的数据传入EMQX平台(采用MQTT协议),java通过代码连接MQTT服务器,进行采集数据接收.解析.业务处理.存储入库.数据展示. MQTT 是基于 发布(Publish)/ ...

  3. 使用java连接neo4j aura数据库

    使用java连接neo4j aura数据库的方法 最近想学习一下neo4j,正好在官网上看到了neo4j aura,就打算尝试一下. 环境配置 这里就不多说了,主要是java环境和neo4j环境的配置 ...

  4. 使用java连接ftp服务器_Java如何连接到FTP服务器?

    文件传输协议(FTP)是一种标准的网络协议,用于在计算机网络上的客户端和服务器之间传输计算机文件.下面的示例向您显示如何连接到FTP服务器. 在此示例中,我们使用FTPClientApache Com ...

  5. JAVA远程连接ssh异步,SSH-2实现java连接远程服务器并执行脚本命令

    参考文档: maven jar包:https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2 Ganymed SSH2 API文档 ...

  6. Java连接FTP服务器并且实现对其文件的上传和下载

    概述 FTP是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文传协议".FTP作为网络共享文件的传输协议,在网络应用软件中具有广泛的应用.F ...

  7. java 连接远程服务器_java实现连接远程服务器并执行命令的基本原理

    一.所需jar包 需要借助Ganymed SSH的jar包:  ganymed-ssh2-build210.jar 二.实现原理 Ganymed SSH-2 java在整个访问过程中担当SSH的客户端 ...

  8. java连接neo4j(使用spring data neo4j)

    1. Spring Data Neo4j概述 认识Spring-Data-Neo4j之前,需要先对OGM有一个了解 OGM即对象图映射(Object Graph Mapper ,简称ORM ),基于O ...

  9. Java连接Linux服务器上传文件

    背景: 项目中有需求要使用Java上传文件至服务器及执行某些shell脚本.通过查阅一些资料,反复测试了两套方案,各有优缺点,下面分别阐述一下. 实现方案一:SpringBoot + JSch + L ...

  10. java 连接Linux服务器并执行指令

    直接上代码. /*** Created by hpp on 2017/6/5.*/import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session ...

最新文章

  1. python做一个系统-用python做一个系统监控程序
  2. input val >=zero input_val <=one
  3. Bootstrap Paginator分页插件+ajax
  4. linux ftp上传下载文件,Linux下ftp命令上传下载文件
  5. 计算器软件设计和计算机软件设计区别,求一个模拟计算器程序
  6. Datawhale-零基础入门NLP-新闻文本分类Task02
  7. php扩展返回字符数组,PHP扩展之数组字符串处理
  8. 云上赶年集、品年味,阿里云让云上中国年“春节不打烊”
  9. 278. First Bad Version
  10. 附录-SpringFactoriesLoader
  11. 怎么把音频转换文字?三个步骤解决它
  12. h3c无线控制器常用命令(wx)
  13. 易福门流量计SA5000
  14. JUCE框架教程(2)—— 创建一个基本的音频/MIDI 插件第一部分:设置
  15. [luogu P5960] 【模板】差分约束算法
  16. BTC钱包(wallet.dat 文件密码与私钥的区别)
  17. 华硕P8H61-M+i3-3220 +GTX650
  18. 阿里内网疯狂传阅的“M8级”分布式架构笔记,GitHub刚上线就霸榜
  19. 使用opencv调用摄像头然后录制视频和保存文件
  20. ACM投稿版权信息去除问题

热门文章

  1. 编译器预编译与变量提升
  2. 简单理解JavaScript中的闭包
  3. 浅谈SEO翻倍提升网站流量
  4. MySQL初步研究数据库
  5. jqGrid宽度自适应
  6. Linux常用命令之:软件安装命令
  7. 苹果Mac 下 Parallels Desktop “无法连接到 Parallels 服务”的解决方法
  8. QuickTime Player 如何开启倍速播放?
  9. 如何在 Mac 上查找路由器 IP 地址?
  10. 始终将文件夹放在 Mac 上 Finder 顶部的方法