Slide 1

Slide 1 text

AN INTRODUCTION TO PYTHON AND GRAPH DATABASES WITH NEO4J Holger Spill Kiwi PyCon – 14 September 2014

Slide 2

Slide 2 text

http://lorenabarba.com/blog/

Slide 3

Slide 3 text

A GRAPH Node Relationship

Slide 4

Slide 4 text

https://wiki.cs.umd.edu/cmsc734_09/index.php David Bowie Defines Rock Royalty ANOTHER GRAPH

Slide 5

Slide 5 text

WHERE ARE GRAPHS USEFUL?  Social Network  Network Impact Analysis  Route Finding  Recommendations  Logistics  Access Control  Fraud Analysis  Securities and Debt  …

Slide 6

Slide 6 text

THE PROPERTIES GRAPH language:’Swedish’ open_source:true year:2007 by Nicole White - @_nicolemargaret

Slide 7

Slide 7 text

FROM SQL TO CYPHER

Slide 8

Slide 8 text

EXAMPLE: YOUTUBE IN SQL ID Name 1 Alice 2 Bob 3 Charles 4 David ID Name 1 Bob’s Gaming Channel 2 Bob’s Cute Dog 3 Cooking with Charles 4 David’s How-To Channel 5 Disco Dancing with David User ID Channel ID 2 1 2 2 3 3 4 4 4 5 USERS CHANNELS USERS_CHANNELS User ID Channel ID 1 3 1 4 2 3 2 5 3 1 USERS_SUBSCRIPTIONS

Slide 9

Slide 9 text

EXAMPLE: YOUTUBE IN A GRAPH name:’Alice’ name:’Bob’ name:’Charles’ name:’ David’ name:’Cooking with Charles’ name:’David’s How- To Channel’ name:’Disco Dancing with David’ name:’Bob’s Gaming Channel’ name:’Bob’s Cute Dog’ User Channel

Slide 10

Slide 10 text

