Eclipse下maven使用嵌入式(Embedded)Neo4j创建Hello World项目
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项目的更多相关文章
- eclipse下maven插件的安装
最近公司项目要求使用maven来进行项目的管理开发,在这里记录一下eclipse下maven插件的安装. maven插件在eclipse下安装害得我挺恼火的. 我想用最简单的那种方式--在线安装: 通 ...
- idea/eclipse下Maven工程集成web服务(tomcat、jetty)
idea/eclipse下Maven工程集成web服务 转载请注明出处:http://www.cnblogs.com/funnyzpc/p/8093554.html 应用服务器最常用的一般有这哥仨: ...
- Eclipse下Maven新建Web项目index.jsp报错完美解决(war包)
Eclipse下Maven新建Web项目步骤 1. 2. 3. 4. 5. 问题描述 最近用eclipse新建了一个maven项目,结果刚新建完成index.jsp页面就报错了,先把错误信息贴出来看看 ...
- maven(5)------eclipse下maven常用命令打包
eclipse集成maven常用命令clean,install,一步完成项目清理和打包.在集成工具下使用maven 命令与命令窗口不同,需要将mvn省掉(比如:mvn clean,在工具中直接用cle ...
- Eclipse下maven部署web项目到tomcat7(兼容tomcat8)
1.下载tomcat7并配置好JAVA_HOME,tomcat7\webapps目录除了manager之外,其它都可以删除(删除没用的,可加速tomcat的启动). 2.新建系统变量CATALINA_ ...
- 嵌入式(Embedded)Neo4j数据库访问方法
应用中采用嵌入式Neo4j(Embedded Neo4j)数据库,插入数据后不知道如何访问.查询之后知道有Neoclipse这个可视化工具,最新版本是1.9.5.添加目录后报错: 应该是Neoclip ...
- eclipse的安装环境及eclipse下maven的配置安装
之前安装zookeeper的时候,就配置过linux下的java环境,即安装过linux JDK,配置过JAVA_HOME 和PATH 变量,,, 现在要运行一个java客户端,来消费kafka ...
- Eclipse下Maven新建项目、自动打依赖jar包(包含普通项目和Web项目)
不多说,直接上干货! 当我们无法从本地仓库找到需要的构件的时候,就会从远程仓库下载构件至本地仓库.一般地,对于每个人来说,书房只有一个,但外面的书店有很多,类似第,对于Maven来说,每个用户只有一个 ...
- eclipse下maven项目保持原有目录结构配置resin运行环境
maven项目用起来很方便,但是它的目录结构和eclipse的目录结构是有区别的,故而在eclipse下的maven项目,直接运行调试是有一些问题的. 为了方便maven项目的运行调试,因而也就有了像 ...
随机推荐
- repcache实现memcached主从
1.repcached介绍 repcached是日本人开发的实现memcached复制功能,它是一个单 master单 slave的方案,但它的 master/slave都是可读写的,而且可以相互同步 ...
- 计算机网络(8)-----TCP报文段的首部格式
TCP报文段的首部格式 概述 TCP报文段首部的前20个字节是固定的,因此TCP首部的最小长度是20字节. 源端口和目标端口 各占2个字节,分别写入源端口号和目的端口号. 序列号 占4个字节,表示本报 ...
- Spring(1)
一.Spring是什么? .Spring是一个开源的框架 .是一个IOC(DI)和AOP容器的框架 .这个框架是为了简化企业级应用开发而生的,使用Spring可以使简单的JavaBean实现以前只有E ...
- Android Binder
http://blog.csdn.net/luoshengyang/article/details/6618363 Android进程间通信(IPC)机制Binder简要介绍和学习计划
- android开发-小技巧篇(集合)
1.对于过多的控件,功能类似,数量又多的,可以用include方法.在实现应用中,可以把控件放入List集合中. private void initView() { // TODO Auto-gene ...
- 例子:Bluetooth app to device sample
本例子演示了: 判断蓝牙是否打开,是通过一个HRsult值为0x8007048F的异常来判断的 catch (Exception ex) { if ((uint)ex.HResult == 0x800 ...
- output和returnvalue的作用
贴两段代码. 1> public int ExecuteNonQuery(string pro, MobileOrder or) { SqlParameter ...
- VS调试时下不到断点的处理方式。
调试无法命中断点的情况我想很多人遇到过,反正我是遇到过很多次了,有时候是没有生成项目或解决方案,有时候是调试版本不一致. 当然还有其他的情况都已经忘记如何处理的了. 今天在release模式下要调试代 ...
- linux下删除文件夹的命令
使用rm -rf 目录名字 命令即可 -r 就是向下递归,不管有多少级目录,一并删除-f 就是直接强行删除,不作任何提示的意思 eg 删除文件夹实例:rm -rf /var/log/httpd/acc ...
- java语法基本知识
java中,变量分为局部和成员变量.局部变量在程序运行的过程中在栈stack中分配存储空间. 从上到下是:heap, stack, data segment, code segment.