neo4j的examples之EmbeddedNeo4j.java

neo2.3.0 代码
下载:github

/** 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
{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: transactiontry ( 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();}}void shutDown(){System.out.println();System.out.println( "Shutting down database ..." );// START SNIPPET: shutdownServergraphDb.shutdown();// END SNIPPET: shutdownServer}// START SNIPPET: shutdownHookprivate 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();}} );}// END SNIPPET: shutdownHook
}

运行结果:

Hello, brave Neo4j World!
Shutting down database ...

自己稍作修改用于测试:

/** 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 n1;
/***@author xubo601450868* 创建节点relationship等,创建后不删除* */
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
{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,friend}// 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: transactiontry ( Transaction tx = graphDb.beginTx() ){// Database operations go here// END SNIPPET: transaction// START SNIPPET: addDatafirstNode = graphDb.createNode();firstNode.setProperty( "message", "Hello,scala " );secondNode = graphDb.createNode();secondNode.setProperty( "message", "hello World!" );for (int i=1;i<=100;i++){Node firstNode1=graphDb.createNode();firstNode1.setProperty("message", "hello World!"+i);}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" ) );System.out.println("\n"+firstNode+":"+firstNode.getId());System.out.println(relationship);System.out.println(secondNode);// END SNIPPET: readDatagreeting = "greet:"+( (String) firstNode.getProperty( "message" ) )+ ( (String) relationship.getProperty( "message" ) )+ ( (String) secondNode.getProperty( "message" ) );// START SNIPPET: transactionSystem.out.println(greeting);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: removingDatatx.success();}}void shutDown(){System.out.println();System.out.println( "Shutting down database ..." );// START SNIPPET: shutdownServergraphDb.shutdown();// END SNIPPET: shutdownServer}// START SNIPPET: shutdownHookprivate 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();}} );}// END SNIPPET: shutdownHook
}

运行结果:

Hello,scala brave Neo4j hello World!
Node[0]:0
Relationship[0]
Node[1]
greet:Hello,scala brave Neo4j hello World!Shutting down database ...

查询:

package n1;
/***@author xubo601450868* 查询* 可以修改* */
import java.io.IOException;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 n1.EmbeddedNeo4j.RelTypes;public class neo4j3 {private static final String DB_PATH = "target/neo4j-hello-db";//target/neo4j-hello-db//D:/1win81/java/neo4jdatabaseGraphDatabaseService graphDb;Node firstNode;Node secondNode;Relationship relationship;private static enum RelTypes implements RelationshipType{KNOWS,friend}public static void main(String[] args) throws IOException{System.out.println("hello neo4j3");neo4j3 hello = new neo4j3();hello.createDb();hello.removeData();hello.shutDown();}void createDb() throws IOException{graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH) ;registerShutdownHook( graphDb );try ( Transaction tx = graphDb.beginTx() ){// Database operations go here// END SNIPPET: transaction// START SNIPPET: addDataint i;for(i=0;i<=100;i++){
//               graphDb.if(graphDb.isAvailable(i)==true){firstNode=graphDb.getNodeById(i);//System.out.println(firstNode);System.out.println(firstNode+":"+firstNode.getProperty( "message" ));}else{break;}// System.out.println(graphDb.isAvailable(i));}
//              firstNode = graphDb.createNode();
//              firstNode.setProperty( "message", "Hello,scala " );
//              secondNode = graphDb.createNode();
//              secondNode.setProperty( "message", "hello 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" ) );
//              System.out.println("\n"+firstNode+":"+firstNode.getId());
//              System.out.println(relationship);
//              System.out.println(secondNode);// END SNIPPET: readDatatx.success();}}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: removingDatatx.success();}}void shutDown(){System.out.println();System.out.println( "Shutting down database ..." );// START SNIPPET: shutdownServergraphDb.shutdown();// END SNIPPET: shutdownServer}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();}} );}
}

