新建一个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: varsGraphDatabaseService graphDb;Node firstNode;Node secondNode;Relationship relationship;// END SNIPPET: vars// START SNIPPET: createReltypeprivate static enum RelTypes implements RelationshipType{KNOWS}// END SNIPPET: createReltypepublic 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: startDbgraphDb = 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: addDatafirstNode = 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: readDataSystem.out.print( firstNode.getProperty( "message" ) );System.out.print( relationship.getProperty( "message" ) );System.out.print( secondNode.getProperty( "message" ) );// END SNIPPET: readDatagreeting = ( (String) firstNode.getProperty( "message" ) )+ ( (String) relationship.getProperty( "message" ) )+ ( (String) secondNode.getProperty( "message" ) );// START SNIPPET: transactiontx.success();}// END SNIPPET: transaction}// 移除新建的数据void removeData(){try ( Transaction tx = graphDb.beginTx() ){// START SNIPPET: removingData// let's remove the datafirstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();firstNode.delete();secondNode.delete();// END SNIPPET: removingDatatx.success();}}// 关闭Neo4j 数据库void shutDown(){System.out.println();System.out.println( "Shutting down database ..." );// START SNIPPET: shutdownServergraphDb.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(){@Overridepublic void run(){graphDb.shutdown();}} );}
}

在事务中操作

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

try ( Transaction tx = graphDb.beginTx() ){// Database operations go heretx.success();
}

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

finally{tx.close();
}

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

Transaction tx = graphDb.beginTx()
try{// Database operations go heretx.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 " );

创建之后的图数据如下:
HelloWorld输出结果

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

教程结束,感谢阅读。
欢迎转载,但请注明本文链接,谢谢。
2016/4/5 20:09:25

Neo4j批量插入(Batch Insertion)相关推荐

  1. hibernate 批量插入 Batch

    批量插入(Batch inserts) 如果要将很多对象持久化,你必须通过经常的调用 flush()以及稍后调用 clear()来控制第一级缓存的大小. Session session = sessi ...

  2. NHibernate官方文档中文版——批量插入(Batch inserts)

    A naive approach t7o inserting 100 000 rows in the database using NHibernate might look like this: 一 ...

  3. Java豆瓣电影爬虫——减少与数据库交互实现批量插入

    节前一个误操作把mysql中record表和movie表都清空了,显然我是没有做什么mysql备份的.所以,索性我把所有的表数据都清空的,一夜回到解放前-- 项目地址:https://github.c ...

  4. mysql基础----mybatis的批量插入(一)

    这里面记录一下使用mybatis处理mysql的批量插入的问题,测试有可能不准.只愿世间风景千般万般熙攘过后,字里行间,人我两忘,相对无言. mybatis的批量插入 我们的测试主体类是springb ...

  5. MyBastis 三种批量插入方式的性能比较

    数据库使用的是MySQL,JDK版本1.8,运行在SpringBoot环境下 本文章源代码:https://github.com/runbeyondmove/mybatis-batch-demo 对比 ...

  6. EF批量插入太慢?那是你的姿势不对

    大概所有的程序员应该都接触过批量插入的场景,我也相信任何的程序员都能写出可正常运行的批量插入的代码.但怎样实现一个高效.快速插入的批量插入功能呢? 由于每个人的工作履历,工作年限的不同,在实现这样的一 ...

  7. mysql 主键 最佳实践_Spring Data Jpa + MySQL IDENTITY 主键下批量插入最佳实践

    Spring Data Jpa 虽然可以使用参数调整批量插入,但是仅限于主键策略不是 IDENTITY 的情况下,对于习惯了使用 IDENTITY 的 MySQL 选手来说,批量插入数据直接就悲剧了. ...

  8. mysql批量插入 增加参数_MySql 的批量操作,要加rewriteBatchedStatements参数

    MySql 的批量操作,要加rewriteBatchedStatements参数 作者:赵磊 博客:http://elf8848.iteye.com ------------------------- ...

  9. Mybatis-Plus批量插入数据太慢,使用rewriteBatchedStatements属性优化,堪称速度与激情!

    rewriteBatchedStatements神秘属性 前言 一.rewriteBatchedStatements参数 二.批量添加员工信息 1.普通saveBatch批量插入 2.设置rewrit ...

最新文章

  1. 图像处理(五)双指数磨皮
  2. 解决ubuntu16.04 qt5.9.1无法输入中文
  3. [Leetcode][第207题][JAVA][课程表][拓扑排序][DFS]
  4. Linux常用命令—文件处理命令格式与目录处理命令ls
  5. 设计灵感|音乐播放器App界面如何设计?
  6. js获得服务器时间并实时更新
  7. Linux LVM管理
  8. 火狐翻译插件_阅读外文必备,浏览器实用的翻译插件推荐
  9. 计算机学术论文写作与发表
  10. windows server2008 部署项目环境总结
  11. 自动化办公:2、Python自动化之Excel读取表格+设置样式
  12. 爬虫,第十次实战之线程池(梨视频下载)
  13. jdbc批量插入的4种方式【百万条数据插入只需几秒】
  14. FFMPEG4.1源码分析之 内存管理APIs av_freep() av_free()
  15. BIM+GIS技术为工程数字化转型提供了新的契机
  16. 2022登高架设理论题库及答案
  17. 微距昆虫摄影的常用技巧
  18. 智慧社区AcrelCloud-3200预付费云平台
  19. ubuntu下载软件包下载中断,并弹出“下载额外数据不成功”的通知。解决办法
  20. win10系统如何启动sql服务器,win10在装SQLServer时提示服务没有法启动如何办?

热门文章

  1. 苹果笔记本android studio安装教程,MAC下如何安装AndroidStudio
  2. ORACLE 11G中的ADR介绍:
  3. 21考研复试上机常见题型、技巧与方法
  4. WINDOWS自启动程序的10大隐身之所
  5. ICL7135的串行采集方式在单片机电压表中的应用
  6. 基于Springboot+Mybatis仓库系统
  7. 人工智能Java SDK:菜品分类识别
  8. exe软伯解密工具_值得收藏!最全勒索解密工具等你来拿
  9. 计算机网络工程期刊,信息工程学院李挥教授课题组在计算机网络顶级期刊JSAC上发表学术论文...
  10. 学习Python的14张思维导图