100% found this document useful (1 vote)
72 views

Graph Databases Ian Robinson 2024 scribd download

Graph

Uploaded by

ismaelxez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
72 views

Graph Databases Ian Robinson 2024 scribd download

Graph

Uploaded by

ismaelxez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Download Full ebookname - Read Now at ebookname.

com

Graph Databases Ian Robinson

https://ebookname.com/product/graph-databases-ian-robinson/

OR CLICK BUTTON

DOWLOAD EBOOK

Discover More Ebook - Explore Now at ebookname.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Genomes Browsers and Databases Data Mining Tools for


Integrated Genomic Databases 1st Edition Peter Schattner

https://ebookname.com/product/genomes-browsers-and-databases-data-
mining-tools-for-integrated-genomic-databases-1st-edition-peter-
schattner/
ebookname.com

Graph Algorithms 2nd Edition Shimon Even

https://ebookname.com/product/graph-algorithms-2nd-edition-shimon-
even-2/

ebookname.com

Graph Algorithms 2nd Edition Shimon Even

https://ebookname.com/product/graph-algorithms-2nd-edition-shimon-
even/

ebookname.com

A Passion to Preserve Gay Men as Keepers of Culture 1st


Edition Will Fellows

https://ebookname.com/product/a-passion-to-preserve-gay-men-as-
keepers-of-culture-1st-edition-will-fellows/

ebookname.com
Fira and the Full Moon First Edition Gail Herman

https://ebookname.com/product/fira-and-the-full-moon-first-edition-
gail-herman/

ebookname.com

Open source software in life science research Practical


solutions to common challenges in the pharmaceutical
industry and beyond 1st Edition Lee Harland
https://ebookname.com/product/open-source-software-in-life-science-
research-practical-solutions-to-common-challenges-in-the-
pharmaceutical-industry-and-beyond-1st-edition-lee-harland/
ebookname.com

Tendon Transfers in Reconstructive Hand Surgery 1st


Edition Jan Friden

https://ebookname.com/product/tendon-transfers-in-reconstructive-hand-
surgery-1st-edition-jan-friden/

ebookname.com

The 5 Second Rule 1st Edition Mel Robbins

https://ebookname.com/product/the-5-second-rule-1st-edition-mel-
robbins/

ebookname.com

Disgust and its disorders theory assessment and treatment


implications 1st ed Edition American Psychological
Association.
https://ebookname.com/product/disgust-and-its-disorders-theory-
assessment-and-treatment-implications-1st-ed-edition-american-
psychological-association/
ebookname.com
Minerva s Night Out Philosophy Pop Culture and Moving
Pictures 1st Edition Noël Carroll

https://ebookname.com/product/minerva-s-night-out-philosophy-pop-
culture-and-moving-pictures-1st-edition-noel-carroll/

ebookname.com
Graph Databases

Ian Robinson, Jim Webber, and Emil Eifrem


Graph Databases
by Ian Robinson, Jim Webber, and Emil Eifrem
Copyright © 2013 Neo Technology, Inc.. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com.
Editors: Mike Loukides and Nathan Jepson Indexer: FIX ME!
Production Editor: Kara Ebrahim Cover Designer: Karen Montgomery
Copyeditor: FIX ME! Interior Designer: David Futato
Proofreader: FIX ME! Illustrator: Robert Romano

June 2013: First Edition

Revision History for the First Edition:


2013-04-11: Early release revision 1

See http://oreilly.com/catalog/errata.csp?isbn=9781449356262 for release details.

Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc. !!FILL THIS IN!! and related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐
mark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.

ISBN: 978-1-449-35626-2
[?]
Table of Contents

Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii

1. Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
About This Book 2
What is a Graph? 2
A High Level View of the Graph Space 5
Graph Databases 6
Graph Compute Engines 8
The Power of Graph Databases 10
Performance 10
Flexibility 10
Agility 11
Summary 11

2. Options for Storing Connected Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13


Relational Databases Lack Relationships 13
NOSQL Databases Also Lack Relationships 16
Graph Databases Embrace Relationships 19
Summary 23

3. Data Modeling with Graphs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25


Models and Goals 25
The Property Graph Model 26
Querying Graphs: An Introduction to Cypher 27
Cypher Philosophy 27
START 29
MATCH 29
RETURN 30
Other Cypher clauses 30

iii
A Comparison of Relational and Graph Modeling 30
Relational Modeling in a Systems Management Domain 31
Graph Modeling in a Systems Management Domain 34
Testing the Model 36
Cross-Domain Models 37
Creating the Shakespeare Graph 40
Beginning a Query 42
Declaring Information Patterns to Find 42
Constraining Matches 44
Processing Results 45
Query Chaining 46
Common Modeling Pitfalls 46
Email Provenance Problem Domain 47
A Sensible First Iteration? 47
Second Time’s the Charm 49
Evolving the Domain 51
Avoiding Anti-Patterns 54
Summary 55

4. Building a Graph Database Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57


Data Modeling 57
Describe the Model in Terms of Your Application’s Needs 57
Nodes for Things, Relationships for Structure 59
Fine-Grained Versus Generic Relationships 59
Model Facts as Nodes 60
Represent Complex Value Types as Nodes 64
Time 64
Iterative and Incremental Development 67
Application Architecture 68
Embedded Versus Server 68
Clustering 73
Load Balancing 74
Testing 76
Test-Driven Data Model Development 76
Performance Testing 82
Capacity Planning 86
Optimization Criteria 87
Performance 87
Redundancy 90
Load 90

5. Graphs in the Real World. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

iv | Table of Contents
Why Organizations Choose Graph Databases 93
Common Use Cases 94
Social 94
Recommendations 95
Geo 96
Master Data Management 96
Network and Data Center Management 97
Authorization and Access Control (Communications) 98
Real-World Examples 99
Social Recommendations (Professional Social Network) 99
Authorization and Access Control 107
Geo (Logistics) 113

