`
sillycat
  • 浏览: 2550895 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

neo4j(4)Java Sample/Scala Client and Routing

 
阅读更多

neo4j(4)Java Sample/Scala Client and Routing

1. Java Sample
Here is the dependencies for neo4j-java in pom.xml
<dependencies>   

  <dependency>            

    <groupId>commons-logging</groupId>            

    <artifactId>commons-logging</artifactId>            

    <version>1.1.3</version>   

  </dependency>   

  <dependency>         

    <groupId>org.neo4j</groupId>         

    <artifactId>neo4j</artifactId>         

    <version>2.1.3</version>   

  </dependency>   

  <dependency>         

    <groupId>log4j</groupId>         

    <artifactId>log4j</artifactId>         

    <version>1.2.17</version>     

  </dependency>

</dependencies>

RailroadExample.java

package com.sillycat.easyhbase.app;

import java.util.Iterator;

import org.neo4j.graphalgo.GraphAlgoFactory;
import org.neo4j.graphalgo.PathFinder;
import org.neo4j.graphalgo.WeightedPath;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.PathExpanders;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.index.Index;

import com.sillycat.easyhbase.util.GraphUtil;
import com.sillycat.easyhbase.util.RelTypes;

public class RailroadExample {

     private static String DB_PATH = "/tmp/neo4j";
     
     public static void main(final String[] args) {
          GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
          GraphUtil.registerShutdownHook(graphDb);
 
          Transaction tx = graphDb.beginTx();
          try {
               Index<Node> nodeIndex = graphDb.index().forNodes("nodes");
              
               Node londonNode = graphDb.createNode();
               //create node London
               londonNode.setProperty("name", "London");
               //add name into index
               nodeIndex.add(londonNode, "name", "London");
 
               //Brighton
               Node brightonNode = graphDb.createNode();
               brightonNode.setProperty("name", "Brighton");
               nodeIndex.add(brightonNode, "name", "Brighton");
 
               //Portsmouth
               Node portsmouthNode = graphDb.createNode();
               portsmouthNode.setProperty("name", "Portsmouth");
               nodeIndex.add(portsmouthNode, "name", "Portsmouth");
 
               //Bristol
               Node bristolNode = graphDb.createNode();
               bristolNode.setProperty("name", "Bristol");
               nodeIndex.add(bristolNode, "name", "Bristol");
 
               //Oxford
               Node oxfordNode = graphDb.createNode();
               oxfordNode.setProperty("name", "Oxford");
               nodeIndex.add(oxfordNode, "name", "Oxford");
 
               //Gloucester
               Node gloucesterNode = graphDb.createNode();
               gloucesterNode.setProperty("name", "Gloucester");
               nodeIndex.add(gloucesterNode, "name", "Gloucester");
 
               //Northampton
               Node northamptonNode = graphDb.createNode();
               northamptonNode.setProperty("name", "Northampton");
               nodeIndex.add(northamptonNode, "name", "Northampton");
 
               //Southampton
               Node southamptonNode = graphDb.createNode();
               southamptonNode.setProperty("name", "Southampton");
               nodeIndex.add(southamptonNode, "name", "Southampton");
 
               // london -> brighton ~ 52mi
               Relationship r1 = londonNode.createRelationshipTo(brightonNode,
                         RelTypes.LEADS_TO);
               //relationship gets property distance
               r1.setProperty("distance", 52);
 
               // brighton -> portsmouth ~ 49mi
               Relationship r2 = brightonNode.createRelationshipTo(portsmouthNode,
                         RelTypes.LEADS_TO);
               r2.setProperty("distance", 49);
 
               // portsmouth -> southampton ~ 20mi
               Relationship r3 = portsmouthNode.createRelationshipTo(
                         southamptonNode, RelTypes.LEADS_TO);
               r3.setProperty("distance", 20);
 
               // london -> oxford ~95mi
               Relationship r4 = londonNode.createRelationshipTo(oxfordNode,
                         RelTypes.LEADS_TO);
               r4.setProperty("distance", 95);
 
               // oxford -> southampton ~66mi
               Relationship r5 = oxfordNode.createRelationshipTo(southamptonNode,
                         RelTypes.LEADS_TO);
               r5.setProperty("distance", 66);
 
               // oxford -> northampton ~45mi
               Relationship r6 = oxfordNode.createRelationshipTo(northamptonNode,
                         RelTypes.LEADS_TO);
               r6.setProperty("distance", 45);
 
               // northampton -> bristol ~114mi
               Relationship r7 = northamptonNode.createRelationshipTo(bristolNode,
                         RelTypes.LEADS_TO);
               r7.setProperty("distance", 114);
 
               // southampton -> bristol ~77mi
               Relationship r8 = southamptonNode.createRelationshipTo(bristolNode,
                         RelTypes.LEADS_TO);
               r8.setProperty("distance", 77);
 
               // northampton -> gloucester ~106mi
               Relationship r9 = northamptonNode.createRelationshipTo(
                         gloucesterNode, RelTypes.LEADS_TO);
               r9.setProperty("distance", 106);
 
               // gloucester -> bristol ~35mi
               Relationship r10 = gloucesterNode.createRelationshipTo(bristolNode,
                         RelTypes.LEADS_TO);
               r10.setProperty("distance", 35);
 
               tx.success();
 
               System.out.println("searching for the shortest route from London to Bristol..");
               PathFinder<WeightedPath> finder = GraphAlgoFactory.dijkstra(
                         PathExpanders.forTypeAndDirection(RelTypes.LEADS_TO,Direction.OUTGOING), "distance");
 
               WeightedPath path = finder.findSinglePath(londonNode, bristolNode);
               System.out.println("London - Bristol with a distance of: " + path.weight() + " and via: ");
               for (Node n : path.nodes()) {
                    System.out.print(" " + n.getProperty("name"));
               }
              
               System.out.println();
               System.out.println();
              
               System.out.println("searching for all routes from London to southamptonNode..");
               PathFinder<Path> finderAll = GraphAlgoFactory.allPaths(
                         PathExpanders.forTypeAndDirection(RelTypes.LEADS_TO,Direction.OUTGOING), Integer.MAX_VALUE);
              
               Iterable<Path> allPaths = finderAll.findAllPaths(londonNode, southamptonNode);
               Iterator<Path> it = allPaths.iterator();
               while(it.hasNext()){
                    Path tmp_path = (Path) it.next();
                    System.out.println("Path is as follow: ");
                    int distance = 0;
                    for (Node n : tmp_path.nodes()) {
                         System.out.print(" " + n.getProperty("name"));
                         distance = distance + (Integer)n.getRelationships(RelTypes.LEADS_TO, Direction.OUTGOING).iterator().next().getProperty("distance");
                    }
                    System.out.println(" with distance:" + distance);
               }
 
 
          } finally {
               tx.close();;
               graphDb.shutdown();
          }
     }
    
}

Some other Util Classes
package com.sillycat.easyhbase.util; import org.neo4j.graphdb.RelationshipType; publicenum RelTypes implements RelationshipType {      KNOWS, LEADS_TO }



package com.sillycat.easyhbase.util;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.index.Index;
import org.neo4j.tooling.GlobalGraphOperations;

public class GraphUtil {

     public static void cleanUp(final GraphDatabaseService graphDb,
               final Index<Node> nodeIndex) {
          for (Node node : GlobalGraphOperations.at(graphDb).getAllNodes()) {
               for (Relationship rel : node.getRelationships()) {
                    rel.delete();
                    //delete all relationship
               }
               //remove all nodes from index
               nodeIndex.remove(node);
               //remote node itself
               node.delete();
          }
 
     }
 
     public static void registerShutdownHook(final GraphDatabaseService graphDb) {
          Runtime.getRuntime().addShutdownHook(new Thread() {
               @Override
               public void run() {
                    graphDb.shutdown();
               }
          });
     }
    
}

It works great. But I do not plan to run neo4j in embedded mode, so I will work to check for other clients.


References:
https://github.com/AnormCypher/AnormCypher

http://www.hascode.com/2012/01/neo4j-graph-database-tutorial-how-to-build-a-route-planner-and-other-examples/
http://jason691353279.iteye.com/blog/2056339

http://docs.neo4j.org.cn/tutorials-java-embedded-traversal.html

分享到:
评论

相关推荐

    neo4j-java-reference-3.1(neo4j官方Java开发指南)

    《neo4j-java-reference-3.1》是Neo4j官方发布的针对Java开发者的详细指南,旨在帮助开发者熟练地利用Java API与Neo4j图形数据库进行交互。这本书涵盖了从安装配置到高级特性的方方面面,是Java程序员在 Neo4j 领域...

    采用java操作neo4j数据库源码

    Java操作Neo4j数据库是利用Java编程语言与Neo4j图数据库进行交互的过程。Neo4j是一种高性能的NoSQL图数据库,它以图形的方式存储和处理数据,特别适合处理复杂的关系网络。在Java中,我们可以借助官方提供的Java驱动...

    neo4j-java-driver-1.7.2.jar

    jar包:neo4j-java-driver-1.7.2.jar 用于java程序中对neo4j数据库进行操作 需要导入的jar包

    reactiveneo, Neo4j的反应式安全 Scala 驱动器.zip

    reactiveneo, Neo4j的反应式安全 Scala 驱动器 reactiveneo 用于 Neo4j的反应式安全 Scala DSL table-内容图模型节点关系索引查询库强制执行强类型检查,对查询格式施加一些限制。 查询中使用的每个 node 和关系都...

    Neo4j-Java开发快速指南

    在Java开发环境中,与Neo4j交互主要依赖于官方提供的Java驱动程序,这使得Java开发者能够利用其丰富的库和工具来构建高效的应用程序。本快速指南将深入探讨如何使用Java API与Neo4j进行集成,以及如何执行常见的...

    基于国内大学构造的neo4j知识图谱,并进行简单问答,帮助了解大学,填报高考志愿

    -v /opt/neo4j/conf:/var/lib/neo4j/conf \ -v /opt/neo4j/import:/var/lib/neo4j/import \ --env NEO4J_AUTH=neo4j/123456 neo4j 3 插入数据 数据来源:先找到了一个大学列表,然后在百度百科中进行爬取, 数据已...

    neo4j-java-reference-3.3

    4. **在Java应用程序中嵌入Neo4j**:介绍了如何将Neo4j嵌入到自己的Java应用程序中,并给出了一些具体的示例代码和配置说明。 5. **遍历框架**:提供了Neo4j遍历框架的详细教程,帮助开发者了解如何利用框架进行...

    neo4j-java-driver-1.0.0.jar

    neo4j-java-driver-1.0.0.jarneo4j-java-driver-1.0.0.jar

    java操作neo4j.zip

    Java操作Neo4j是数据库领域的热门话题,尤其是在处理复杂图数据时。Neo4j是一款强大的图形数据库,它以节点、关系和属性的形式存储数据。在Java应用中,我们通常使用`neo4j-java-driver`库来与Neo4j服务器进行交互,...

    neo4j的java-jdbc驱动

    neo4j的java连接驱动,jdbc,neo4j图数据库,可在java中进行连接。

    neo4j-java驱动

    neo4j-java驱动,可以支持你的开发

    neo4j图数据库社区开源版4.4.5的neo4j-java驱动

    neo4j图数据库社区开源版4.4.5的java驱动,用于java开发处理图数据库相关

    图形数据库Neo4j之Java API使用示例

    需要使用 java API 编写查询接口,参考网上的示例自己写了一个接口类: Neo4jUtil.java。目前只完成了查询方法。创建、删除及修改方法未实现。请自行参考代码来实现。 使用方法: 在 eclipse 中导入解压缩的工程...

    Neo4j Java Reference 3.0

    ### Neo4j Java Reference 3.0:深入理解图数据库扩展与高级应用 #### 概述 《Neo4j Java Reference 3.0》是一本详细介绍如何使用Java语言来开发和扩展Neo4j图数据库的专业指南。本书不仅覆盖了Neo4j的核心功能,...

    Neo4j Java开发快速指南

    本快速指南将深入探讨如何使用Java与Neo4j进行交互,包括创建自定义函数和过程。 首先,理解Neo4j的核心概念是至关重要的。它是一个无模式的图数据库,存储节点(代表实体)、关系(连接节点)和属性(附属于节点或...

    java项目集成neo4j的demo

    Java项目集成Neo4j是一个将图数据库技术引入到传统Java应用程序中的实践过程。Neo4j是一种高性能、无模式的图数据库,特别适合处理高度互联的数据,例如社交网络、推荐系统和知识图谱等。在本Java项目中,我们看到一...

    使用Neo4j中的数据在浏览器中图形化可视化

    neovis.js 由vis.js支持的图形可视化以及来自Neo4j的数据。 产品特点 连接到Neo4j实例以获取实时数据 用户指定的标签和要显示的属性 ...为了便于使用,可以从Neo4jLabs CDN获得Neovis.js: 最新发行 &lt;script src="h

    Java读取CSV插入Neo4j并生成Echarts关系图

    4. **插入数据**:执行Cypher查询将Java对象转化为 Neo4j 图形结构。例如,创建一个新节点,可以使用如下语句: ```cypher CREATE (n:Label {properties}) ``` 其中,`:Label`是节点标签,`properties`是属性...

    Neo4j jar包集合 java项目开发

    4. **事务函数**:`neo4j-transaction-api.jar`包含了用于处理数据库事务的接口和类。在Java项目中,这有助于确保数据的一致性和完整性,因为事务可以确保一组操作要么全部成功,要么全部失败。 5. **图形处理API**...

Global site tag (gtag.js) - Google Analytics