One, foreword

Open the APP of “Beijing Subway” and input the starting point: Huoying and the ending point: Beijing South Railway Station. We found that the system recommended two routes for us. The shortest time route and the least interchange route are given respectively. See here, can not help but open the boy question, if give you the subway station related data, how to build such a relationship network? (Write as little code as possible, after all, I am lazy, spend the least effort, the most dazzling effect, yi)

1. Organize subway station data and process it into the structure required by Echarts chart

2. Collate the data of subway stations, write code to achieve it, can achieve it (tired ah…)

3. Sort out subway station data, import it into Neo4j, and click to view it

Analysis: Plan 1, not only processing data but also echarts style, forget it. Plan 2 is to PASS directly, or plan 3 is to save effort. It just so happens that I have installed the Neo4j graph database on my computer before. Ha ha ha!

Software download address: doc.we-yun.com:1008/ neo4J-CHS

Software installation tutorial: www.cnblogs.com/jstarseven/…

2. Data preparation

Since the subway relationship needs to be displayed, the first thing we need is the information of all subway stations in Beijing, as well as the relationship between stations, distance and time consumption. After searching around, I found all the subway lines, station names, distance between stations and other information on the official website of Beijing Metro. But the lack of specific stations and stations between the bus time, ok, first with it! Specific data samples are as follows:

Source address: www.bjsubway.com/station/zjg…

Site relationship

After 18 lines, a quick copy and paste and Notepad++ text replacement, we have an Excel file that contains the relational data for all the sites as follows:

Access to the site

Copy all sites into a sheet in Excel file and select data deduplication to get all site names.

Handle the CSV

The site relationship and site data are processed into CSV file format for easy import into Neo4j database to establish graph nodes and relationships. Notepad++ replaces’ ‘with’, ‘and saves it as a.csv file. (Because it is convenient for different routes in Neo4j database to display different colors, I specially disintegrate the site relationship data into CSV files of multiple subway lines. If the display is not considered, we can import the site relationship into a CSV file.)

Note: Place all CSV files in the import folder under the Neo4j installation directory (if not created) as follows:

File download address: files-cdn.cnblogs.com/files/jstar…

Third, establish subway network

Neo4j supports importing CSV file data from local and network resources, and can directly establish graph nodes and node relationships from CSV file data. Specific CYPher statements are as follows:

Setting up metro stations

LOAD CSV WITH HEADERS  FROM "file:///station.csv" AS line
MERGE (p:Station{id:line.id,name:line.name});
Copy the code

Establishing site connections

LOAD CSV WITH HEADERS FROM "file:///line1.csv" AS line1
match (from1:Station{name:line1.sn}),(to1:Station{name:line1.en})
merge (from1)-[r1: line1 {jl:line1.jl,xl:line1.xl}]->(to1);

LOAD CSV WITH HEADERS FROM "file:///line2.csv" AS line2
match (from2:Station{name:line2.sn}),(to2:Station{name:line2.en})
merge (from2)-[r2: line2 {jl:line2.jl,xl:line2.xl}]->(to2);

LOAD CSV WITH HEADERS FROM "file:///line4.csv" AS line4
match (from4:Station{name:line4.sn}),(to4:Station{name:line4.en})
merge (from4)-[r4: line4 {jl:line4.jl,xl:line4.xl}]->(to4);

LOAD CSV WITH HEADERS FROM "file:///line5.csv" AS line5
match (from5:Station{name:line5.sn}),(to5:Station{name:line5.en})
merge (from5)-[r5: line5 {jl:line5.jl,xl:line5.xl}]->(to5);

LOAD CSV WITH HEADERS FROM "file:///line6.csv" AS line6
match (from6:Station{name:line6.sn}),(to6:Station{name:line6.en})
merge (from6)-[R6 :line6.jl,xl:line6.xl}]->(to6);

LOAD CSV WITH HEADERS FROM "file:///line7.csv" AS line7
match (from7:Station{name:line7.sn}),(to7:Station{name:line7.en})
merge (from7)-[R7: xl {jL :line7.jl,xl:line7.xl}]->(to7);

LOAD CSV WITH HEADERS FROM "file:///line8.csv" AS line8
match (from8:Station{name:line8.sn}),(to8:Station{name:line8.en})
merge (from8)-{jL :line8. Jl,xl:line8. Xl}->(to8);

