Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
×

# Building social network with Neo4j and Python

Social phenomena is coming. We have lot’s of social applications that we are using every day, let’s say Facebook, twitter, Instagram. Lot’s of such kind apps based on social graph and graph theory. I would like to share my knowledge and expertise about how to work with graphs and build large social graph as engine for Social network using python and Graph databases. We'll compare SQL and NoSQL approaches for friends relationships.

• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here

• Be the first to like this

### Building social network with Neo4j and Python

1. 1. Building social network with Neo4j and Python Andrii Soldatenko 3-4 July 2016 @a_soldatenko
2. 2. Agenda: • Who am I? • 101 Graph data structure in Python • neo4j overview • PEP-249 and Neo4j • other python neo4j clients
3. 3. Andrii Soldatenko • Backend Python Developer at • CTO in Persollo.com • Speaker at many PyCons and Python meetups • blogger at https://asoldatenko.com
4. 4. Preface
5. 5. Books
6. 6. (Graphs)-[:ARE]->(Everywhere)
7. 7. 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/
8. 8. BDFL
9. 9. 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
10. 10. 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'] >>>
11. 11. Graph database <id=234>  roles=Jummy Dugan <id=12>  born=1971 name=“Halle Berry”
12. 12. SQL vs NoSQL
13. 13. Who is my boss?
14. 14. Consistency
15. 15. Availability
16. 16. Cypher Query Language MATCH (relationship, pattern matches) WHERE (filter condition) RETURN (what to return, nodes, rels, properties)
17. 17. Cypher: Example MATCH (n:Person {name: "Tom Hanks”}) -[:ACTED_IN]->(m) RETURN n,m SELECT n,m FROM persons;
18. 18. Cypher: Example
19. 19. Cypher: Example ╒══════════════════════╕ !tomHanksMovies.title ! ╞══════════════════════╡ !Charlie Wilson's War ! "######################\$ !The Polar Express ! "######################\$ !A League of Their Own ! !Apollo 13 ! "######################\$ !The Green Mile ! "######################\$
20. 20. Advanced Cypher http://neo4j.com/docs/cypher-refcard/current/
21. 21. Pros
22. 22. Contra - unable to handle lots of data - when you want to update all or a subset of entities
23. 23. Python
24. 24. Python 200 OK
25. 25. https://github.com/nigelsmall/py2neo Graph Data Types Graph Databases Batch & Manual Indexing Calendar subgraph neokit – Command Line Toolkit for Neo4 Object-Graph Mapping
26. 26. PEP 249 Python Database API Specification v2.0 https://www.python.org/dev/peps/pep-0249/
27. 27. 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
28. 28. 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'})") ...
29. 29. Object Graph Mapper
30. 30. 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
31. 31. (n)->[:Questions]
32. 32. Thank You @a_soldatenko andrii.soldatenko@gmail.com