一: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();
- }
- }
http://blog.csdn.net/adam_wzs/article/details/8622250
相关推荐
// Neo4j服务器地址 private static final String USER = "neo4j"; // 用户名 private static final String PASSWORD = "password"; // 密码 private static Driver driver; public static synchronized ...
3. **驱动程序**:`neo4j-java-driver`是官方提供的Java客户端,用于与Neo4j服务器通信。它提供了创建、读取、更新和删除(CRUD)操作的API,使得在Java代码中操作图数据库变得简单。 4. **配置连接**:在Java项目...
在Java应用中,我们通常使用`neo4j-java-driver`库来与Neo4j服务器进行交互,实现创建、读取、更新和删除(CRUD)等基本操作。以下将详细讲解如何使用Java操作Neo4j。 首先,我们需要在项目中引入`neo4j-java-...
- 嵌入式方式连接Neo4j,意味着Java应用程序直接运行在Neo4j数据库进程中,无需额外的服务器。 - 编写Java代码以创建和查询图形数据库,利用Neo4j提供的API操作节点、关系和属性。 6. **可视化Neo4j数据**: - ...
1.基于spring-data-neo4j 3.2.0通过REST远程连接Neo4j服务器,并非嵌入式连接; 2.创建接口用于创建一个简单的图存数据库,实现一个中心点到其他十个点的连接; 3.提供删除接口,可删除所有点和边; 4.提供查询...
Neo4j为Java开发者提供了Java驱动程序,包括`Neo4jDriver`,它允许通过bolt协议连接到数据库。此外,还有`Session`和`Transaction`概念,用于执行数据库操作。 3. **安装与配置**: 安装Neo4j涉及到下载服务器...
- **嵌入式模式**:Neo4j 可以直接嵌入到 Java 应用程序中运行,无需单独的服务器。 - **远程客户端访问**:除了嵌入式模式外,Neo4j 还支持远程客户端访问,这允许开发者从不同的应用程序中访问同一个 Neo4j 数据库...
如果你需要创建自己的Java函数或过程,可以编写Java类并打包为JAR,然后在Neo4j服务器上加载。 5. **APOC库**: APOC是Neo4j的一个强大扩展,提供了大量的实用函数和过程。例如,你可以使用它来导入数据、执行复杂的...
通过以下代码建立与Neo4j服务器的连接: ```java import org.neo4j.driver.Driver; import org.neo4j.driver.GraphDatabase; public class Neo4jConnector { private static final String URI = "bolt://localhost...
3. **驱动程序**:Neo4j提供了多种Java驱动,如`neo4j-java-driver.jar`,它是一个异步非阻塞的驱动,使得Java应用能够安全高效地连接到Neo4j服务器。通过这个驱动,开发者可以创建会话,执行Cypher查询,并获取结果...
解压后,你可以通过运行`bin/neo4j console`启动Neo4j服务器。注意,为了安全和性能,建议在生产环境中配置适当的环境变量和修改默认配置文件`conf/neo4j.conf`。 对于Windows用户,提供了`neo4j-community-3.4.9-...
该手册详细介绍了如何使用Neo4j的官方Java驱动程序,包括驱动程序的安装、使用示例、配置、连接、会话管理、事务处理、查询执行以及异常处理等关键知识点。下面将详细阐释这些知识点。 ### 获取入门 手册首先介绍...
如果你的应用需要处理大量的数据,你可以扩展Neo4j服务器,增加服务器的数量。为了保证数据的安全,Neo4j也提供了数据备份和安全功能。 Neo4j提供了一个基于Web的图数据库管理工具,你可以通过它来管理Neo4j数据库...
- 介绍了如何部署和配置Neo4j服务器,以及如何使用REST API与服务器交互。 - 讨论了如何通过扩展Neo4j服务器来增加自定义功能。 以上内容概述了《neo4j使用文档》中涵盖的主要知识点,从Neo4j的基本概念到具体的...
3. **建立连接**:通过`DriverManager.getConnection()`方法创建一个到Neo4j服务器的连接,提供URL、用户名和密码作为参数。 4. **执行查询**:创建`Statement`或`PreparedStatement`对象,然后调用其`executeQuery...
选择Scala作为实现语言,可能是出于其强大的并发处理能力、类型安全以及与Java平台的紧密集成,使得插件可以高效地运行在 Neo4J 服务器上。 【压缩包子文件的文件名称列表】: neo4j-rdf-plugin-master 这个列表...
9. **连接应用程序**: 除了通过Browser,还可以通过编程接口(如Java API、Python的neo4j库等)连接到Neo4j,实现数据的自动化处理和分析。 10. **监控与优化**: Neo4j提供了监控工具,帮助跟踪性能指标,并进行...
2. **实体映射**:Spring Data Neo4j允许我们通过注解将Java类映射为Neo4j中的节点或关系。例如,使用`@NodeEntity`注解表示一个节点类,`@RelationshipEntity`表示关系类,`@Id`定义唯一标识,`@Property`标注属性...
这一章节应该涉及如何在Java应用程序中嵌入Neo4j数据库,即不作为独立的数据库服务器运行,而是直接在Java应用程序内部启动和管理。这通常用于需要数据库功能但不需要独立数据库服务器的应用场景。 3. 遍历框架 ...
- **部署指南**:详细介绍如何设置和配置Neo4j服务器。 **6. REST API目录前言** - **REST API文档**:包含了所有可用的REST API端点,方便开发者查阅。 - **API调用示例**:提供了如何使用这些API的实际例子。 #...