首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

Neo4j之入门事例

2013-10-31 
Neo4j之入门例子简介:Neo4j是一个网络——面向图网络的数据库——也就是说,它是一个嵌入式的、基于磁盘的、具备

Neo4j之入门例子

简介:

Neo4j是一个网络——面向图网络的数据库——也就是说,它是一个嵌入式的、基于磁盘的、具备完全的事务特性的Java持久化引擎,但是它将结构化数据存储在图网络上而不是表中。图网络(从数学角度叫做图)是一个灵活的数据结构,可以应用更加敏捷和快速的开发模式。

?

嵌入式使用代码如下:

RelationshipType:

enum UserRelationship implements RelationshipType {FELLOW, BELONG}

调用:

public static void main(String[] args) {String DB_PATH = "G:/neo4j/db/data1/";GraphDatabaseFactory factory = new GraphDatabaseFactory();GraphDatabaseService gds = factory.newEmbeddedDatabase(DB_PATH);GlobalGraphOperations ggo = GlobalGraphOperations.at(gds);Transaction tx = gds.beginTx();try {Node fromNode = gds.createNode();fromNode.setProperty("table", "person");fromNode.setProperty("name", "马云");Node toNode = gds.createNode();toNode.setProperty("table", "person");toNode.setProperty("name", "李彦宏");Relationship relationship = fromNode.createRelationshipTo(toNode,UserRelationship.FELLOW);List<String> eventList = new ArrayList<String>();eventList.add("2013福布斯中国富豪榜:李彦宏第三、马化腾第五、马云第八 ");eventList.add("李彦宏推轻应用马云入股浏览器 移动入口争夺暗战升级 ");relationship.setProperty("event", JSON.toJSONString(eventList));Node companyNode = gds.createNode();companyNode.setProperty("table", "company");companyNode.setProperty("name", "阿里巴巴");Relationship belongRelationship = fromNode.createRelationshipTo(companyNode,UserRelationship.BELONG);belongRelationship.setProperty("event", "马云如何掌控阿里巴巴? ");tx.success();} catch (Throwable e) {e.printStackTrace();tx.failure();} finally {tx.finish();}Iterator<Node> iterator = ggo.getAllNodes().iterator();while (iterator.hasNext()) {Node node = iterator.next();Iterator<String> keysIterator = node.getPropertyKeys().iterator();while (keysIterator.hasNext()) {String key = keysIterator.next();System.out.println(key + "->" + node.getProperty(key));}Iterator<Relationship> relationshipsIterator = node.getRelationships().iterator();while (relationshipsIterator.hasNext()) {Relationship relationships = relationshipsIterator.next();System.out.println("关系:" + relationships.getType());}}gds.shutdown();}

maven依赖:

<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.1.36</version></dependency><dependency><groupId>org.neo4j</groupId><artifactId>neo4j-cypher-dsl</artifactId><version>1.9.M04</version></dependency><dependency><groupId>org.neo4j</groupId><artifactId>neo4j-kernel</artifactId><version>${neo4j.version}</version></dependency><dependency><groupId>org.neo4j</groupId><artifactId>neo4j-cypher</artifactId><version>${neo4j.version}</version></dependency><dependency><groupId>org.neo4j</groupId><artifactId>neo4j-com</artifactId><version>${neo4j.version}</version></dependency><dependency><groupId>org.neo4j</groupId><artifactId>neo4j-shell</artifactId><version>${neo4j.version}</version></dependency><dependency><groupId>org.neo4j</groupId><artifactId>neo4j</artifactId><version>${neo4j.version}</version></dependency><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><neo4j.version>1.9.M04</neo4j.version></properties>

?

热点排行