INTRODUCTION TO GRAPH DATABASES GRAPHS NOT CHARTS MOTIVATING
INTRODUCTION TO GRAPH DATABASES
GRAPHS … NOT CHARTS
MOTIVATING EXAMPLE Courtesy of: “Informal Networks: The Company Behind the Chart, ” Krackhardt and Hanson, Harvard Business Review 1993
ALTERNATIVE: ADVICE NETWORK
ALTERNATIVE: TRUST NETWORK
FUNDAMENTAL GRAPH MODELING ELEMENTS Nodes Represent Entities Can have any number of properties Can be queried according to properties Relationships Represent any kind of relationship Also support any number of properties Usually directed Properties Either nodes or relationships can have properties
MOTIVATION Social Networks People Employees Positions Firms Fraud Analysis People Accounts Clinics Claims Physicians Recipients Salesperson Deal Product Customer Recommendation Engines Dynamic, not batched operations People Products/Videos/Books
EASE OF USE/PERFORMANCE TIME Stories of order of magnitude improvements due to far fewer joins Thought experiment: Friends of Friends
FRIENDS OF FRIENDS EXAMPLE ALICE BOB CARLY DAVE EVELY N
CREATE FRIENDS NETWORK CREATE (person 1: Person {name: 'Alice', attitude: 'Friendly'}), (person 2: Person {name: 'Bob', attitude: 'Scary'}), (person 3: Person {name: 'Carly', attitude: 'Friendly'}), (person 4: Person {name: 'Dave', attitude: 'Reticent'}), (person 5: Person {name: 'Evelyn', attitude: 'Scary'}), (person 1)-[: FRIEND_OF]->(person 2), (person 2)-[: FRIEND_OF]->(person 1), (person 2)-[: FRIEND_OF]->(person 3), (person 3)-[: FRIEND_OF]->(person 2), (person 3)-[: FRIEND_OF]->(person 4), (person 4)-[: FRIEND_OF]->(person 3), (person 4)-[: FRIEND_OF]->(person 5), (person 5)-[: FRIEND_OF]->(person 4), ;
SHOW ALL NODES MATCH (n) RETURN n;
FRIENDS OF EVELYN MATCH (n)-[: FRIEND_OF]->(person: Person) WHERE person. name = 'Evelyn' return n, person;
MATCH FRIEND OF A FRIEND FOR EVELYN MATCH (n 1)-[: FRIEND_OF]->(n 2)-[: FRIEND_OF]>(person: Person) WHERE person. name = 'Evelyn' return n 1, n 2, person;
UNDERLYING TECHNOLOGY No. SQL Variant – Fundamentally a key-value store Cluster-based high availability
USABILITY ADVANTAGES “The whiteboard model is the physical model” Additive development of data model (add properties, node types, etc) By default, no schema (In Neo 4 j at least) Discussion Question: No schema, advantage or disadvantage?
CYPHER QUERY LANGUAGE “ASCII ART” ( ) indicates node -[]-> indicates relationship Supports CREATE, MATCH, shortest. Path() MATCH assigns variables to nodes and relationships based on pattern matching Can return nodes, relationships, or properties Can execute functions
CYPHER EXAMPLE MATCH (n 1)-[: FRIEND_OF]->(n 2)-[: FRIEND_OF]->(person: Person) WHERE person. name = 'Evelyn' RETURN n 1, n 2, person;
DELETE ALL NODES AND ALL RELATIONSHIPS match (n) OPTIONAL MATCH (n)-[r]-() DELETE n, r;
CREATE A NEW NETWORK WITH A DIFFERENT TYPE OF NODE CREATE (person 1: Person {name: 'Alice', attitude: 'Friendly'}), (person 2: Person {name: 'Bob', attitude: 'Scary'}), (person 3: Person {name: 'Carly', attitude: 'Friendly'}), (person 4: Person {name: 'Dave', attitude: 'Reticent'}), (person 5: Person {name: 'Evelyn', attitude: 'Scary'}), (person 1)-[: FRIEND_OF]->(person 2), (person 2)-[: FRIEND_OF]->(person 1), (person 2)-[: FRIEND_OF]->(person 3), (person 3)-[: FRIEND_OF]->(person 2), (person 3)-[: FRIEND_OF]->(person 4), (person 4)-[: FRIEND_OF]->(person 3), (person 4)-[: FRIEND_OF]->(person 5), (person 5)-[: FRIEND_OF]->(person 4), (company 1: Company {Name: 'Enron'}), (company 2: Company {Name: 'AT&T'}), (person 1) -[: WORKS_AT {tenure: 7}]->(company 1), (person 2) -[: WORKS_AT {tenure: 12}]->(company 2),
EXPLORE THE NEW NETWORK MATCH (p 1: Person {name: "Evelyn"}), (c 1: Company {Name: "Enron"}), p=shortest. Path((p 1)-[*]-(c 1)) RETURN p MATCH (p: Person {attitude: 'Friendly'}) RETURN p MATCH (p: Person {attitude: 'Scary'}) RETURN p MATCH (n) -[rel: WORKS_AT]->(c: Company {Name: "AT&T"}) RETURN n, c; MATCH (n) -[rel: WORKS_AT]->(c: Company {Name: "AT&T"}) RETURN AVG(rel. tenure)
POINTS TO CONSIDER 1. When is a relational DB better? 2. What are the advantages and disadvantages of other systems, such as Orient. DB? 3. How to make relational DBs and Graph DBs play together 1. For example, implement hierarchy in graph and store employee information in relational tables?
- Slides: 21