Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Holger Spill: An introduction to Python and gra...

Holger Spill: An introduction to Python and graph databases with Neo4j

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Holger Spill:
An introduction to Python and graph databases with Neo4j
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
@ Kiwi PyCon 2014 - Sunday, 14 Sep 2014 - Track 2
http://kiwi.pycon.org/

**Audience level**

Novice

**Description**

A number of organisations have realised that the relational model for databases is insufficient for certain scenarios. Graph databases are good at modelling relationships. This talk will introduce you to Neo4j (a graph database engine) with Python. How to set it up, how to use it and some questions you can answer with it.

**Abstract**

This talk will introduce you to Neo4j (a graph database engine) with Python. How to set it up, how to use it and some questions you can answer with it.

You don’t need any previous experience with Neo4j or NoSQL databases and very little experience with Python.

We'll cover: Introduction to Graphs Setting up your toolbox Neo4j and Cypher A simple real-world example use case with Neo4j

**YouTube**

https://www.youtube.com/watch?v=ps5RtmWKwLY

New Zealand Python User Group

September 14, 2014
Tweet

More Decks by New Zealand Python User Group

Other Decks in Programming

Transcript

  1. WHERE ARE GRAPHS USEFUL?  Social Network  Network Impact

    Analysis  Route Finding  Recommendations  Logistics  Access Control  Fraud Analysis  Securities and Debt  …
  2. 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
  3. 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
  4. CYPHER QUERY LANGUAGE .|'''', '|| | || || '|| ||`

    '||''|, ||''|, .|''|, '||''| || `|..|| || || || || ||..|| || `|....' || ||..|' .|| || `|... .||. , |' || '' .||
  5. 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;
  6. 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;
  7. 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;
  8. 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;
  9. 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;
  10. 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;
  11. 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
  12. 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
  13. 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 <dest>; bin/neo4j start  Visit http://localhost:7474/browser/
  14. 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()
  15. … 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
  16. 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” }
  17. 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/