6. Graph Database Internals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125


Native Graph Processing 125
Native Graph Storage 128
Programmatic APIs 135
Kernel API 136
Core (or “Beans”) API 136
Traversal API 138
Non-Functional Characteristics 139
Transactions 140
Recoverability 141
Availability 142
Scale 144
Summary 147

7. Predictive Analysis with Graph Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149


Depth- and Breadth-First Search 149
Path-Finding with Dijkstra’s Algorithm 150
The A* Algorithm 157
Graph Theory and Predictive Modeling 158
Triadic Closures 159
Structural Balance 160
Local Bridges 163
Summary 165

A. NOSQL Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189

Table of Contents | v
Preface

Conventions Used in This Book


The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements
such as variable or function names, databases, data types, environment variables,
statements, and keywords.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by values deter‐
mined by context.

This icon signifies a tip, suggestion, or general note.

This icon indicates a warning or caution.

Using Code Examples


This book is here to help you get your job done. In general, if this book includes code
examples, you may use the code in this book in your programs and documentation. You

vii
do not need to contact us for permission unless you’re reproducing a significant portion
of the code. For example, writing a program that uses several chunks of code from this
book does not require permission. Selling or distributing a CD-ROM of examples from
O’Reilly books does require permission. Answering a question by citing this book and
quoting example code does not require permission. Incorporating a significant amount
of example code from this book into your product’s documentation does require per‐
mission.
We appreciate, but do not require, attribution. An attribution usually includes the title,
author, publisher, and ISBN. For example: “Book Title by Some Author (O’Reilly).
Copyright 2012 Some Copyright Holder, 978-0-596-xxxx-x.”
If you feel your use of code examples falls outside fair use or the permission given above,
feel free to contact us at permissions@oreilly.com.

Safari® Books Online


Safari Books Online is an on-demand digital library that delivers ex‐
pert content in both book and video form from the world’s leading
authors in technology and business.
Technology professionals, software developers, web designers, and business and crea‐
tive professionals use Safari Books Online as their primary resource for research, prob‐
lem solving, learning, and certification training.
Safari Books Online offers a range of product mixes and pricing programs for organi‐
zations, government agencies, and individuals. Subscribers have access to thousands of
books, training videos, and prepublication manuscripts in one fully searchable database
from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Pro‐
fessional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John
Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT
Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technol‐
ogy, and dozens more. For more information about Safari Books Online, please visit us
online.

How to Contact Us
Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.


1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)

viii | Preface
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional
information. You can access this page at http://www.oreilly.com/catalog/<catalog page>.
To comment or ask technical questions about this book, send email to bookques
tions@oreilly.com.
For more information about our books, courses, conferences, and news, see our website
at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

Preface | ix
CHAPTER 1
Introduction

Graph databases address one of the great macroscopic business trends of today: lever‐
aging complex and dynamic relationships in highly-connected data to generate insight
and competitive advantage. Whether we want to understand relationships between
customers, elements in a telephone or datacenter network, entertainment producers
and consumers, or genes and proteins, the ability to understand and analyze vast graphs
of highly-connected data will be key in determining which companies outperform their
competitors over the coming decade.
For data of any significant size or value, graph databases are the best way to represent
and query connected data. Connected data is data whose interpretation and value re‐
quires us first to understand the ways in which its constituent elements are related. More
often than not, to generate this understanding, we need to name and qualify the con‐
nections between things.
While large corporates realized this some time ago, creating their own proprietary graph
processing technologies, we’re now in an era where that technology has rapidly become
democratized. Today, general-purpose graph databases are a reality, allowing main‐
stream users to experience the benefits of connected data without having to invest in
building their own graph infrastructure.
What’s remarkable about this renaissance of graph data and graph thinking is that graph
theory itself is not new. Graph theory was pioneered by Euler in the 18th century, and
has been actively researched and improved by mathematicians, sociologists, anthro‐
pologists, and others ever since. However, it is only in the last few years that graph theory
and graph thinking have been applied to information management. In that time, graph
databases have helped solve important problems in the areas of social networking, mas‐
ter data management, geospatial, recommendations, and more. This increased focus on
graph is driven by twin forces: by the massive commercial successes of companies such
as Facebook, Google, and Twitter, all of whom have centered their business models

1
around their own proprietary graph technologies; and by the introduction of general
purpose graph databases into the technology landscape.

Graphs are everywhere


Graphs are extremely useful in understanding a wide diversity of datasets in fields such
as science, government and business. The real world—unlike the forms-based model
behind the relational database—is rich and interrelated: uniform and rule-bound in
parts, exceptional and irregular in others. Once we understand graphs, we begin to see
them in all sorts of places. Gartner, for example, identifies five graphs in the world of
business—social, intent, consumption, interest, and mobile—and says that the ability
to leverage these graphs provides a “sustainable competitive advantage.”1

About This Book


The purpose of this book is to introduce graphs and graph databases to technology
practitioners, including developers, database professionals, and technology decision
makers. Reading this book, you will come away with a practical understanding of graph
databases. We show how the graph model “shapes” data, and how we query, reason
about, understand and act upon data using a graph database. We discuss the kinds of
problems that are well aligned with graph databases, with examples drawn from actual
real-world use cases. And we show how to plan and implement a graph database solu‐
tion.
While much of this book talks about graph data models, it is not a book about graph
theory.2 We don’t need much theory to take advantage of graph databases: provided we
understand what a graph is, we’re practically there. With that in mind, let’s refresh our
memories about graphs in general.

What is a Graph?
Formally a graph is just a collection of vertices and edges--or, in less intimidating lan‐
guage, a set of nodes and the relationships that connect them. Graphs represent entities
as nodes and the ways in which those entities relate to the world as relationships. This
general-purpose, expressive structure allows us to model all kinds of scenarios, from