运行结果:

hello neo4j3
Node[0]:Hello,scala
Node[1]:hello World!
Node[2]:hello World!1
Node[3]:hello World!2
Node[4]:hello World!3
Node[5]:hello World!4
Node[6]:hello World!5
Node[7]:hello World!6
Node[8]:hello World!7
Node[9]:hello World!8
Node[10]:hello World!9
Node[11]:hello World!10
Node[12]:hello World!11
Node[13]:hello World!12
Node[14]:hello World!13
Node[15]:hello World!14
Node[16]:hello World!15
Node[17]:hello World!16
Node[18]:hello World!17
Node[19]:hello World!18
Node[20]:hello World!19
Node[21]:hello World!20
Node[22]:hello World!21
Node[23]:hello World!22
Node[24]:hello World!23
Node[25]:hello World!24
Node[26]:hello World!25
Node[27]:hello World!26
Node[28]:hello World!27
Node[29]:hello World!28
Node[30]:hello World!29
Node[31]:hello World!30
Node[32]:hello World!31
Node[33]:hello World!32
Node[34]:hello World!33
Node[35]:hello World!34
Node[36]:hello World!35
Node[37]:hello World!36
Node[38]:hello World!37
Node[39]:hello World!38
Node[40]:hello World!39
Node[41]:hello World!40
Node[42]:hello World!41
Node[43]:hello World!42
Node[44]:hello World!43
Node[45]:hello World!44
Node[46]:hello World!45
Node[47]:hello World!46
Node[48]:hello World!47
Node[49]:hello World!48
Node[50]:hello World!49
Node[51]:hello World!50
Node[52]:hello World!51
Node[53]:hello World!52
Node[54]:hello World!53
Node[55]:hello World!54
Node[56]:hello World!55
Node[57]:hello World!56
Node[58]:hello World!57
Node[59]:hello World!58
Node[60]:hello World!59
Node[61]:hello World!60
Node[62]:hello World!61
Node[63]:hello World!62
Node[64]:hello World!63
Node[65]:hello World!64
Node[66]:hello World!65
Node[67]:hello World!66
Node[68]:hello World!67
Node[69]:hello World!68
Node[70]:hello World!69
Node[71]:hello World!70
Node[72]:hello World!71
Node[73]:hello World!72
Node[74]:hello World!73
Node[75]:hello World!74
Node[76]:hello World!75
Node[77]:hello World!76
Node[78]:hello World!77
Node[79]:hello World!78
Node[80]:hello World!79
Node[81]:hello World!80
Node[82]:hello World!81
Node[83]:hello World!82
Node[84]:hello World!83
Node[85]:hello World!84
Node[86]:hello World!85
Node[87]:hello World!86
Node[88]:hello World!87
Node[89]:hello World!88
Node[90]:hello World!89
Node[91]:hello World!90
Node[92]:hello World!91
Node[93]:hello World!92
Node[94]:hello World!93
Node[95]:hello World!94
Node[96]:hello World!95
Node[97]:hello World!96
Node[98]:hello World!97
Node[99]:hello World!98
Node[100]:hello World!99Shutting down database ...

