Eclipse下maven使用嵌入式(Embedded)Neo4j创建Hello World项目

新建一个maven工程,这里不赘述如何新建maven工程。

添加Neo4j jar到你的工程

有两种方式:

  • 上网站官网下载jar包,根据自己的系统下载不同的压缩包,详细过程不描述,请自行搜索其他博客
  • 通过maven获得jar包

pom.xml文件下添加dependency

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>nd.esp.com</groupId>
<artifactId>MyNeo4j</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>MyNeo4j</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <!-- Embedded Neo4j依赖,目前最新版本是2.3.3-->
<dependencies>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-slf4j</artifactId>
<version>2.3.3</version>
</dependency>
</dependencies>
</project>

Hello World 程序

代码可以在github上看到:https://github.com/neo4j/neo4j/blob/2.3.3/community/embedded-examples/src/main/java/org/neo4j/examples/EmbeddedNeo4j.java

/*
* Licensed to Neo Technology under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Neo Technology licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.neo4j.examples; import java.io.File;
import java.io.IOException; import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.io.fs.FileUtils; public class EmbeddedNeo4j
{
// Embedded Neo4j会在本地产生一个文件夹(类似于Mysql的数据库)
private static final String DB_PATH = "target/neo4j-hello-db"; public String greeting; // START SNIPPET: vars
GraphDatabaseService graphDb;
Node firstNode;
Node secondNode;
Relationship relationship;
// END SNIPPET: vars // START SNIPPET: createReltype
private static enum RelTypes implements RelationshipType
{
KNOWS
}
// END SNIPPET: createReltype public static void main( final String[] args ) throws IOException
{
EmbeddedNeo4j hello = new EmbeddedNeo4j();
hello.createDb();
// 删除数据
hello.removeData();
hello.shutDown();
} void createDb() throws IOException
{
FileUtils.deleteRecursively( new File( DB_PATH ) ); // START SNIPPET: startDb
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
registerShutdownHook( graphDb );
// END SNIPPET: startDb // START SNIPPET: transaction
// Embedded Neo4j基本上所有的操作都需要在事务内执行
try ( Transaction tx = graphDb.beginTx() )
{
// Database operations go here
// END SNIPPET: transaction
// START SNIPPET: addData
firstNode = graphDb.createNode();
firstNode.setProperty( "message", "Hello, " );
secondNode = graphDb.createNode();
secondNode.setProperty( "message", "World!" ); relationship = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
relationship.setProperty( "message", "brave Neo4j " );
// END SNIPPET: addData // START SNIPPET: readData
System.out.print( firstNode.getProperty( "message" ) );
System.out.print( relationship.getProperty( "message" ) );
System.out.print( secondNode.getProperty( "message" ) );
// END SNIPPET: readData greeting = ( (String) firstNode.getProperty( "message" ) )
+ ( (String) relationship.getProperty( "message" ) )
+ ( (String) secondNode.getProperty( "message" ) ); // START SNIPPET: transaction
tx.success();
}
// END SNIPPET: transaction
}
// 移除新建的数据
void removeData()
{
try ( Transaction tx = graphDb.beginTx() )
{
// START SNIPPET: removingData
// let's remove the data
firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();
firstNode.delete();
secondNode.delete();
// END SNIPPET: removingData tx.success();
}
}
// 关闭Neo4j 数据库
void shutDown()
{
System.out.println();
System.out.println( "Shutting down database ..." );
// START SNIPPET: shutdownServer
graphDb.shutdown();
// END SNIPPET: shutdownServer
} // 为Neo4j 实例注册一个关闭的hook,当VM被强制退出时,Neo4j 实例能够正常关闭
private static void registerShutdownHook( final GraphDatabaseService graphDb )
{
// Registers a shutdown hook for the Neo4j instance so that it
// shuts down nicely when the VM exits (even if you "Ctrl-C" the
// running application).
Runtime.getRuntime().addShutdownHook( new Thread()
{
@Override
public void run()
{
graphDb.shutdown();
}
} );
}
}

在事务中操作

所有的操作都必须在一个事务中完成。这是官方一个刻意的设计,因为他们坚信事务划分是企业型数据库重要的一部分。所以,你可以使用如下的方式开启事务:

try ( Transaction tx = graphDb.beginTx() ){

// Database operations go here

tx.success();

}

try(){}这种语法是在jdk1.7之后支持的,这种方式能够让vm支持自动释放使用结束的资源。在这里可以不需要自己调用语句:

finally{

tx.close();

}

如果你使用低于jdk1.7以后的版本,可以修改为如下的代码:

Transaction tx = graphDb.beginTx()

try{

// Database operations go here

tx.success();

}finally{

tx.close();

}

创建一个简单的图

使用一下代码创建两个节点和一个关系:

firstNode = graphDb.createNode();
firstNode.setProperty( "message", "Hello, " );
secondNode = graphDb.createNode();
secondNode.setProperty( "message", "World!" ); relationship = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
relationship.setProperty( "message", "brave Neo4j " );

创建之后的图数据如下:

本文翻译自官网使用手册:http://neo4j.com/docs/stable/tutorials-java-embedded-hello-world.html

教程结束,感谢阅读。

欢迎转载,但请注明本文链接,谢谢。

2016/4/5 20:09:25

Eclipse下maven使用嵌入式(Embedded)Neo4j创建Hello World项目的更多相关文章

  1. eclipse下maven插件的安装

    最近公司项目要求使用maven来进行项目的管理开发,在这里记录一下eclipse下maven插件的安装. maven插件在eclipse下安装害得我挺恼火的. 我想用最简单的那种方式--在线安装: 通 ...

  2. idea/eclipse下Maven工程集成web服务(tomcat、jetty)

     idea/eclipse下Maven工程集成web服务 转载请注明出处:http://www.cnblogs.com/funnyzpc/p/8093554.html 应用服务器最常用的一般有这哥仨: ...

  3. Eclipse下Maven新建Web项目index.jsp报错完美解决(war包)

    Eclipse下Maven新建Web项目步骤 1. 2. 3. 4. 5. 问题描述 最近用eclipse新建了一个maven项目,结果刚新建完成index.jsp页面就报错了,先把错误信息贴出来看看 ...

  4. maven(5)------eclipse下maven常用命令打包

    eclipse集成maven常用命令clean,install,一步完成项目清理和打包.在集成工具下使用maven 命令与命令窗口不同,需要将mvn省掉(比如:mvn clean,在工具中直接用cle ...

  5. Eclipse下maven部署web项目到tomcat7(兼容tomcat8)

    1.下载tomcat7并配置好JAVA_HOME,tomcat7\webapps目录除了manager之外,其它都可以删除(删除没用的,可加速tomcat的启动). 2.新建系统变量CATALINA_ ...

  6. 嵌入式(Embedded)Neo4j数据库访问方法

    应用中采用嵌入式Neo4j(Embedded Neo4j)数据库,插入数据后不知道如何访问.查询之后知道有Neoclipse这个可视化工具,最新版本是1.9.5.添加目录后报错: 应该是Neoclip ...

  7. eclipse的安装环境及eclipse下maven的配置安装

    之前安装zookeeper的时候,就配置过linux下的java环境,即安装过linux JDK,配置过JAVA_HOME   和PATH  变量,,, 现在要运行一个java客户端,来消费kafka ...

  8. Eclipse下Maven新建项目、自动打依赖jar包(包含普通项目和Web项目)

    不多说,直接上干货! 当我们无法从本地仓库找到需要的构件的时候,就会从远程仓库下载构件至本地仓库.一般地,对于每个人来说,书房只有一个,但外面的书店有很多,类似第,对于Maven来说,每个用户只有一个 ...

  9. eclipse下maven项目保持原有目录结构配置resin运行环境

    maven项目用起来很方便,但是它的目录结构和eclipse的目录结构是有区别的,故而在eclipse下的maven项目,直接运行调试是有一些问题的. 为了方便maven项目的运行调试,因而也就有了像 ...

随机推荐

  1. (转载)jQuery 1.6 源码学习(一)——core.js[1]之基本架构

    在网上下了一个jQuery 1.2.6的源码分析教程,看得似懂非懂,于是还是去github上下载源码,然后慢慢看源代码学习,首先来说说core.js这个核心文件吧. jQuery整体的基本架构说起来也 ...

  2. Python Day02

    Python 代码执行流程: 编译 --> 执行 源代码  -->  字节码  -->  机器码  --> CPU执行 python 先将自己的源代码,编译成Python 字节 ...

  3. Visor 应用之一 通过ER 设计生成数据库脚本和实体对象

    前言 Visor(http://www.visor.com.cn)   是一个基于HTML5 Canvas 开发的IDE 框架和设计开发平台,有关Visor的设计架构和技术应用,在以后的文章里会逐渐跟 ...

  4. Hibernate1

    计应134(实验班) 杨伟 Hibernate的核心接口一共有6个,分别为:Session.SessionFactory.Transaction.Query.Criteria和Configuratio ...

  5. Delphi: 有关Form处理 :需要调用的时候进行调用。

    if not Assigned(frmAppServer) then frmAppServer := TfrmAppServer.Create(Application); frmAppServer.S ...

  6. STM32——CAN通讯实现

    CAN通讯的实现步骤: 1.CAN初始化,其中包括:a.配置CAN时钟,配置IO: b.使能CAN中断向量: c.CAN硬件寄存器配置初始化: d.过滤器初始化: e.打开CAN中断. 2.CAN发送 ...

  7. NSLOG打印不全的问题

    #ifdef DEBUG #define NSLog(FORMAT, ...) fprintf(stderr, "%s:%zd\t%s\n", [[[NSString string ...

  8. Hive的API的说明

    之前通过命令行的界面可以操作Hive,可是在实际的生产环境中,往往都是需要写API的,因此对Hive的API简单的列举了一下.并对Hive进行了一个简单的封装.具体的封装可以参考github网站主页: ...

  9. ASP.NET 缓存技术分析

    缓存功能是大型网站设计一个很重要的部分.由数据库驱动的Web应用程序,如果需要改善其性能,最好的方法是使用缓存功能.可能的情况下尽量使用缓存,从内存中返回数据的速度始终比去数据库查的速度快,因而可以大 ...

  10. Mybatis 源码分析--Configuration.xml配置文件加载到内存

    (补充知识点: 1 byte(字节)=8 bit(位) 通常一个标准英文字母占一个字节位置,一个标准汉字占两个字节位置:字符的例子有:字母.数字系统或标点符号) 1.创建SqlSessionFacto ...