LOAD CSV WITH HEADERS FROM "file:///line9.csv" AS line9
match (from9:Station{name:line9.sn}),(to9:Station{name:line9.en})
merge (from9)-[r9: line9 {jl:line9.jl,xl:line9.xl}]->(to9);

LOAD CSV WITH HEADERS FROM "file:///line10.csv" AS line10
match (from10:Station{name:line10.sn}),(to10:Station{name:line10.en})
merge (from10)-[r10: line 10 {jl: line10 jl, xl: line10. Xl}]->(to10);

LOAD CSV WITH HEADERS FROM "file:///line13.csv" AS line13
match (from13:Station{name:line13.sn}),(to13:Station{name:line13.en})
merge (from13)-[r13:13 line {jl: line13. Jl, xl: line13. Xl}]->(to13);

LOAD CSV WITH HEADERS FROM "file:///line14.csv" AS line14
match (from14:Station{name:line14.sn}),(to14:Station{name:line14.en})
merge (from14)-[r14:14th line {jl: line14 jl, xl: line14. Xl}]->(to14);

LOAD CSV WITH HEADERS FROM "file:///line15.csv" AS line15
match (from15:Station{name:line15.sn}),(to15:Station{name:line15.en})
merge (from15)-[line r15:15 {jl: line15. Jl, xl: line15. Xl}]->(to15);

LOAD CSV WITH HEADERS FROM "file:///linebt.csv" AS linebt
match (frombt:Station{name:linebt.sn}),(tobt:Station{name:linebt.en})
merge (frombt)-{[RBT: metro batong line jl: linebt jl, xl: linebt. Xl}]->(tobt);

LOAD CSV WITH HEADERS FROM "file:///linecp.csv" AS linecp
match (fromcp:Station{name:linecp.sn}),(tocp:Station{name:linecp.en})
merge (fromcp)-[the RCP: changping line {jl: linecp jl, xl: linecp. Xl}]->(tocp);

LOAD CSV WITH HEADERS FROM "file:///lineyz.csv" AS lineyz
match (fromyz:Station{name:lineyz.sn}),(toyz:Station{name:lineyz.en})
merge (fromyz)-[ryz: e-town line {jl: lineyz jl, xl: lineyz. Xl}]->(toyz);

LOAD CSV WITH HEADERS FROM "file:///linedx.csv" AS linedx
match (fromdx:Station{name:linedx.sn}),(todx:Station{name:linedx.en})
merge (fromdx)-[RDX: daxing line {jl: linedx jl, xl: linedx. Xl}]->(todx);

LOAD CSV WITH HEADERS FROM "file:///linefs.csv" AS linefs
match (fromfs:Station{name:linefs.sn}),(tofs:Station{name:linefs.en})
merge (fromfs)-[RFS: well line {jl: linefs jl, xl: linefs. Xl}]->(tofs);

LOAD CSV WITH HEADERS FROM "file:///linejc.csv" AS linejc
match (fromjc:Station{name:linejc.sn}),(tojc:Station{name:linejc.en})
merge (fromjc)-[RJC: airport line {jl: linejc jl, xl: linejc. Xl}]->(tojc);
Copy the code

perform

Note: The Neo4j browser needs to enable multi-statement execution configuration; otherwise, a statement execution error will be reported. The configuration interface is as follows:

1. Configure the Neo4j browser

2. Cypher statement execution process

3. Subway network effect

4. Path retrieval

Take “Huoying” and “Beijing South Railway Station” subway stations as examples to retrieve specific routes:

Minimum site path

MATCH(p1: Station {name: "HuoYing}") and (p2: Station {name: "Beijing south Station"}), p=shortestpath((p1)-[*]-(p2)) RETURN p
Copy the code

Shortest path

MATCH p=(b: Station {name: "HuoYing"})-[*.20.]->(d:Station{name:" Beijing South Station "})WITH p,reduce(s = 0, r IN rels(p) | s + r.jl) AS dist return p ORDER BY dist DESC limit 1
Copy the code

Least time-consuming path

It is a pity that the specific time data between each station of Beijing subway was not found, so it cannot be retrieved here. Ok, I am too difficult.

Five, the summary

1) Data collation involves node data and node relation data, which can be created when importing CSV;

2) Cypher is a Neo4j database operation statement based on SQL statements;

3) (a)-[*..20]->(b) : indicates that the maximum path length is 20, the start node is A, and the end node is B;

4) To execute multiple statements at a time, Neo4j browser needs to enable multi-statement execution configuration;

over