当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

neo4j 入门实例

发表于: 2014-02-14   作者:blackproof   来源:转载   浏览次数:
摘要:     neo4j 2.0.1实例   package com.neo4j.demo1; import org.neo4j.graphdb.Direction; import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.Node; import org.neo4j

 

 

neo4j 2.0.1实例

 

package com.neo4j.demo1;

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;


public class HelloWorldTest {
	
	private static enum RelTypes implements RelationshipType{
		KNOWS
	};
	
	GraphDatabaseService graphDb;
	Node firstNode;
	Node secondNode;
	Relationship relationship;
	

	private static final String DB_PATH = "demo1/db1";//"neo4j-db";//

	public static void main(String[] args) {
		HelloWorldTest hwt = new HelloWorldTest();
		hwt.run();
	}
	
	public void run() {
		//创建连接,生成图
		GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
		registerShutdownHook( graphDb );
		//创建事务,
		Transaction tx = graphDb.beginTx();
		try {
			//添加节点
			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");
			
			print();
			
			delete();
			tx.success();//提交
		} finally{
			tx.finish();
			graphDb.shutdown();
		}
	}
	
	private void delete(){
		firstNode.getSingleRelationship(RelTypes.KNOWS, Direction.OUTGOING).delete();
		firstNode.delete();
		secondNode.delete();
	}
	
	private void print() {
		System.out.println(firstNode.getProperty("message"));
		System.out.println(relationship.getProperty("message"));
		System.out.println(secondNode.getProperty("message"));
	}

	private static void registerShutdownHook(final GraphDatabaseService graphDb) {
		Runtime.getRuntime().addShutdownHook(
				new Thread(){
					@Override
					public void run(){
						graphDb.shutdown();
					}
				}
			);
	}


}

 

neo4j 入门实例

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
I want to write a extention to provide REST API for client to add a new user to Neo4j databas
一、High Availability 高可用 neo4j高可用集群永远一台master,0到多台slave。不同于类似mongo的副
安装neo4j高可用集群,抓图安装过程 http://www.ibm.com/developerworks/cn/java/j-lo-neo4j/ Step1
图数据库实践系列 (三)--Neo4j Spatial的REST集成 - 林场 - 博客园 图数据库实践系列 (三)--Neo4j S
上讲回顾:主要讲述Neo4j Spatial项目以及空间数据(矢量)的存储。本文主要介绍Neo4j Spatial与Neo4j
neo4j——图数据库初探 - JDream314的专栏 - 博客频道 - CSDN.NET 图数据库neo4j初探(下载地址:ht
上讲回顾:介绍了Neo4j的概念和安装,同时包括介绍了Git和Maven两个工具。本文会介绍Neo4j Spatial
Neo4j 是什么?首先让我们看看 Neo4j 自己是怎么介绍自己的。下面是 Neo4j 官网 (www.neo4j.org) 首
首先介绍一下neo4j的使用背景,本人在做一个关于社交网络分析的项目,所以有很多关于图的算法和构建
原创不易,转载请注明出处:SpringMVC入门教程及其原理讲解和实例代码下载 代码下载地址:http://ww
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号