Skip to content

Commit 256f0e6

Browse files
committed
Load bidirectional edges to the DB
1 parent 77591aa commit 256f0e6

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

import_data.py

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import os
22
from neo4j import GraphDatabase
33
import json
4+
import argparse
5+
from dotenv import load_dotenv
6+
import os
7+
48

5-
def import_tube_data(uri, username, password, data_file):
9+
def import_tube_data(uri, username, password, data_file, undirected=False):
610
driver = GraphDatabase.driver(uri, auth=(username, password))
711

812
# Load JSON data
@@ -37,27 +41,55 @@ def import_tube_data(uri, username, password, data_file):
3741
s.rail = toInteger(station.rail)
3842
""", {'stations': data['stations']})
3943

40-
session.run("""
41-
UNWIND $connections AS conn
42-
MATCH (s1:UndergroundStation {id: conn.station1})
43-
MATCH (s2:UndergroundStation {id: conn.station2})
44-
MERGE (s1)-[r:LINK {
45-
line: conn.line,
46-
time: toInteger(conn.time),
47-
distance: toInteger(conn.time)
48-
}]->(s2)
49-
""", {'connections': data['connections']})
44+
if undirected:
45+
# Create bidirectional relationships for undirected graph
46+
session.run("""
47+
UNWIND $connections AS conn
48+
MATCH (s1:UndergroundStation {id: conn.station1})
49+
MATCH (s2:UndergroundStation {id: conn.station2})
50+
MERGE (s1)-[r1:LINK {
51+
line: conn.line,
52+
time: toInteger(conn.time),
53+
distance: toInteger(conn.time)
54+
}]->(s2)
55+
MERGE (s2)-[r2:LINK {
56+
line: conn.line,
57+
time: toInteger(conn.time),
58+
distance: toInteger(conn.time)
59+
}]->(s1)
60+
""", {'connections': data['connections']})
61+
else:
62+
# Create directed relationships (default behavior)
63+
session.run("""
64+
UNWIND $connections AS conn
65+
MATCH (s1:UndergroundStation {id: conn.station1})
66+
MATCH (s2:UndergroundStation {id: conn.station2})
67+
MERGE (s1)-[r:LINK {
68+
line: conn.line,
69+
time: toInteger(conn.time),
70+
distance: toInteger(conn.time)
71+
}]->(s2)
72+
""", {'connections': data['connections']})
5073

5174
driver.close()
5275

53-
# Usage
54-
from dotenv import load_dotenv
55-
import os
56-
load_dotenv('.env')
57-
58-
uri = os.environ["NEO4J_URI"]
59-
username = os.environ["NEO4J_USERNAME"]
60-
password = os.environ["NEO4J_PASSWORD"]
61-
data_file = "dataset/london.json"
76+
def main():
77+
# Parse command line arguments
78+
parser = argparse.ArgumentParser(description='Import London Underground data into Neo4j')
79+
parser.add_argument('--undirected', action='store_true',
80+
help='Load the graph as undirected (creates bidirectional relationships)')
81+
args = parser.parse_args()
82+
83+
load_dotenv('.env')
84+
85+
uri = os.environ["NEO4J_URI"]
86+
username = os.environ["NEO4J_USERNAME"]
87+
password = os.environ["NEO4J_PASSWORD"]
88+
data_file = "dataset/london.json"
89+
90+
print(f"Loading graph as {'undirected' if args.undirected else 'directed'}...")
91+
import_tube_data(uri, username, password, data_file, undirected=args.undirected)
92+
print("Import completed successfully!")
6293

63-
import_tube_data(uri, username, password, data_file)
94+
if __name__ == "__main__":
95+
main()

0 commit comments

Comments
 (0)