1. http://www.gartner.com/id=2081316
2. For introductions to graph theory, see Richard J. Trudeau, Introduction To Graph Theory (Dover, 1993) and
Gary Chartrand, Introductory Graph Theory (Dover, 1985). For an excellent introduction to how graphs
provide insight into complex events and behaviors, see David Easley and Jon Kleinberg, Networks, Crowds,
and Markets: Reasoning about a Highly Connected World (Cambridge University Press, 2010)

2 | Chapter 1: Introduction
the construction of a space rocket, to a system of roads, and from the supply-chain or
provenance of foodstuff, to medical history for populations, and beyond.
For example, Twitter’s data is easily represented as a graph. In Figure 1-1 we see a small
network of followers. The relationships are key here in establishing the semantic context:
namely, that Billy follows Harry, and that Harry, in turn, follows Billy. Ruth and Harry
likewise follow each other, but sadly, while Ruth follows Billy, Billy hasn’t (yet) recip‐
rocated.

Figure 1-1. A small social graph

Of course, Twitter’s real graph is hundreds of millions of times larger than the example
in Figure 1-1, but it works on precisely the same principles. In Figure 1-2 we’ve expanded
the graph to include the messages published by Ruth.

What is a Graph? | 3
Figure 1-2. Publishing messages

Though simple, Figure 1-2 shows the expressive power of the graph model. It’s easy to
see that Ruth has published a string of messages. The most recent message can be found

4 | Chapter 1: Introduction
by following a relationship marked CURRENT; PREVIOUS relationships then create a time‐
line of posts.

The property graph model


In discussing Figure 1-2 we’ve also informally introduced the most popular variant of
graph model, the property graph.3 A property graph has the following characteristics:

• It contains nodes and relationships


• Nodes contain properties (key-value pairs)
• Relationships are named and directed, and always have a start and end node
• Relationships can also contain properties

Most people find the property graph model intuitive and easy to understand. While
simple, it can be used to describe the overwhelming majority of graph use cases in ways
that yield useful insights into our data.

A High Level View of the Graph Space


Numerous projects and products for managing, processing and analyzing graphs have
exploded onto the scene in recent years. The sheer number of technologies makes it
difficult to keep track of these tools and how they differ, even for those of us who are
active in the space. This section provides a high level framework for making sense of
the emerging graph landscape.
From 10,000 feet we can divide the graph space into two parts:

1. Technologies used primarily for transactional online graph persistence, typically


accessed directly in real-time from an application. These technologies are called
graph databases and are the main focus of this book. They are the equivalent of
“normal” OLTP database in the relational world.
2. Technologies used primarily for offline graph analytics, typically performed as a
series of batch steps. These technologies can be called graph compute engines. They
can be thought of as being in the same category as other technologies for analysis
of data in bulk, such as data mining and OLAP.

3. In the appendix, we discuss alternative graph data models in more detail.

A High Level View of the Graph Space | 5


Another way to slice the graph space is to look at the graph models
employed by the various technologies. There are three dominant graph
data models: the property graph, RDF triples and hypergraphs. We de‐
scribe these in detail in Appendix A. Most of the popular graph data‐
bases on the market use the property graph model, and in consequence,
it’s the model we’ll use throughout the remainder of this book.

Graph Databases
A graph database management system (henceforth, a graph database) is an online da‐
tabase management system with Create, Read, Update and Delete methods that expose
a graph data model. Graph databases are generally built for use with transactional
(OLTP) systems. Accordingly, they are normally optimized for transactional perfor‐
mance, and engineered with transactional integrity and operational availability in mind.
There are two properties of graph databases you should consider when investigating
graph database technologies:

1. The underlying storage. Some graph databases use native graph storage that is op‐
timized and designed for storing and managing graphs. Not all graph database
technologies use native graph storage however. Some serialize the graph data into
a relational database, an object-oriented database, or some other general-purpose
data store.
2. The processing engine. Some definitions require that a graph database use index-
free adjacency, meaning that connected nodes physically “point” to each other in
the database.4 Here we take a slightly broader view: any database that from the user’s
perspective behaves like a graph database, i.e. exposes a graph data model through
CRUD operations, qualifies as a graph database. We do acknowledge however the
significant performance advantages of index-free adjacency, and therefore use the
term native graph processing to describe graph databases that leverage index-free
adjacency.

