Building
social network with
Neo4j and Python
Andrii Soldatenko
3-4 July 2016
@a_soldatenko
Agenda:
• Who am I?
• 101 Graph data structure in Python
• neo4j overview
• PEP-249 and Neo4j
• other python neo4j clients
Andrii Soldatenko
• Backend Python Developer at
• CTO in Persollo.com
• Speaker at many PyCons and
Python meetups
• blogger at https://asoldatenko.com
Preface
Books
(Graphs)-[:ARE]->(Everywhere)
101 Graph data structure
graph = {'A': ['B', 'C'],
'B': ['C', 'D'],
'C': ['D'],
'D': ['C'],
'E': ['F'],
'F': ['C']}
from https://www.python.org/doc/essays/graphs/
BDFL
101 Graph data structure
def find_path(g, s, e, path=[]):
path = path + [s]
if s == e:
return path
if not s in g:
return None
for node in g[s]:
if node not in path:
newpath = find_path(g, node,
e, path)
if newpath: return newpath
return None
101 Graph data structure
>>> graph = {'A': ['B', 'C'],
'B': ['C', 'D'],
'C': ['D'],
'D': ['C'],
'E': ['F'],
'F': ['C']}
>>> find_path(graph, 'A', 'D')
['A', 'B', 'C', 'D']
>>>
Graph database
<id=234>

roles=Jummy Dugan
<id=12>

born=1971
name=“Halle Berry”
SQL vs NoSQL
Who is my boss?
Consistency
Availability
Cypher Query Language
MATCH (relationship, pattern matches)
WHERE (filter condition)
RETURN (what to return, nodes, rels,
properties)
Cypher: Example
MATCH (n:Person {name: "Tom Hanks”})
-[:ACTED_IN]->(m)
RETURN n,m
SELECT n,m
FROM persons;
Cypher: Example
Cypher: Example
╒══════════════════════╕
!tomHanksMovies.title !
╞══════════════════════╡
!Charlie Wilson's War !
"######################$
!The Polar Express !
"######################$
!A League of Their Own !
!Apollo 13 !
"######################$
!The Green Mile !
"######################$
Advanced Cypher
http://neo4j.com/docs/cypher-refcard/current/
Pros
Contra
- unable to handle lots of data
- when you want to update all or a subset of
entities
Python
Python
200 OK
https://github.com/nigelsmall/py2neo
Graph Data Types
Graph Databases
Batch & Manual Indexing
Calendar subgraph
neokit – Command Line Toolkit for Neo4
Object-Graph Mapping
PEP 249
Python Database API
Specification v2.0
https://www.python.org/dev/peps/pep-0249/
Python DB API 2.0
for Neo4j
https://github.com/jakewins/neo4jdb-python
class Connection(object):
def __init__(self, db_uri):
self.errorhandler = default_error_handler
self._host = urlparse(db_uri).netloc
self._http =
http.HTTPConnection(self._host)
self._tx = TX_ENDPOINT
self._messages = []
self._cursors = set()
self._cursor_ids = 0
neo4jdb-python
https://github.com/jakewins/neo4jdb-python
>>> from neo4j.contextmanager import
Neo4jDBConnectionManager
>>>
>>>
>>> neo_url = 'http://localhost:7474'
>>> manager = Neo4jDBConnectionManager(neo_url)
>>> with manager.write() as w:
... w.execute("CREATE (TheMatrix:Movie
{title:'The Matrix'})")
...
Object Graph Mapper
class FriendRel(StructuredRel):
since = DateTimeProperty(default=lambda:
datetime.now(pytz.utc))
met = StringProperty()
class Person(StructuredNode):
name = StringProperty()
friends = RelationshipTo('Person', 'FRIEND',
model=FriendRel)
rel = jim.friend.connect(bob)
rel.since # datetime object
(n)->[:Questions]
Thank You
@a_soldatenko
andrii.soldatenko@gmail.com
Building social network with Neo4j and Python

Building social network with Neo4j and Python