天天看點

連結Neo4j圖資料庫+最短路徑分析功能

連接配接圖資料庫+最短路徑分析功能

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.neo4j.driver.v1.*;
import org.neo4j.driver.v1.types.Node;
import org.neo4j.driver.v1.types.Path;
import org.neo4j.driver.v1.types.Relationship;

import java.util.*;

public class Neo4jBatchTest {

    static Driver driver = GraphDatabase.driver("bolt://127.0.0.1:7687", AuthTokens.basic("neo4j", "123456"));

    private static Session session = driver.session();

    public static void main(String[] args) throws Exception {
        shortEstPath();
    }

    /**
     * 批量建立
     *
     * @throws Exception
     */
    public static void shortEstPath() throws Exception {

        try {

            String cmdSql = "MATCH n=shortestPath((a)-[*]-(b))  where id(a)=31824 and id(b)=31861 return n";

            StatementResult result = session.run(cmdSql);
            JSONObject jsonOb=new JSONObject();
            while (result.hasNext()) {
                Record record = result.next();
                List<Value> values = record.values();
                Map nodesMap = new HashMap<>();
                JSONArray jsonArray=new JSONArray();

                for (Value value : values) {
                    if (value.type().name().equals("PATH")) {
                        Path p = value.asPath();
                        System.out.println("關系最短路徑長度為:" + p.length());
                        System.out.println("====================================");
                        Iterable<Node> nodes = p.nodes();
                        for (Node node : nodes) {
                            nodesMap.put(node.id(), node);
                        }
                        //列印最短路徑裡面的關系 == 關系包括起始節點的ID和末尾節點的ID,以及關系的type類型
                        Iterable<Relationship> relationships = p.relationships();
                        for (Relationship relationship : relationships) {
                            JSONObject jsonObject=new JSONObject();
                            Long startID = relationship.startNodeId();
                            Long endID = relationship.endNodeId();
                            String rType = relationship.type();
                        //asMap 相當于 節點的properties屬性資訊
                        //    System.out.println(nodesMap.get(startID).asMap()+"-" + rType + "-" + nodesMap.get(endID).asMap());
                            System.out.println(startID+"-" + rType + "-" + endID);
                            jsonObject.put("start",startID);
                            jsonObject.put("type",rType);
                            jsonObject.put("end",endID);
                            jsonArray.add(jsonObject);
                        }
                    }
                }
                jsonOb.put("data",jsonArray);
            }
            System.out.println(jsonOb);

        } catch (Exception e) {
            System.err.println(e.getClass() + "," + e.getMessage());

        }

    }

}

           

依賴

<dependency>
            <!-- 伺服器開發需要的jar包 -->
            <groupId>org.neo4j.driver</groupId>
            <artifactId>neo4j-java-driver</artifactId>
            <version>1.5.0</version>
        </dependency>
   
    <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.78</version>
        </dependency>