CYPHER QUERY LANGUAGE .|'''', '|| | || || '|| ||` '||''|, ||''|, .|''|, '||''| || `|..|| || || || || ||..|| || `|....' || ||..|' .|| || `|... .||. , |' || '' .||

Slide 11

Slide 11 text

EXAMPLE: YOUTUBE IN A GRAPH name:’Alice’ name:’Bob’ name:’Charles’ name:’ David’ name:’Cooking with Charles’ name:’David’s How- To Channel’ name:’Disco Dancing with David’ name:’Bob’s Gaming Channel’ name:’Bob’s Cute Dog’ User Channel MATCH (c:Channel) RETURN c;

Slide 12

Slide 12 text

EXAMPLE: YOUTUBE IN A GRAPH name:’Alice’ name:’Bob’ name:’Charles’ name:’ David’ name:’Cooking with Charles’ name:’David’s How- To Channel’ name:’Disco Dancing with David’ name:’Bob’s Gaming Channel’ name:’Bob’s Cute Dog’ User Channel MATCH (u:User) RETURN u;

Slide 13

Slide 13 text

EXAMPLE: YOUTUBE IN A GRAPH name:’Alice’ name:’Bob’ name:’Charles’ name:’ David’ name:’Cooking with Charles’ name:’David’s How- To Channel’ name:’Disco Dancing with David’ name:’Bob’s Gaming Channel’ name:’Bob’s Cute Dog’ User Channel MATCH (bob:User {name:’Bob’}) RETURN bob;

Slide 14

Slide 14 text

EXAMPLE: YOUTUBE IN A GRAPH name:’Alice’ name:’Bob’ name:’Charles’ name:’ David’ name:’Cooking with Charles’ name:’David’s How- To Channel’ name:’Disco Dancing with David’ name:’Bob’s Gaming Channel’ name:’Bob’s Cute Dog’ User Channel MATCH (u:User)-[r:OPERATES]->(c:Channel) RETURN u,r,c;

Slide 15

Slide 15 text

EXAMPLE: YOUTUBE IN A GRAPH name:’Alice’ name:’Bob’ name:’Charles’ name:’ David’ name:’Cooking with Charles’ name:’David’s How- To Channel’ name:’Disco Dancing with David’ name:’Bob’s Gaming Channel’ name:’Bob’s Cute Dog’ User Channel MATCH p=(:User)-[:OPERATES]->(:Channel) RETURN p;

Slide 16

Slide 16 text

EXAMPLE: YOUTUBE IN A GRAPH name:’Alice’ name:’Bob’ name:’Charles’ name:’ David’ name:’Cooking with Charles’ name:’David’s How- To Channel’ name:’Disco Dancing with David’ name:’Bob’s Gaming Channel’ name:’Bob’s Cute Dog’ User Channel MATCH p=(:User {name:'Charles'})-[*1..4]-(:User {name:'David'}) RETURN p LIMIT 1;

Slide 17

Slide 17 text

CYPHER QUERY STRUCTURE MATCH (n:Label)-[:REL]->(m:Label) WHERE n.prop < 42 WITH n, count(m) as cnt, collect(m.attr) as attrs WHERE cnt > 12 RETURN n.prop, extract(a2 in filter(a1 in attrs WHERE a1 =~ "...-.*") | substr(a2,4,size(a2)-1)] AS ids ORDER BY length(ids) DESC LIMIT 10

Slide 18

Slide 18 text

PROS & CONS Strengths  Powerful data model, as general as RDBMS  Whiteboard friendly, flexible development  Fast, for connected data  Easy to query Weaknesses:  Sharding  Global Queries and Number Crunching  Binary Data / Blobs  Requires conceptual shift

Slide 19

Slide 19 text

READY, STEADY, GO START … or how to install Neo4j on Ubuntu  Start with a stock Ubuntu install  Install Oracle JDK7 (see askubuntu)  Download Neo4j Linux Community Edition  Follow the Neo4j installation instructions for Linux cd ; bin/neo4j start  Visit http://localhost:7474/browser/

Slide 20

Slide 20 text

READY, STEADY, GO START

Slide 21

Slide 21 text

MAKING FRIENDS www.neo4j.org www.python.org

Slide 22

Slide 22 text

PY2NEO  simple and pragmatic Python library  provides access via RESTful web services  no external dependencies  installation is straightforward  getting started is easy  actively maintained on GitHub by Nigel Small from py2neo import neo4j db_uri = "http://localhost:7474/db/data/" graph = neo4j.GraphDatabaseService(db_uri) neo4j.CypherQuery(graph, "Cypher goes here").run()

Slide 23

Slide 23 text

… AND ALTERNATIVES Object Graph Mapper (OGM) built on top of py2neo Neo4j Rest Client Object Graph Mapper (OGM) for Django. Use familiar Django models and queries against Neo4j. Python persistence framework for graph databases

Slide 24

Slide 24 text

REST API :GET /dbdata { "extensions": {}, "node": "http://localhost:7474/db/data/node", "node_index": "http://localhost:7474/db/data/index/node", […] "http://localhost:7474/db/data/schema/constraint", "transaction": http://localhost:7474/db/data/transaction", "node_labels": "http://localhost:7474/db/data/labels", "neo4j_version": "2.1.4” }

Slide 25

Slide 25 text

CURRENT RELEASE Neo4j 2.1.4 4 September, 2014 Community and Enterprise Edition  Import CSV Files with Cypher  Dense nodes support  New Cypher functionality  Official support for OpenJDK 7  ... and more http://neo4j.com/release-notes/neo4j-2-1-4/

Slide 26

Slide 26 text

JOIN THE CONVERSATION bit.ly/neo4j-google @neo4j #neo4j www.stackoverflow.com neo4j.meetup.com >>> Neo4j version, programming language, driver name, query language <<<

Slide 27

Slide 27 text

No content