-
Notifications
You must be signed in to change notification settings - Fork 8
/
migrate_1_mailing_lists_people.py
67 lines (47 loc) · 2 KB
/
migrate_1_mailing_lists_people.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'''
MIGRATION 1:
Table migrated: "mailing_lists_people"
This migration
--------------
* Creates a node for each mailing list with label `MList`
* Creates a node for each user in the mailing list with label `:MUser`
* Associates the user with the mailing list. `:BELONGS_TO`
* Does not add extra information to each node. (Done by other migrations)
NOTE
----
The table migrated was a many-to-many connecting table so it does not contain
property information of each node. That will be in the other tables and the
node data can be merged from those tables.
'''
# Import MySQL-python conncetor
import _mysql
# Import py2neo (python connector for Neo4j)
from py2neo import Graph
db=_mysql.connect()
db = raw_input("Enter Database Name (Default: AmarokDB)") or "AmarokDB"
host = raw_input("Enter Host Name (Default: localhost)") or "localhost"
user = raw_input("Enter Username (Default: root)") or "root"
password = raw_input("Enter password (Default: <blank>)") or ""
db=_mysql.connect(host=host ,user=user, passwd=password, db=db)
db.query("""SELECT * FROM mailing_lists_people""")
result = db.use_result()
# Name of fields in the MySQL database.
fields = [e[0] for e in result.describe()]
# Returns all the rows in the MySQL table.
rows = result.fetch_row(maxrows=0)
# In our case the data has 2 fields.
# (email_address, mailing_list_url)
# Connect to running neo4j graph database.
n4j_graph = Graph("http://localhost:7474/db/data/")
# Creating indexes on fields that would be used frequently for filtering.
n4j_graph.cypher.execute("CREATE INDEX ON :MUser(email)")
n4j_graph.cypher.execute("CREATE INDEX ON :MList(mailing_list_url)")
ctr = 0
for row in rows:
print row
email = row[0]
mailing_list_url = row[1]
s = '''MERGE (u:MUser { %(primary_key_field)s: "%(email)s"})
MERGE (m:MList { mailing_list_url: "%(mailing_list_url)s"})
CREATE UNIQUE u-[:BELONGS_TO]->m'''%{"primary_key_field": fields[0], "email": email, "mailing_list_url": mailing_list_url }
n4j_graph.cypher.execute(s)