java连接Neo4j服务器
一: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
四:程序代码
- package com.wzs.linux;
- public class Relationship
- {
- public static final String OUT = "out";
- public static final String IN = "in";
- public static final String BOTH = "both";
- private String type;
- private String direction;
- public String toJsonCollection()
- {
- StringBuilder sb = new StringBuilder();
- sb.append("{ ");
- sb.append(" \"type\" : \"" + type + "\"");
- if (direction != null)
- {
- sb.append(", \"direction\" : \"" + direction + "\"");
- }
- sb.append(" }");
- return sb.toString();
- }
- public Relationship(String type, String direction)
- {
- setType(type);
- setDirection(direction);
- }
- public Relationship(String type)
- {
- this(type, null);
- }
- public void setType(String type)
- {
- this.type = type;
- }
- public void setDirection(String direction)
- {
- this.direction = direction;
- }
- }
- import java.net.URI;
- import java.net.URISyntaxException;
- import javax.ws.rs.core.MediaType;
- import com.sun.jersey.api.client.Client;
- import com.sun.jersey.api.client.ClientResponse;
- import com.sun.jersey.api.client.WebResource;
- public class CreateSimpleGraph
- {
- private static final String SERVER_ROOT_URI = "http://localhost:7474/db/data/";
- public static void main( String[] args ) throws URISyntaxException
- {
- checkDatabaseIsRunning();
- // START SNIPPET: nodesAndProps
- URI firstNode = createNode();
- addProperty( firstNode, "name", "Joe Strummer" );
- URI secondNode = createNode();
- addProperty( secondNode, "band", "The Clash" );
- // END SNIPPET: nodesAndProps
- // START SNIPPET: addRel
- URI relationshipUri = addRelationship( firstNode, secondNode, "singer",
- "{ \"from\" : \"1976\", \"until\" : \"1986\" }" );
- // END SNIPPET: addRel
- // START SNIPPET: addMetaToRel
- addMetadataToProperty( relationshipUri, "stars", "5" );
- // END SNIPPET: addMetaToRel
- // START SNIPPET: queryForSingers
- findSingersInBands( firstNode );
- // END SNIPPET: queryForSingers
- }
- private static void findSingersInBands( URI startNode )
- throws URISyntaxException
- {
- // START SNIPPET: traversalDesc
- // TraversalDescription turns into JSON to send to the Server
- TraversalDescription t = new TraversalDescription();
- t.setOrder( TraversalDescription.DEPTH_FIRST );
- t.setUniqueness( TraversalDescription.NODE );
- t.setMaxDepth( 10 );
- t.setReturnFilter( TraversalDescription.ALL );
- t.setRelationships( new Relationship( "singer", Relationship.OUT ) );
- // END SNIPPET: traversalDesc
- // START SNIPPET: traverse
- URI traverserUri = new URI( startNode.toString() + "/traverse/node" );
- WebResource resource = Client.create()
- .resource( traverserUri );
- String jsonTraverserPayload = t.toJson();
- ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
- .type( MediaType.APPLICATION_JSON )
- .entity( jsonTraverserPayload )
- .post( ClientResponse.class );
- System.out.println( String.format(
- "POST [%s] to [%s], status code [%d], returned data: "
- + System.getProperty( "line.separator" ) + "%s",
- jsonTraverserPayload, traverserUri, response.getStatus(),
- response.getEntity( String.class ) ) );
- response.close();
- // END SNIPPET: traverse
- }
- // START SNIPPET: insideAddMetaToProp
- private static void addMetadataToProperty( URI relationshipUri,
- String name, String value ) throws URISyntaxException
- {
- URI propertyUri = new URI( relationshipUri.toString() + "/properties" );
- String entity = toJsonNameValuePairCollection( name, value );
- WebResource resource = Client.create()
- .resource( propertyUri );
- ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
- .type( MediaType.APPLICATION_JSON )
- .entity( entity )
- .put( ClientResponse.class );
- System.out.println( String.format(
- "PUT [%s] to [%s], status code [%d]", entity, propertyUri,
- response.getStatus() ) );
- response.close();
- }
- // END SNIPPET: insideAddMetaToProp
- private static String toJsonNameValuePairCollection( String name,
- String value )
- {
- return String.format( "{ \"%s\" : \"%s\" }", name, value );
- }
- private static URI createNode()
- {
- // START SNIPPET: createNode
- final String nodeEntryPointUri = SERVER_ROOT_URI + "node";
- // http://localhost:7474/db/data/node
- WebResource resource = Client.create()
- .resource( nodeEntryPointUri );
- // POST {} to the node entry point URI
- ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
- .type( MediaType.APPLICATION_JSON )
- .entity( "{}" )
- .post( ClientResponse.class );
- final URI location = response.getLocation();
- System.out.println( String.format(
- "POST to [%s], status code [%d], location header [%s]",
- nodeEntryPointUri, response.getStatus(), location.toString() ) );
- response.close();
- return location;
- // END SNIPPET: createNode
- }
- // START SNIPPET: insideAddRel
- private static URI addRelationship( URI startNode, URI endNode,
- String relationshipType, String jsonAttributes )
- throws URISyntaxException
- {
- URI fromUri = new URI( startNode.toString() + "/relationships" );
- String relationshipJson = generateJsonRelationship( endNode,
- relationshipType, jsonAttributes );
- WebResource resource = Client.create()
- .resource( fromUri );
- // POST JSON to the relationships URI
- ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
- .type( MediaType.APPLICATION_JSON )
- .entity( relationshipJson )
- .post( ClientResponse.class );
- final URI location = response.getLocation();
- System.out.println( String.format(
- "POST to [%s], status code [%d], location header [%s]",
- fromUri, response.getStatus(), location.toString() ) );
- response.close();
- return location;
- }
- // END SNIPPET: insideAddRel
- private static String generateJsonRelationship( URI endNode,
- String relationshipType, String... jsonAttributes )
- {
- StringBuilder sb = new StringBuilder();
- sb.append( "{ \"to\" : \"" );
- sb.append( endNode.toString() );
- sb.append( "\", " );
- sb.append( "\"type\" : \"" );
- sb.append( relationshipType );
- if ( jsonAttributes == null || jsonAttributes.length < 1 )
- {
- sb.append( "\"" );
- }
- else
- {
- sb.append( "\", \"data\" : " );
- for ( int i = 0; i < jsonAttributes.length; i++ )
- {
- sb.append( jsonAttributes[i] );
- if ( i < jsonAttributes.length - 1 )
- { // Miss off the final comma
- sb.append( ", " );
- }
- }
- }
- sb.append( " }" );
- return sb.toString();
- }
- private static void addProperty( URI nodeUri, String propertyName,
- String propertyValue )
- {
- // START SNIPPET: addProp
- String propertyUri = nodeUri.toString() + "/properties/" + propertyName;
- // http://localhost:7474/db/data/node/{node_id}/properties/{property_name}
- WebResource resource = Client.create()
- .resource( propertyUri );
- ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
- .type( MediaType.APPLICATION_JSON )
- .entity( "\"" + propertyValue + "\"" )
- .put( ClientResponse.class );
- System.out.println( String.format( "PUT to [%s], status code [%d]",
- propertyUri, response.getStatus() ) );
- response.close();
- // END SNIPPET: addProp
- }
- private static void checkDatabaseIsRunning()
- {
- // START SNIPPET: checkServer
- WebResource resource = Client.create()
- .resource( SERVER_ROOT_URI );
- ClientResponse response = resource.get( ClientResponse.class );
- System.out.println( String.format( "GET on [%s], status code [%d]",
- SERVER_ROOT_URI, response.getStatus() ) );
- response.close();
- // END SNIPPET: checkServer
- }
- }
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class TraversalDescription
- {
- public static final String DEPTH_FIRST = "depth first";
- public static final String NODE = "node";
- public static final String ALL = "all";
- private String uniqueness = NODE;
- private int maxDepth = 1;
- private String returnFilter = ALL;
- private String order = DEPTH_FIRST;
- private List<Relationship> relationships = new ArrayList<Relationship>();
- public void setOrder( String order )
- {
- this.order = order;
- }
- public void setUniqueness( String uniqueness )
- {
- this.uniqueness = uniqueness;
- }
- public void setMaxDepth( int maxDepth )
- {
- this.maxDepth = maxDepth;
- }
- public void setReturnFilter( String returnFilter )
- {
- this.returnFilter = returnFilter;
- }
- public void setRelationships( Relationship... relationships )
- {
- this.relationships = Arrays.asList( relationships );
- }
- public String toJson()
- {
- StringBuilder sb = new StringBuilder();
- sb.append( "{ " );
- sb.append( " \"order\" : \"" + order + "\"" );
- sb.append( ", " );
- sb.append( " \"uniqueness\" : \"" + uniqueness + "\"" );
- sb.append( ", " );
- if ( relationships.size() > 0 )
- {
- sb.append( "\"relationships\" : [" );
- for ( int i = 0; i < relationships.size(); i++ )
- {
- sb.append( relationships.get( i )
- .toJsonCollection() );
- if ( i < relationships.size() - 1 )
- { // Miss off the final comma
- sb.append( ", " );
- }
- }
- sb.append( "], " );
- }
- sb.append( "\"return filter\" : { " );
- sb.append( "\"language\" : \"builtin\", " );
- sb.append( "\"name\" : \"" );
- sb.append( returnFilter );
- sb.append( "\" }, " );
- sb.append( "\"max depth\" : " );
- sb.append( maxDepth );
- sb.append( " }" );
- return sb.toString();
- }
- }
java连接Neo4j服务器相关推荐
- java连接linux服务器执行shell命令(框架分析+推荐)
java连接linux服务器执行shell命令(框架分析+推荐) 一.分类+连接方式 程序打成jar包,在本地服务器上执行shell命令.这种使用MyRuntimeUtil工具类 java程序远程li ...
- java连接MQTT服务器(Springboot整合MQTT)
一.业务场景 硬件采集的数据传入EMQX平台(采用MQTT协议),java通过代码连接MQTT服务器,进行采集数据接收.解析.业务处理.存储入库.数据展示. MQTT 是基于 发布(Publish)/ ...
- 使用java连接neo4j aura数据库
使用java连接neo4j aura数据库的方法 最近想学习一下neo4j,正好在官网上看到了neo4j aura,就打算尝试一下. 环境配置 这里就不多说了,主要是java环境和neo4j环境的配置 ...
- 使用java连接ftp服务器_Java如何连接到FTP服务器?
文件传输协议(FTP)是一种标准的网络协议,用于在计算机网络上的客户端和服务器之间传输计算机文件.下面的示例向您显示如何连接到FTP服务器. 在此示例中,我们使用FTPClientApache Com ...
- JAVA远程连接ssh异步,SSH-2实现java连接远程服务器并执行脚本命令
参考文档: maven jar包:https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2 Ganymed SSH2 API文档 ...
- Java连接FTP服务器并且实现对其文件的上传和下载
概述 FTP是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文传协议".FTP作为网络共享文件的传输协议,在网络应用软件中具有广泛的应用.F ...
- java 连接远程服务器_java实现连接远程服务器并执行命令的基本原理
一.所需jar包 需要借助Ganymed SSH的jar包: ganymed-ssh2-build210.jar 二.实现原理 Ganymed SSH-2 java在整个访问过程中担当SSH的客户端 ...
- java连接neo4j(使用spring data neo4j)
1. Spring Data Neo4j概述 认识Spring-Data-Neo4j之前,需要先对OGM有一个了解 OGM即对象图映射(Object Graph Mapper ,简称ORM ),基于O ...
- Java连接Linux服务器上传文件
背景: 项目中有需求要使用Java上传文件至服务器及执行某些shell脚本.通过查阅一些资料,反复测试了两套方案,各有优缺点,下面分别阐述一下. 实现方案一:SpringBoot + JSch + L ...
- java 连接Linux服务器并执行指令
直接上代码. /*** Created by hpp on 2017/6/5.*/import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session ...
最新文章
- python做一个系统-用python做一个系统监控程序
- input val >=zero input_val <=one
- Bootstrap Paginator分页插件+ajax
- linux ftp上传下载文件,Linux下ftp命令上传下载文件
- 计算器软件设计和计算机软件设计区别,求一个模拟计算器程序
- Datawhale-零基础入门NLP-新闻文本分类Task02
- php扩展返回字符数组,PHP扩展之数组字符串处理
- 云上赶年集、品年味,阿里云让云上中国年“春节不打烊”
- 278. First Bad Version
- 附录-SpringFactoriesLoader
- 怎么把音频转换文字?三个步骤解决它
- h3c无线控制器常用命令(wx)
- 易福门流量计SA5000
- JUCE框架教程(2)—— 创建一个基本的音频/MIDI 插件第一部分:设置
- [luogu P5960] 【模板】差分约束算法
- BTC钱包(wallet.dat 文件密码与私钥的区别)
- 华硕P8H61-M+i3-3220 +GTX650
- 阿里内网疯狂传阅的“M8级”分布式架构笔记,GitHub刚上线就霸榜
- 使用opencv调用摄像头然后录制视频和保存文件
- ACM投稿版权信息去除问题