4. See Rodriguez, M.A., Neubauer, P., “The Graph Traversal Pattern,” 2010 (http://arxiv.org/abs/1004.1001)

6 | Chapter 1: Introduction
It’s important to note that native graph storage and native graph pro‐
cessing are neither good nor bad—they’re simply classic engineering
tradeoffs. The benefit of native graph storage is that its purpose-built
stack is engineered for performance and scalability. The benefit of non-
native graph storage, in contrast, is that it typically depends on a mature
non-graph backend (such as MySQL) whose production characteristics
are well understood by operations teams. Native graph processing
(index-free adjacency) benefits traversal performance, but at the ex‐
pense of making some non-traversal queries difficult or memory in‐
tensive.

Relationships are first-class citizens of the graph data model, unlike other database
management systems, which require us to infer connections between entities using
contrived properties such as foreign keys, or out-of-band processing like map-reduce.
By assembling the simple abstractions of nodes and relationships into connected struc‐
tures, graph databases allow us to build arbitrarily sophisticated models that map closely
to our problem domain. The resulting models are simpler and at the same time more
expressive than those produced using traditional relational databases and the other
NOSQL stores.
Figure 1-3 shows a pictorial overview of some of the graph databases on the market
today based on their storage and processing models:

A High Level View of the Graph Space | 7


Figure 1-3. An overview of the graph database space

Graph Compute Engines


A graph compute engine is a technology that enables global graph computational algo‐
rithms to be run against large data sets. Graph compute engines are designed to do things
like identify clusters in your data, or answer questions such as, “how many relationships,
on average, does everyone in a social network have?”
Because of their emphasis on global queries, graph compute engines are normally op‐
timized for scanning and processing large amounts of information in batch, and in that
respect they are similar to other batch analysis technologies, such as data mining and
OLAP, familiar in the relational world. While some graph compute engines include a
graph storage layer, others (and arguably most) concern themselves strictly with pro‐
cessing data that is fed in from an external source, and returning the results.

8 | Chapter 1: Introduction
Figure 1-4. A high level view of a typical graph compute engine deployment

A variety of different types of graph compute engines exist. Most notably there are in-
memory/single machine graph compute engines like Cassovary, and distributed graph
compute engines like Pegasus or Giraph. Most distributed graph compute engines are
based on the Pregel white paper, authored by Google, which describes the graph com‐
pute engine Google uses to rank pages.5

This book focuses on graph databases


The previous section provided a course-grained overview of the entire graph space. The
rest of this book focuses on graph databases. Our goal throughout is to describe graph
database concepts. Where appropriate, we illustrate these concepts with examples drawn
from our experience of developing solutions using the property graph model and the
Neo4j database. Irrespective of the graph model or database used for the examples,
however, the important concepts carry over to other graph databases.

5. Cassovary: https://github.com/twitter/cassovary. Pegasus: http://www.cs.cmu.edu/~pegasus/. Giraphe: http://


incubator.apache.org/giraph/. Pregel: http://dl.acm.org/citation.cfm?id=1807184.

A High Level View of the Graph Space | 9


The Power of Graph Databases
Notwithstanding the fact that just about anything can be modeled as a graph, we live in
a pragmatic world of budgets, project timelines, corporate standards and commoditized
skill-sets. That a graph database provides a powerful but novel data modeling technique
does not in itself provide sufficient justification for replacing a well-established, well-
understood data platform; there must also be an immediate and very significant practical
benefit. In the case of graph databases, this motivation exists in the form of a set of use
cases and data patterns whose performance improves by one or more orders of mag‐
nitude when implemented in a graph, and whose latency is much lower compared to
batch processing of aggregates. On top of this performance benefit, graph databases
offer an extremely flexible data model, and a mode of delivery aligned with today’s agile
software delivery practices.

Performance
One compelling reason, then, for choosing a graph database is the sheer performance
increase when dealing with connected data versus relational databases and NOSQL
stores. In contrast to relational databases, where join-intensive query performance de‐
teriorates as the dataset gets bigger, with a graph database performance tends to remain
relatively constant, even as the dataset grows. This is because queries are localized to a
portion of the graph. As a result, the execution time for each query is proportional only
to the size of the part of the graph traversed to satisfy that query, rather than the size of
the overall graph.

Flexibility
As developers and data architects we want to connect data as the domain dictates,
thereby allowing structure and schema to emerge in tandem with our growing under‐
standing of the problem space, rather than being imposed upfront, when we know least
about the real shape and intricacies of the data. Graph databases address this want
directly. As we show in Chapter 3, the graph data model expresses and accommodates
the business’ needs in a way that enables IT to move at the speed of business.
Graphs are naturally additive, meaning we can add new kinds of relationships, new
nodes, and new subgraphs to an existing structure without disturbing existing queries
and application functionality. These things have generally positive implications for de‐
veloper productivity and project risk. Because of the graph model’s flexibility, we don’t
have to model our domain in exhaustive detail ahead of time—a practice which is all
but foolhardy in the face of changing business requirements. The additive nature of
graphs also means we tend to perform fewer migrations, thereby reducing maintenance
overhead and risk.

10 | Chapter 1: Introduction
Agility
We want to be able to evolve our data model in step with the rest of our application,
using a technology aligned with today’s incremental and iterative software delivery
practices. Modern graph databases equip us to perform frictionless development and
graceful systems maintenance. In particular, the schema-free nature of the graph data
model, coupled with the testable nature of a graph database’s API and query language,
empower us to evolve an application in a controlled manner.
Graph users cannot rely on fixed schemas to provide some level of governance at the
level of the database. But this is not a risk; rather it presents an opportunity to implement
more visible, actionable governance. As we show in Chapter 4, governance is typically
applied in a programmatic fashion, using tests to drive out the data model and queries,
as well as assert the business rules that depend upon the graph. This is no longer a
controversial practice: more so than relational development, graph database develop‐
ment aligns well with today’s agile and test-driven software development practices, al‐
lowing graph database-backed applications to evolve in step with changing business
environment.

Summary
In this chapter we’ve defined connected data and reviewed the graph property model,
a simple yet expressive tool for representing connected data. Property graphs capture
complex domains in an expressive and flexible fashion, while graph databases make it
easy to develop applications that manipulate our graph models.
In the next chapter we’ll look in more detail at how several different technologies address
the challenge of connected data, starting with relational databases, moving onto aggre‐
gate NOSQL stores, and ending with graph databases. In the course of the discussion,
we’ll see why graphs and graph databases provide the best means for modeling, storing
and querying connected data. Later chapters then go on to show how to design and
implement a graph database-based solution.

Summary | 11
CHAPTER 2
Options for Storing Connected Data

We live in a connected world. To thrive and progress, we need to understand and in‐
fluence the web of connections that surrounds us.
How do today’s technologies deal with the challenge of connected data? In this chapter
we look at how relational databases and aggregate NOSQL stores manage graphs and
connected data, and compare their performance to that of a graph database.1

Relational Databases Lack Relationships


For several decades, developers have tried to accommodate connected, semi-structured
datasets inside relational databases. But whereas relational databases were initially de‐
signed to codify paper forms and tabular structures—something they do exceedingly
well—they struggle when attempting to model the ad hoc, exceptional relationships that
crop up in the real world. Ironically, relational databases deal poorly with relationships.
Relationships do exist in the vernacular of relational databases, but only as a means of
joining tables. In our discussion of connected data in the previous chapter, we men‐
tioned we often need to disambiguate the semantics of the relationships that connect
entities, as well as qualify their weight or strength. Relational relations do nothing of
the sort. Worse still, as outlier data multiplies, and the overall structure of the dataset
becomes more complex and less uniform, the relational model becomes burdened with
large join tables, sparsely populated rows, and lots of null-checking logic. The rise in
connectedness translates in the relational world into increased joins, which impede
performance and make it difficult for us to evolve an existing database in response to
changing business needs.

1. For readers interested in exploring the topic of NOSQL, Appendix A describes the four major types of NOSQL
databases

13
Figure 2-1 shows a relational schema for storing customer orders in a customer-centric,
transactional application.

Figure 2-1. Semantic relationships are hidden in a relational database

The application exerts a tremendous influence over the design of this schema, making
some queries very easy, others more difficult:

• Join tables add accidental complexity; they mix business data with foreign key met‐
adata.
• Foreign key constraints add additional development and maintenance overhead
just to make the database work.
• Sparse tables with nullable columns require special checking in code, despite the
presence of a schema.
• Several expensive joins are needed just to discover what a customer bought.
• Reciprocal queries are even more costly. “What products did a customer buy?” is
relatively cheap compared to “which customers bought this product?”, which is the
basis of recommendation systems. We could introduce an index, but even with an
index, recursive questions such as “which customers bought this product who also
bought that product?” quickly become prohibitively expensive as the degree of re‐
cursion increases.

Relational databases struggle with highly-connected domains. To understand the cost


of performing connected queries in a relational database, we’ll look at some simple and
not-so-simple queries in a social network domain.
Figure 2-2 shows a simple join-table arrangement for recording friendships.

Figure 2-2. Modeling friends and friends-of-friends in a relational database

Asking “who are Bob’s friends?” is easy:


Example 2-1. Bob’s friends
SELECT PersonFriend.friend
FROM Person JOIN PersonFriend

14 | Chapter 2: Options for Storing Connected Data


Discovering Diverse Content Through
Random Scribd Documents
Splash! they landed on the back of the Snail and ducked 70
him completely under water, but he bobbed right up
again. They floated across the moat in the moonlight
and the spider climbed on top of the parasol and kept
the watch, for the Snail kept getting his eyes full of
water, and Nikko was so busy holding on that she could
not look around at all.

“Take care,” said the Spider, “someone is coming! Thank


goodness, here we are ashore. But I’m afraid somebody
has seen us!”

The Snail crawled out of the water and shook himself,


stretched his eyes and paused for breath.

“Run away, Nikko,” he said. “I should like to carry you,


but my pace is too slow. You had better trust to your
feet. Hurry, hurry and flee from the land of China, or the
Emperor’s soldiers will catch you!”

“But I won’t leave you,” protested Nikko.

“Yes, you must leave me. I will follow by and by and 71


meet you in the big world beyond China, for no one will
think about me or try to catch me. Take the Spider with
you. Buddha preserve you!”
72
So Nikko and the Spider kissed him goodbye and ran
away together, over muddy fields of rice and big dry
fields of black poppies, past temples and villages till
they came to the furthest end of the country. It was
lucky they had not waited for the Snail, for no sooner
were they out of sight of the palace than they heard the
Emperor’s soldiers coming after them. Somebody really
had seen them in the moat; it was the Dragon, who had
drunk so much tea the day before that he could not
sleep that night and was prowling through the yard
looking for the Snail. He wanted to have another
argument to pass the time. But when he discovered the
Snail in the moat, carrying Nikko away from the tower,
he did not stop to argue—he ran straight to the
Emperor’s room, and told him what he had seen.

Nikko ran as fast as she could, ducking under the tea-


plants whenever she had to stop and rest. She lost one
shoe in the deep mud, so she took off the other one too
and carried it in her hand, glad to be barefoot again.
The tea-plants tore her gown, but every time there was
a fresh rent the Spider promptly mended it.

Thus they came to the great Wall of China, and there


they had to stop. The gates were locked and the wall
was much, much too high to climb over.

“Ha, we have you now!” shouted the soldiers, catching 73


up and swinging their swords most grandly.
But just then there came a mighty puff of wind that
pulled hard at Nikko’s parasol. She held on with all her
might, and the wind picked her up and wafted her high,
high into the air, and carried her clean over the wall!
The soldiers dropped their swords in amazement, and
stood with mouths agape, but there was nothing they
could do for the gate was locked and Nikko escaped
under their very noses. They could only pick up their
swords and go home again, with their pig-tails dangling
foolishly behind.

In a great forest outside the land of China, the parasol 74


came to earth. Nikko planted it in the ground to let it
grow as a toadstool once more. When it rained she sat
underneath it, and by and by the Spider made curtains
all around, so that she had a lovely little house to live
in.

About three months later, when they had given up all


hope of ever seeing the Snail again and the Spider had
hung a black crepe on the door in his memory, they
heard a rustle among the tea-plants, and saw his pearly
shell plodding through the forest!
“Well,” said the Snail, sitting down heavily, “I’m sure I 75
never want to go travelling again! The roads are really
dreadful in China, and the baggage problem is terrible!”
It had taken him two months to make the trip, and he
had to wait another month for the great gate to be
opened, for it is only opened once a year, and anyone
who wants to come in or go out has to wait for that day.

But they were so happy to be reunited again that they


soon forgot all their troubles. The Snail lived in his
house and Nikko in hers, and the Spider had Nikko’s old
shoe for a bungalow. Every afternoon Nikko received
her friends on the little lawn between the three houses,
where she spread the bright brocaded shawl and served
them tea in the most imperial fashion. When it was time
to go they would make a bow and say:

“Goodnight, Empress, Buddha preserve your Majesty!”

“But I am not an Empress any more!” said Nikko sadly.


“Lu Tsing is Empress of China.”

“Oh well,” replied the Snail, “Lu Tsing may be Empress 76


of China, but I’m sure you are Empress of everywhere
else!”

“Where is that?” asked the Spider.

“Lazybones,” said the Snail with his great-auntliest air,


“look it up on the map!”

So they continued to bow, and the Spider (who didn’t


look it up) embroidered a little sampler with these
words on it and fastened it over Nikko’s door:

“Here under curtains magnificent dwells


Nikko, the Empress of Everywhere Else!”
77
PETER DWARF

78

79
Once upon a time there was a man who lived in a dark
hut under a willow tree. His face, and his wife’s face,
and the faces of their six black-haired children, were as
dark and gnarled as the willow trunk. But when their
seventh son was born, he was a light-haired boy, with
clear blue eyes, and a smile like golden sunshine.

“This is not our child!” cried the black-eyed man and the
black-eyed woman; “this yellow-haired baby is a
changeling; the dwarfs have put him into the cradle!” So
they called him Peter Dwarf. They were very unkind to
him, and when he grew older they made him do hard,
ugly work, like picking nettles and killing lambs. Peter
liked to work, but he did not at all like to kill poor little
lambs.

80
One day it happened that the cat got into the larder and
ate a big piece of meat. The black-eyed woman took her
by the tail and flung her out of the window at Peter
Dwarf, telling him that he must get rid of her at once.
But when he had the lovely white cat in his arms, she
looked at him so pleadingly that tears came into his
eyes, and he said: “Minka, I cannot hurt you! But if I
don’t obey, my father and mother will be very angry.’”
But the cat still looked at him so sorrowfully that he
said: “Minka, let us both run away. You shall not be
harmed.”

They walked over many fields where corn and beans 81


grew in rows and the rabbits jumped away as they
came. When night fell they had reached a mountain,
and there were no more fields, only roots and rocks and
shadowy trees.

“Let us go into a cave to sleep,” Peter Dwarf suggested.


So they crept into a deep cavern, which seemed to have
no end. Peter spread his coat and lay down; but Minka
crept into all the dark crannies mewing and scratching,
and finally she disappeared. When Peter heard her
come back again, he could only see her eyes, shining
like stars in the rocky passage.

“Oho!” he cried, and the cavern echoed, “does this vault 82


go on into the mountain? I must see how far it goes.”
So he took up his coat and followed Minka. Presently
they were in the heart of the hill. The caves were cold
and damp, and it was very dark. Then Peter,
shuddering, turned around to go back, but he was
entirely lost among the winding passages, and the white
cat walked aimlessly from one cavern to another. At last,
after much wandering, they saw a light, and at the
same time they heard voices—little buzzing voices, that
sounded like a copper dish when you strike it and set it
ringing.

“They are coming this way!” whispered Peter Dwarf.


“Look, they have lanterns—they will save us. But who
are they?—Minka, they are the Diggerfolk—the Dwarfs!”

Presently they came, and their lanterns made shadowy 83


circles on the walls. They were little men, in gay,
patched clothes, and their faces were brown and
wrinkled like walnuts. They stopped, raised their arms,
and pointed at Peter, crying all together:

“Here is a mortal! Here is a child of mortals, in our own


native caverns!”
Now Peter had gone many hours without anything to
eat, and the darkness and hunger had made him shaky.
His knees gave way under him and he sank down on
the stone.

“Oh Diggerfolk, Diggerfolk, have mercy on us! We are 84


lost, and hungry, and have not a friend in the world!”

The little men all muttered and grunted; they did not
look unkind.

“Who are you?” asked one of them who carried a great


stone hammer.

“I am Peter Dwarf,” replied the boy, bowing his fair,


bright head. “And this”—he drew the white cat into his
arms—“this is Minka.”
“Peter Dwarf!” exclaimed the one with the hammer,
“why do they call you Dwarf? You are as tall and well-
shaped a boy as ever I have seen.”

85
“Because I have blue eyes and yellow hair,” Peter
replied, “I was so different from my brothers, and so
ugly that my mother said I was not her own son, but a
fairy changeling whom the dwarfs have put into the
cradle.”

“Ho, ho!” cried a big dwarf with a bunch of keys at his


belt, “so they have sent you back where you came from,
have they? And do we look as though we were your
relatives? No, no, little boy; take up that purring friend
of yours and go home to your mother and tell her that
this is no place either for her child or her pussy-cat.”
Peter was still kneeling on the ground, and Minka sat
between his knees. Now he stretched his arms toward
the little men, and implored:

“Oh good kind Diggerfolk, let me go with you and work 86


for you! My mother has not sent me; I ran away,
because I would not hurt Minka, and they would have
been very angry with me. I will work for you from
morning until night, only let me stay!”

“Work?” said the dwarf with the hammer, “how can such
a slight and princely creature work? Peter, let me see
your hands.” He felt Peter’s hands; they were thin and
strong and callous. “Yes,” he said, “this boy knows what
it is to work, I think we had better let him stay with us.
And now, Peter, since you are coming with us, let us
have a general introduction. My name is Stroke,” and he
bowed as best he could over his round stomach. “I am a
Swordsmith, and he with the pick-axe is a Miner,
Mushroom by name; he of the pointed ears is Berry, the
Blacksmith; and those three who are talking to the Lady
Minka, are Hump, the Goldsmith, Crow the
Coppersmith, and Wisely, he that jingles the keys—a
Locksmith.”

87

Peter got up and bowed to the little men. They told him
to follow, then they led the way through winding
passages down to the very center of the earth.

“Now, if you will truly learn the trades,” they said, “you 88
must work with each one of us for a year. You shall be
given plenty to eat, and shall sleep beside the fire.”

So Peter worked for the first year with Mushroom, the


Miner. They would go into the shafts together and break
the good ore out of the crags, letting the pieces roll with
a noise like thunder down into the cave where Thorn,
the Smelter, kept his furnace glowing.

The next year Peter worked for Thorn, the Smelter, and
his face became a ruddy brown from standing over the
roaring furnaces; then he learned from Berry, the
Blacksmith, how to make hammers and axes and other
tools; and the next year he helped Stroke to fashion
swords and armor. He made gold chains and brooches
and rings with Hump, and keys with Wisely, the
Locksmith. Before the seventh year was over, there was
not a lock in all Christendom which Peter could not
open.

“Keys,” said Wisely, stroking his silken beard, “Keys are 89


the most magic things in all the world. You have learned
your trades well, Peter Dwarf; now we will let you go
forth into the world and try your luck. And because you
have been faithful and sweet-natured, you shall have a
gift of magic. This gift shall be that whenever you stand
in another person’s shoes, you will be able to see what
that person sees and know what that person knows.
Now use your magic as you will, and do not forget us.
Good-bye, Peter Dwarf, good-bye!”

All the little dwarfs waved their caps and their big brown
hands, as Peter and Minka went back to the sunny
upper Earth, which they had not seen for seven years.

They wandered for a long time, when finally they came 90


to the hut where Peter had been born, but strange
people lived in it now—his wicked black-eyed family had
all died; the woman of a cat-bite, the man of a dog-bite,
and the six naughty boys of over-eating. So Peter and
his white Pussy walked on for many miles and came to a
splendid palace where a king lived with his queen.
“O Minka!” said Peter, quite breathlessly, “if one could
only look inside for one single moment! Wouldn’t you
love to look inside?”

“Mew-ew,” said the cat, rubbing her head against his


bare ankles. “Mew!”

Just then a fat gentleman, in blue and gold attire, came


running down the hillside, as fast as he could run. He
stopped to catch his breath, and then started again. He
was the king’s chamberlain. Peter bowed and spoke to
him. “Sir, is there anything I can do for you? If it is an
errand, I am a swift runner!”

“Indeed you should be swifter than I,” groaned the 91


chamberlain. “Oh what a stitch I have; what a stitch!
Yes, run if you will, and summon all the doctors in the
land, and all the wise philosophers; for the King is very
ill.”

So Peter ran, as fast as only a bare-foot boy can run;


and soon he came to a house that bore a sign:
92
He drummed on the door until the old magician came
out, pipe in hand, to ask what had happened.

“The King is very ill!” cried Peter. “Go swiftly to the


palace, good doctor, and find out what ails him.”

All afternoon Peter ran on and on, hunting up physicians


and wise men and sending them to the palace. At night
he returned to the palace and the blue-and-gold
gentleman called him into the banquet hall. Peter’s
heart beat high as he entered the shining room which
was lit by a thousand candles. Timidly he stood in the
doorway, his red pointed cap in his hands and the white
cat at his heels.

He almost lost his breath when the Queen stepped


through the great portal of gold. She was arrayed in
crimson silk, with red roses in her black hair, and tiny
silver slippers on her feet.

93

“Who is the little ragamuffin hiding behind the Lord


Chamberlain?” demanded the Queen, spying Peter
Dwarf. “Send him here, I would speak with him!”

Peter approached, frightened and dazed, and dropped


upon one knee.
“Your Majesty,” he replied to her questioning, “I am
Peter Dwarf; the kind Lord Chamberlain has permitted
me to enter the hall.”

“I like this boy,” said the Queen to the stout Lord


Chamberlain. “Put him into proper clothes and send him
back to me; he shall be my page.”

So they took Peter through many snow-white rooms to 94


a little room in the back of the palace. In it were a bed
and chairs made of rosewood, and roses painted on the
walls, and silver stars on the ceiling; so that when you
lay in bed you felt as though you were in a bower,
looking up at the starry sky.

“Here is your room,” said the servant who had brought


him in. “And these are the clothes you are to wear.”

Peter took off his leather apron and his red cap, and put
on a doublet and hose of light blue silk, and a mantle of
dark blue velvet. But happy as he was in his rich attire,
he did not forget about the king who was so ill. Every
time he met somebody who might know, he asked:

“Is his Majesty any better?”

“No,” was always the answer, “He is very ill.”

At last the kind Lord Chamberlain told Peter what the 95


doctors had said.

“Some wicked enemy of the King,” he reported, “is


burning a waxen image of his majesty over a slow fire;
and as long as the image lasts the King will live, but
when it has all melted he will die.”

“Who can it be?” cried Peter. “Does no one know?”


“Nobody knows except the wicked person himself. We
think—but say not that I told you—we think it is
someone in this very palace, for the good King has no
enemies among his neighbors.”

Suddenly Peter remembered the magic power that the


dwarfs had given him.

“Let me try on everybody’s shoes,” he cried, “and when


I come to the shoes of the wicked person I shall know
where the waxen image is melting!”

96

The Lord Chamberlain gave him permission to creep


into every bedroom and dressing room in the palace.
Minka always went ahead, and when anyone was in the
room she waved her tail to warn Peter away, but when
the chamber was empty, she said, “Mew,” and then
Peter went in and tried on all the shoes he could find.
But all the knowledge that came to him was a lot of
little foolish secrets—where the Lady Natalia kept her
jewels, and the Lord Richard had ridden over a chicken
and had not paid the poor farmer a penny for it, and
that the little chambermaid Clarissa was in love with a
beggar-man. But he could not find out where the waxen
image was melting.
Meanwhile he hardly saw the Queen at all. She was 97
always with the King, bathing his forehead, smoothing
his pillow, and getting his chicken broth.

“See,” said the chamberlain and the doctors, “how much


she loves him!”

One day the King was tired of having so many people


about him, and sent everybody away but the Queen and
Peter who had come in to fill the lamps. The Queen was
stroking the poor King’s forehead. She had forgotten
that Peter was in the room. As soon as he was asleep
she doubled up her lovely white hand and shook her fist
at him, whispering: “Melt—melt—melt! Another night
and you are done!”

Peter thought his heart would stand still. Had he really 98


heard aright? He crept out of the room as quietly as he
had come, and hastened to the Queen’s dressing room.
Never had it occurred to him to try on her shoes! He
attempted to open the door; it was locked. So he turned
sadly to his own room, and sat down to think it over.

Presently a valet came in with a message.

“You are to wait on the Queen at dinner tonight,” it


read. Peter obeyed, and thought little more about it. But
when the Queen was seated he stood behind her chair
and he noticed that she slipped her silver shoe off under
the table. Deftly he stepped out of his own, and while
she was helping herself to pink ice cream, he tried to
push his foot into her little shoe. But alas! the shoe was
so small that he could not even get his toes into it! He
tried and tried, but in vain; he had to give it up.

That night he walked disconsolately with Minka in the 99


garden. They were just under the Queen’s window
when suddenly, the casement was opened, and
something which looked like a fiery rocket or a shooting
star flew out into the air—over their heads.

“A witch!” cried Peter, and then, “The Queen!” For at


that very moment a silver slipper fell beside him in the
grass. When he had stepped into it at dinner, trying to
stand in it for just one moment, he had stretched it so
much that now it was too big for the Queen.

As it fell Minka made a leap for it and tapped it with her


paws—but no sooner was her little white foot inside the
slipper then she began to wave her tail violently. She
too had the magic gift of the Dwarfs!
“I believe you know,” cried Peter, as he followed her 100
down the garden walks. “I believe you know where the
waxen image is melting!” Peter took the slipper from
Minka and she ran ahead swiftly and quietly, and led
him over the fields and fences to a high, dark mountain.
At the foot of the mountain stood a tower of granite,
with great iron doors.

“Mew-ew,” said Minka, as she came up against the iron


doors. “Mew!”

Peter tried to force the door but it was strongly locked


and would not move. Then he peered through the key-
hole, and saw a reddish light, like the glow of a great
fire. A voice that sounded like the Queen’s was chanting
a dismal verse and Peter knew from what she said that
the image would be melted by dawn.

“Come,” said Peter to his cat, “I know what we must do; 101
but it must be quick work! Oh Minka—one more dawn,
and it will be too late!”
He went into a cavern at the foot of the mountain. Here
he called loudly down the dark passage way—“O
Mushroom, Thorn, Stroke, Wisely! Help me—help me—
help me!” And in another minute he saw little lights
approaching from all parts of the mountain, as the
faithful Diggerfolk came to his call.

Swiftly they set up a workshop, smelted the broken ore,


hammered and polished and labored as only Dwarfs
can. As the first streaks of light showed in the sky, they
finished their work. Peter, who had taken off his silken
clothes and put on a leather apron, now changed to the
new suit of mail they had wrought and parted hastily
from his friends with grateful thanks. Besides the armor,
they had made him a sword, and most important of all a
key to fit the iron doors. Soon he reached the tower
again and putting the key into the lock he used all his
strength and finally the great doors swung open.
Before him in the tower-chamber burned a mighty fire; 102
the flames jumped up around something that seemed
like a human figure, stretched directly above them—but
if you looked twice you saw it was a statue of wax,
rapidly melting away. Over it the Queen was murmuring
incantations while she watched the figure grow smaller
and smaller at every lick of the cruel flames.

Suddenly the white cat leaped at the Queen and began


to scratch her. This gave Peter time to put out the fire
and save the waxen image before it was quite melted.
A wonderful lady stood before him in the shining
dawn

“I’m coming, Minka, I’m coming!” he cried, as the 103


Queen seized her by the throat and tried to choke her.
Minka fought valiantly, until Peter rushed forward and
cut off the Queen’s head with a single stroke of his
sword. At that moment everything grew dark; a noise
like thunder came from the depth of the mountain and
Peter clasped his hands over his eyes, for he did not
want to see any more.

When he looked up again, the sun had risen. Light


flooded the room, and a wonderful lady, clad in white
samite as soft and pure as Minka’s fur, stood before him
in the shining dawn. She held out her hands, and
shaking back her golden hair said:

“Peter Dwarf, my good sweet Peter, I am the Princess


Minka; don’t you recognize me?” Then Peter looked into
her starry eyes, and knew that his beloved Minka must
have been enchanted by the wicked Queen many years
ago, and that now the spell was broken.

So they returned swiftly to the royal palace, where they 104


found everybody rejoicing because the King was so
much better that that morning he had eaten four
buckwheat cakes with syrup for his breakfast. And the
King dubbed Peter a knight and made him general of his
army and for a wedding present gave him a palace with
a great rose-garden and a banquet hall. Soon after this
Peter and Minka were married. When the good King
died, many years later, they were made King and
Queen, and ruled in peace and happiness all the rest of
their days.
105
THE CRYSTAL BOWL

106

107
Once upon a time in distant Orient Land, there lived a
beautiful royal maiden, the Princess Zarashne. Her hair
was long and black as the veils of night, her arms were
fair as the pink sea-shells, and her gowns were as
glorious in color as the feathers of the peacock.
Everybody in the palace where she lived, and in the
town outside the palace-walls, loved Princess Zarashne;
even the animals who dwelt far away in the desert had
heard how good and beautiful she was, and many of
them came to town hoping they might see her—the lion
and the tiger, and the striped zebra, the ostrich and the
camel and the curious giraffe. But the keeper of the
palace-gate was afraid to let them in, so the only ones
who saw her were the ostrich and the giraffe, because
they could look over the wall and watch her feed her
gold-fish in the fountain.

108
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookname.com

You might also like