W Java135
W Java135
NEO4J
Preface 2
Introduction to Neo4j 2
What is a Graph Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Cypher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Getting Started 3
Installing Neo4j . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Accessing Neo4j Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Creating a new graph database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Basic Data Retrieval 4
Retrieving nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Retrieving relationships. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Combining node and relationship retrieval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Filtering and Sorting 4
Using WHERE to filter nodes and relationships. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Applying multiple conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Sorting query results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Aggregation and Grouping 4
Using COUNT, SUM, AVG, MIN, MAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
GROUP BY clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Filtering aggregated results with HAVING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Advanced Relationship Traversal 5
Traversing multiple relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Variable-length relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Controlling traversal direction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Pattern Matching with MATCH 5
Matching specific patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Optional match with OPTIONAL MATCH. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Using patterns as placeholders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Working with Path Results 5
Returning paths in queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Filtering paths based on conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Limiting the number of paths. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
PREFACE
PREFACE intuitive and efficient manner compared to
traditional relational databases.
This cheatsheet is your guide to effectively querying
graphs using Neo4j. Whether you’re a seasoned Graph databases offer several advantages,
database professional looking to expand your skills especially when dealing with highly interconnected
or a curious enthusiast eager to dive into the world data:
of graph data, this resource is designed to provide
you with quick and concise information to get you Advantages Of Graph Description
started. Databases
Query Performance Graph databases In this query, MATCH is used to specify the pattern
generally offer superior you’re looking for, (p:Person) defines a node
query performance labeled as "Person," and RETURN specifies what
when retrieving related information to retrieve.
data, thanks to their
optimized traversal Cypher also supports a wide range of functionalities
mechanisms. beyond basic querying, including creating nodes
and relationships, filtering, sorting, aggregating
Natural Representation Graph databases
data, and more. It’s a central tool for interacting
provide a more natural
with Neo4j databases effectively and efficiently.
way to model and
represent real-world It’s important to note that while Cypher is specific
scenarios, aligning well to Neo4j, other graph databases might have their
with how humans own query languages or might support other query
perceive and languages like GraphQL, SPARQL, etc., depending
understand on the database technology being used.
relationships.
GETTINGSTARTED
GETTING STARTED
However, it’s important to note that while graph
databases excel in certain use cases, they might not
To begin using Neo4j for Querying Graphs, follow
be the optimal choice for every type of application.
these steps:
Choosing the right database technology depends on
the specific needs of your project, including data
structure, query patterns, and performance INSTALLING NEO4J
requirements.
Download and install Neo4j from the official
website. Choose the appropriate version based on
CYPHER your operating system. Follow the installation
instructions for a smooth setup.
Neo4j uses its own language for Querying Graphs
called Cypher. Cypher is specifically designed for
querying and manipulating graph data in the Neo4j ACCESSING NEO4J BROWSER
database. It provides a powerful and expressive
Neo4j Browser is a web-based interface that allows
way to interact with the graph database, making it
you to interact with your graph database using
easier to work with nodes, relationships, and their
Cypher queries. After installing Neo4j, you can
properties.
access the browser by navigating to
https://localhost:7474 in your web browser.
Cypher is designed to be human-readable and
closely resembles patterns in natural language
when describing graph patterns. It allows you to CREATING A NEW GRAPH DATABASE
express complex queries in a concise and intuitive
manner. Cypher queries are written using ASCII Once you’re in Neo4j Browser, you can create a new
art-like syntax to represent nodes, relationships, graph database using Cypher. For example, to
and patterns within the graph. create a node with a "Person" label and a "name"
property, run:
For example, a simple Cypher query to retrieve all
nodes labeled as "Person" and their names might
look like:
CREATE (:Person {name: 'John'})
MATCH (p:Person)
BASIC
BASICDATA
DATARETRIEVAL
RETRIEVAL RETURN p
RETRIEVING NODES
MATCH (p:Person)
To retrieve all nodes with a specific label, use the
MATCH clause followed by the label: WHERE p.age > 30 AND p.location =
'New York'
RETURN p
MATCH (p:Person)
RETURN p
SORTING QUERY RESULTS
FILTERINGAND
FILTERING ANDSORTING
SORTING GROUP BY CLAUSE
Use the WHERE clause to filter query results based on Group data based on specific properties:
specific conditions.
MATCH (p:Person)
USING WHERE TO FILTER NODES AND
RETURN p.location, AVG(p.age) AS
RELATIONSHIPS
avgAge
Filter nodes based on property values: GROUP BY p.location
MATCH (p:Person)
WHERE p.age > 30
Neo4j power of Querying Graphs lies in traversing OPTIONAL MATCH WITH OPTIONAL
complex relationships. MATCH
MATCH (p:Person)-[:FRIENDS_WITH]-
MATCH (p:Person)- >(friend:Person)
[:FRIENDS_WITH*1..2]- WITH friend, size((friend)-[:LIKES]-
>(friend:Person) >()) AS numLikes
RETURN p, friend WHERE numLikes > 2
RETURN friend
Cypher allows you to create, update, and delete DROPPING INDEXES AND CONSTRAINTS
data.
Remove indexes and constraints as needed:
Use the SET clause to update properties: USING WITH FOR RESULT PIPELINING
PERFORMANCETUNING
PERFORMANCE TUNING
CHAINING MULTIPLE QUERIES
Chain queries together using semicolons: Optimize your queries for better performance.
Importing external data into Neo4j as graphs for • Avoid unnecessary traversals by using specific
querying is a common task. patterns.
Perform calculations on date properties: Export query results for further analysis.
EXPORTING TO CSV
MATCH (p:Person)
SET p.age = date().year - Use the EXPORT CSV clause to export data to a CSV
p.birthdate.year file:
RETURN p
MATCH (p:Person)
HANDLING TIME ZONES RETURN p.name, p.age
EXPORT CSV WITH HEADERS FROM
Use the datetime() function to work with time 'file:///people.csv'
zones:
Title Description
JCG delivers over 1 million pages each month to more than 700K software
developers, architects and decision makers. JCG offers something for everyone,
including news, tutorials, cheat sheets, research guides, feature articles, source code
and more.
CHEATSHEET FEEDBACK
WELCOME
[email protected]
Copyright © 2014 Exelixis Media P.C. All rights reserved. No part of this publication may be SPONSORSHIP
reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, OPPORTUNITIES
mechanical, photocopying, or otherwise, without prior written permission of the publisher. [email protected]