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

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

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. AN INTRODUCTION TO PYTHON AND
    GRAPH DATABASES WITH NEO4J
    Holger Spill
    Kiwi PyCon – 14 September 2014

    View Slide

  2. http://lorenabarba.com/blog/

    View Slide

  3. A GRAPH
    Node
    Relationship

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  7. FROM SQL TO CYPHER

    View Slide

  8. 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

    View Slide

  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

    View Slide

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

    View Slide

  11. 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;

    View Slide

  12. 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;

    View Slide

  13. 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;

    View Slide

  14. 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;

    View Slide

  15. 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;

    View Slide

  16. 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;

    View Slide

  17. 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

    View Slide

  18. 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

    View Slide

  19. 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/

    View Slide

  20. READY, STEADY, GO START

    View Slide

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

    View Slide

  22. 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()

    View Slide

  23. … 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

    View Slide

  24. 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”
    }

    View Slide

  25. 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/

    View Slide

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

    View Slide

  27. View Slide