Neo4j批量插入(Batch Insertion)
新建一个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
Neo4j批量插入(Batch Insertion)的更多相关文章
- NHibernate官方文档中文版——批量插入(Batch inserts)
A naive approach t7o inserting 100 000 rows in the database using NHibernate might look like this: 一 ...
- MyBatis 学习笔记(七)批量插入ExecutorType.BATCH效率对比
MyBatis 学习笔记(七)批量插入ExecutorType.BATCH效率对比一.在mybatis中ExecutorType的使用1.Mybatis内置的ExecutorType有3种,默认的是s ...
- 【mybatis】mybatis中批量插入 批量更新 batch 进行insert 和 update,或者切割LIst进行批量操作
================================================================== 分别展示 mybatis 批量新增 和 批量更新 的操作: ...
- Hibernate的批量插入(&&JDBC)
来自: http://blog.csdn.net/an_2016/article/details/51759890 一.批量插入(两种方式) 1,通过hibernate缓存 如果这样写代码进行批量插入 ...
- 批量插入数据(基于Mybatis的实现-Oracle)
前言:做一个数据同步项目,要求:同步数据不丢失的情况下,提高插入性能. 项目DB框架:Mybatis.DataBase:Oracle. -------------------------------- ...
- 使用管道(PipeLine)和批量(Batch)操作
使用管道(PipeLine)和批量(Batch)操作 前段时间在做用户画像的时候,遇到了这样的一个问题,记录某一个商品的用户购买群,刚好这种需求就可以用到Redis中的Set,key作为product ...
- Hibernate批处理操作优化 (批量插入、更新与删除)
问题描述 我开发的网站加了个新功能:需要在线上处理表数据的批量合并和更新,昨天下午发布上线,执行该功能后,服务器的load突然增高,变化曲线异常,SA教育了我一番,让我尽快处理,将CPU负载降低. 工 ...
- 161102、MyBatis中批量插入
方法一: <insert id="insertbatch" parameterType="java.util.List"> <selectKe ...
- PHP框架 Laravel Eloquent ORM 批量插入数据 && 批量更新目前没有
foreach ($products as $v=>$a) { $count[] = array('product_name' => $a['name'], 'product_weight ...
随机推荐
- 掌握Redmine
一个带有建议.技巧和最佳实践的全面指导和易懂易学的结构. 掌握Redmine 版权©2013 Packt出版 前言(略) 1.熟悉Redmin 我们尝试去做一个新的网站应用程序的时候,回去询问一些了解 ...
- CVPR历年Best Papers
作者:我爱机器学习原文链接:CVPR历年Best Papers CVPR (Computer Vision)(2000-2016) 年份 标题 一作 一作单位 2016 Deep Residual L ...
- java web学习之表单
前台页面与后台页面的数据又form表单完成. <form name ="form1" method="post" action="index ...
- 作业七:团队项目——Alpha版本冲刺阶段005
今日进展:编写代码. 今日安排:完成开始部分代码.
- 如何处理C#的HttpWebResponse的GetResponse中的超时异常
程序中,有时会遇到超时的异常,需要进行处理,用一般的try...catch(Exception ex)...会发现ex没有status属性,此时使用WebException捕获异常: try { re ...
- Birt使用总结
把report放到其他服务器要重新建立Data Source ,这是配置,拷贝项目时不会同时拷贝 (1)在EXTJs中利用Report实现报表的刷新 Ext.getCmp("showview ...
- x.1
最近公司人事变动略频 昨日老板召集众骨干动员,谈心,表示有信心,没资金压力. 今日各种谈心,唉…… 人事姐姐约逻辑组长聊,美术主管就找上了我,一通倾述.内容实事求是,但是行业内各公司都这样,唉,还想着 ...
- maven项目管理利器
一.maven介绍及环境搭建 maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建.报告和文档的软件项目管理工具. maven可以更有效的管理项目,也是一套功能强大的自动化管 ...
- Activity的保存状态和状态恢复
Activity的保存状态和状态恢复 当系统内存不足时,系统会强制结束一些不可见的Activity以节省内存资源.在某些情况下,当被强制结束的Activity再次显示时会出现一些问题. 例如:一个AP ...
- 修改HTTPD.CONF中的DocumentRoot,出现 You don't have permission to access /??? on this server.
apache 2.4 修改/conf/extra/httpd-vhosts.conf <VirtualHost _default_:80> DocumentRoot '${SRVROOT} ...