`
m635674608
  • 浏览: 5062929 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

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

 

四:程序代码

  1. package com.wzs.linux;  
  2.   
  3. public class Relationship  
  4. {  
  5.     public static final String OUT = "out";  
  6.     public static final String IN = "in";  
  7.     public static final String BOTH = "both";  
  8.     private String type;  
  9.     private String direction;  
  10.   
  11.     public String toJsonCollection()  
  12.     {  
  13.         StringBuilder sb = new StringBuilder();  
  14.         sb.append("{ ");  
  15.         sb.append(" \"type\" : \"" + type + "\"");  
  16.         if (direction != null)  
  17.         {  
  18.             sb.append(", \"direction\" : \"" + direction + "\"");  
  19.         }  
  20.         sb.append(" }");  
  21.         return sb.toString();  
  22.     }  
  23.   
  24.     public Relationship(String type, String direction)  
  25.     {  
  26.         setType(type);  
  27.         setDirection(direction);  
  28.     }  
  29.   
  30.     public Relationship(String type)  
  31.     {  
  32.         this(type, null);  
  33.     }  
  34.   
  35.     public void setType(String type)  
  36.     {  
  37.         this.type = type;  
  38.     }  
  39.   
  40.     public void setDirection(String direction)  
  41.     {  
  42.         this.direction = direction;  
  43.     }  
  44. }  

 

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

 

  1. import java.util.ArrayList;  
  2. import java.util.Arrays;  
  3. import java.util.List;  
  4.   
  5. public class TraversalDescription  
  6. {  
  7.     public static final String DEPTH_FIRST = "depth first";  
  8.     public static final String NODE = "node";  
  9.     public static final String ALL = "all";  
  10.   
  11.     private String uniqueness = NODE;  
  12.     private int maxDepth = 1;  
  13.     private String returnFilter = ALL;  
  14.     private String order = DEPTH_FIRST;  
  15.     private List<Relationship> relationships = new ArrayList<Relationship>();  
  16.   
  17.     public void setOrder( String order )  
  18.     {  
  19.         this.order = order;  
  20.     }  
  21.   
  22.     public void setUniqueness( String uniqueness )  
  23.     {  
  24.         this.uniqueness = uniqueness;  
  25.     }  
  26.   
  27.     public void setMaxDepth( int maxDepth )  
  28.     {  
  29.         this.maxDepth = maxDepth;  
  30.     }  
  31.   
  32.     public void setReturnFilter( String returnFilter )  
  33.     {  
  34.         this.returnFilter = returnFilter;  
  35.     }  
  36.   
  37.     public void setRelationships( Relationship... relationships )  
  38.     {  
  39.         this.relationships = Arrays.asList( relationships );  
  40.     }  
  41.   
  42.     public String toJson()  
  43.     {  
  44.         StringBuilder sb = new StringBuilder();  
  45.         sb.append( "{ " );  
  46.         sb.append( " \"order\" : \"" + order + "\"" );  
  47.         sb.append( ", " );  
  48.         sb.append( " \"uniqueness\" : \"" + uniqueness + "\"" );  
  49.         sb.append( ", " );  
  50.         if ( relationships.size() > 0 )  
  51.         {  
  52.             sb.append( "\"relationships\" : [" );  
  53.             for ( int i = 0; i < relationships.size(); i++ )  
  54.             {  
  55.                 sb.append( relationships.get( i )  
  56.                         .toJsonCollection() );  
  57.                 if ( i < relationships.size() - 1 )  
  58.                 { // Miss off the final comma  
  59.                     sb.append( ", " );  
  60.                 }  
  61.             }  
  62.             sb.append( "], " );  
  63.         }  
  64.         sb.append( "\"return filter\" : { " );  
  65.         sb.append( "\"language\" : \"builtin\", " );  
  66.         sb.append( "\"name\" : \"" );  
  67.         sb.append( returnFilter );  
  68.         sb.append( "\" }, " );  
  69.         sb.append( "\"max depth\" : " );  
  70.         sb.append( maxDepth );  
  71.         sb.append( " }" );  
  72.         return sb.toString();  
  73.     }  

http://blog.csdn.net/adam_wzs/article/details/8622250

分享到:
评论

相关推荐

    采用java操作neo4j数据库源码

    // Neo4j服务器地址 private static final String USER = "neo4j"; // 用户名 private static final String PASSWORD = "password"; // 密码 private static Driver driver; public static synchronized ...

    java项目集成neo4j的demo

    3. **驱动程序**:`neo4j-java-driver`是官方提供的Java客户端,用于与Neo4j服务器通信。它提供了创建、读取、更新和删除(CRUD)操作的API,使得在Java代码中操作图数据库变得简单。 4. **配置连接**:在Java项目...

    java操作neo4j.zip

    在Java应用中,我们通常使用`neo4j-java-driver`库来与Neo4j服务器进行交互,实现创建、读取、更新和删除(CRUD)等基本操作。以下将详细讲解如何使用Java操作Neo4j。 首先,我们需要在项目中引入`neo4j-java-...

    Eclipse搭建JAVA,PYTHON,Neo4j开发平台

    - 嵌入式方式连接Neo4j,意味着Java应用程序直接运行在Neo4j数据库进程中,无需额外的服务器。 - 编写Java代码以创建和查询图形数据库,利用Neo4j提供的API操作节点、关系和属性。 6. **可视化Neo4j数据**: - ...

    Spring连接图存数据库Neo4j实现增删改查

    1.基于spring-data-neo4j 3.2.0通过REST远程连接Neo4j服务器,并非嵌入式连接; 2.创建接口用于创建一个简单的图存数据库,实现一个中心点到其他十个点的连接; 3.提供删除接口,可删除所有点和边; 4.提供查询...

    neo4j-java-reference-3.1(neo4j官方Java开发指南)

    Neo4j为Java开发者提供了Java驱动程序,包括`Neo4jDriver`,它允许通过bolt协议连接到数据库。此外,还有`Session`和`Transaction`概念,用于执行数据库操作。 3. **安装与配置**: 安装Neo4j涉及到下载服务器...

    Neo4j_1.8中文手册 内附Java代码

    - **嵌入式模式**:Neo4j 可以直接嵌入到 Java 应用程序中运行,无需单独的服务器。 - **远程客户端访问**:除了嵌入式模式外,Neo4j 还支持远程客户端访问,这允许开发者从不同的应用程序中访问同一个 Neo4j 数据库...

    Neo4j Java开发快速指南

    如果你需要创建自己的Java函数或过程,可以编写Java类并打包为JAR,然后在Neo4j服务器上加载。 5. **APOC库**: APOC是Neo4j的一个强大扩展,提供了大量的实用函数和过程。例如,你可以使用它来导入数据、执行复杂的...

    Neo4j-Java开发快速指南

    通过以下代码建立与Neo4j服务器的连接: ```java import org.neo4j.driver.Driver; import org.neo4j.driver.GraphDatabase; public class Neo4jConnector { private static final String URI = "bolt://localhost...

    Neo4j jar包集合 java项目开发

    3. **驱动程序**:Neo4j提供了多种Java驱动,如`neo4j-java-driver.jar`,它是一个异步非阻塞的驱动,使得Java应用能够安全高效地连接到Neo4j服务器。通过这个驱动,开发者可以创建会话,执行Cypher查询,并获取结果...

    neo4j-3.3.3 及对应的java-sdk

    解压后,你可以通过运行`bin/neo4j console`启动Neo4j服务器。注意,为了安全和性能,建议在生产环境中配置适当的环境变量和修改默认配置文件`conf/neo4j.conf`。 对于Windows用户,提供了`neo4j-community-3.4.9-...

    neo4j-driver-manual-4.2-java.pdf

    该手册详细介绍了如何使用Neo4j的官方Java驱动程序,包括驱动程序的安装、使用示例、配置、连接、会话管理、事务处理、查询执行以及异常处理等关键知识点。下面将详细阐释这些知识点。 ### 获取入门 手册首先介绍...

    Neo4j中文操作指南及JavaDemo PDF高清

    如果你的应用需要处理大量的数据,你可以扩展Neo4j服务器,增加服务器的数量。为了保证数据的安全,Neo4j也提供了数据备份和安全功能。 Neo4j提供了一个基于Web的图数据库管理工具,你可以通过它来管理Neo4j数据库...

    neo4j使用文档(英文版)

    - 介绍了如何部署和配置Neo4j服务器,以及如何使用REST API与服务器交互。 - 讨论了如何通过扩展Neo4j服务器来增加自定义功能。 以上内容概述了《neo4j使用文档》中涵盖的主要知识点,从Neo4j的基本概念到具体的...

    neo4j-jdbc相关jar

    3. **建立连接**:通过`DriverManager.getConnection()`方法创建一个到Neo4j服务器的连接,提供URL、用户名和密码作为参数。 4. **执行查询**:创建`Statement`或`PreparedStatement`对象,然后调用其`executeQuery...

    neo4j-rdf-plugin:支持 RDF 图的 Neo4J 服务器插件

    选择Scala作为实现语言,可能是出于其强大的并发处理能力、类型安全以及与Java平台的紧密集成,使得插件可以高效地运行在 Neo4J 服务器上。 【压缩包子文件的文件名称列表】: neo4j-rdf-plugin-master 这个列表...

    Linux的neo4j安装包

    9. **连接应用程序**: 除了通过Browser,还可以通过编程接口(如Java API、Python的neo4j库等)连接到Neo4j,实现数据的自动化处理和分析。 10. **监控与优化**: Neo4j提供了监控工具,帮助跟踪性能指标,并进行...

    springboot+neo4j

    2. **实体映射**:Spring Data Neo4j允许我们通过注解将Java类映射为Neo4j中的节点或关系。例如,使用`@NodeEntity`注解表示一个节点类,`@RelationshipEntity`表示关系类,`@Id`定义唯一标识,`@Property`标注属性...

    neo4j-java-reference-3.0

    这一章节应该涉及如何在Java应用程序中嵌入Neo4j数据库,即不作为独立的数据库服务器运行,而是直接在Java应用程序内部启动和管理。这通常用于需要数据库功能但不需要独立数据库服务器的应用场景。 3. 遍历框架 ...

    Neo4j中文使用手册

    - **部署指南**:详细介绍如何设置和配置Neo4j服务器。 **6. REST API目录前言** - **REST API文档**:包含了所有可用的REST API端点,方便开发者查阅。 - **API调用示例**:提供了如何使用这些API的实际例子。 #...

Global site tag (gtag.js) - Google Analytics