neo4j的examples之EmbeddedNeo4j.java相关推荐

  1. neo4j安装_Neo4J和Enterprise Java的撤消功能

    neo4j安装 我创建了一个示例,说明如何在使用Neo4J数据库的Quarkus应用程序中实现撤消功能. 从用户的角度来看,撤消操作可能看起来并不引人注目,但是一旦您知道需要在数据库端考虑的内容,它就 ...

  2. java嵌入式开发neo4j_java-嵌入式Neo4j实际如何工作?

    我是neo4j的新手,根据我到目前为止所做的阅读,似乎有两种方法可以使用Neo4j REST和Embedded与neo4j进行交互.我有点困惑的是,"嵌入式"选项是否仅使您能够使用 ...

  3. 知识图谱java实现_知识图谱:neo4j(四)Java API

    知识图谱:neo4j(四)Java API 知识图谱:neo4j(四)Java API Neo4j Java API Neo4j 提供 JAVA API 以编程方式执行所有数据库操作.它支持两种类型的 ...

  4. Java操作Neo4J就是这么简单,#yyds盘点#

    使用 Java 操作 Neo4J 首先我们先使用原生的这种方式,导入 jar 包,然后: public class TestController { public static void main(S ...

  5. Neo4j ③ 管理员操作, 备份恢复, 调优思路, 程序访问, 嵌入式, 服务器模式, Java 操作 Neo4j, 整合 SpringBoot

    目录 第四部分 Neo4j之Admin管理员操作 4.1 Neo4j - 数据库备份和恢复 4.2 调优思路 1.增加服务器内存 和 调整neo4j配置文件 2.neo4j刚启动数据是冷的需要预热 3 ...

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

    Eclipse下maven使用嵌入式(Embedded)Neo4j创建Hello World项目 新建一个maven工程,这里不赘述如何新建maven工程. 添加Neo4j jar到你的工程 有两种方 ...

  7. Neo4j批量插入(Batch Insertion)

    新建一个maven工程,这里不赘述如何新建maven工程. 添加Neo4j jar到你的工程 有两种方式: 上网站官网下载jar包,根据自己的系统下载不同的压缩包,详细过程不描述,请自行搜索其他博客 ...

  8. java编写字符串连接程序注释_一种利用JAVA注释支持多行字符串的方法

    从BeetlSql项目将SQL全放在Beetl模板里得到启发,又想到一个比较偏门的用法.以下代码实测通过,详见jSqlBox项目的test\examples\multipleLineSQL\SqlTe ...

  9. 缓冲区溢出_在Java中使用Google的协议缓冲区

    缓冲区溢出 最近发布了有效的Java第三版,我一直对确定此类Java开发书籍的更新感兴趣,该书籍的最新版本仅通过Java 6进行了介绍. 在此版本中,显然存在与Java 7 , Java 8和Java ...

最新文章

  1. Linux中使用gcp拷贝报错:dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NotSupported:
  2. spring的Autowired和@Resource的区别是什么
  3. Floating-point exception
  4. Spring Cloud Config Server简介
  5. 字符串,数组,定时器,form
  6. 黑科技揭秘:百种异常随机注入,专有云为何稳如泰山 1
  7. Javascript图片滚动
  8. c语言编程指法输入,C语言 课件 第一章引论.pdf
  9. 【最佳实践】【Blend】Triggers、Actions 和 Behaviors
  10. 一个火车上遇到的女孩所引发的联想
  11. 读取图像中任意点的像素值
  12. Bolt界面引擎元对象(UIObject)的动态创建
  13. pc软件签名:数字证书的使用
  14. JVM 内存分析工具MAT
  15. ROS路由器ethernet接口:
  16. 如何写毕业设计——开题报告
  17. creo 6.0—02:单位的设置,默认绘图模板的绘制(重点)
  18. 【Python】​​​​​​​turtle八边形绘制
  19. RAS 在 x86 上的应用及 Linux 实现
  20. 龙之谷服务器仓库在哪个位置,全区全服版本更新至Ver.190

热门文章

  1. 3.1 Python 字符串类型常用操作及内置方法
  2. Pytorch可视化语义分割特征图
  3. 用css做透明效果,CSS实现毛玻璃透明效果
  4. 爪爪博士:入秋遛狗需要注意的一些问题
  5. 【程序设计与实践】实验三:自动售货机
  6. 泰克示波器维修常见的故障现象及处理方法
  7. manifest.json 解析--手机web app开发笔记(三-1)
  8. ZBRUSH 快捷键
  9. 多元函数求极值,万能函数——fmincon讲解
  10. R语言兴起与SAS,SPSS