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. Android FragmentTransactionExtended:使Fragment以多种样式动画切换

    有多种fragment之间切换的效果,效果是这样的: Demo的实现是很简单的. 在res/values中,新建一个arrays.xml文件,存放Fragment动画效果的名称,在spinner中使用 ...

  2. 个人纪录(初)----配置文件.properties的简单读取

    本文为个人文本纪录. demo:::: 1.创建普通的java项目:这实例项目名字叫properties. 2.创建.properties文件:src目录下创建XX.properties文件,识别&q ...

  3. 初始化git远程仓库步骤

    Git global setup git config --global user.name "bingo" git config --global user.email &quo ...

  4. Python学习之路

    从今天开始学习Python,没有什么特殊的目的,只是觉得这门语言比较有意思,既然现在可能也不会用到,那就直接从Python3学起吧! 本人开始学习时,还是喜欢听别人讲,于是又找到了小甲鱼的<零基 ...

  5. eclipse + python dev

    错误:Project interpreter not specified解决方法 http://blog.csdn.net/magictong/article/details/7288732 安装Py ...

  6. mysql5.7.10免安装版出现的问题及应对

    在对应目录下新建my.ini [mysqld] # 设置mysql的安装目录 basedir=D:\mysql-5.7.10 # 设置mysql数据库的数据的存放目录,必须是data datadir= ...

  7. Yii Uploadify批量上传

    控制器: $reinfo = "fail"; $filename=""; //重要说明: //使用uploadify 上传时,每次这个sessionID都会改变 ...

  8. JAVAFX纯手写布局

    主页面效果: 第一栏的效果: 工程目录: package MessageBean; /** * * @author novo */ public class Message { private Str ...

  9. ajax+php数据增加查询获取删除

    前段代码部分其实前面已经有写出但是做一点修改所以还是贴出来,可能大家看到代码回不理解,看完图我想大家会理解我为什么这么写了,这和前端布局有关系的,先列出内容在选择内容删除或修改 <!DOCTYP ...

  10. 2014年7月份第2周51Aspx源码发布详情

      体育馆综合会员管理系统源码  2014-7-11 [VS2010]功能介绍:本系统适用于羽毛球馆,台球馆,乒乓球馆,棋牌室,篮球馆等综合体育馆,可同时使用.本系统功能非常强大,包含体育馆内餐厅,超 ...