SlideShare a Scribd company logo
1 of 75
Download to read offline
Introduction to Neo4j
Cypher
github.com/maxdemarzi
About 200 public repositories
Max De Marzi
Neo4j Field Engineer
About
Me !
01
02
03
04
maxdemarzi.com
@maxdemarzi
About 175 blog posts
• Relational Databases (quick version)
• Graph Databases (quick version)
• The most important Slide about Neo4j you will ever see
• Neo4j Desktop
• Setup
• Introduction to Cypher
• The Movies Dataset
• Questions
Agenda
A query comparison
Relational VS Graph
Orders
OrderItemsOrderItems PK index
{Order_num, Item_num}
<m
<f5
>c11
<d112
Order_num=e1234567
<e4
>e120 Products
Products PK index
{product_num}
Manufacturer
Retailer
SQL Joins – using Primary Indexes
6 IO’s traversing the index
(plus a few if leaf is >1 page)
(do 6 times – 1 for each row)
7 IO’s traversing the index
(plus a few if leaf is >1 page)
5 IO’s traversing the index
(plus a few if leaf is >1 page)
(do 6 times – 1 for each row)
5 IO’s traversing the index
(plus a few if leaf is >1 page)
(do 6 times – 1 for each row)1 outer row + 7 index + 6 inner rows + 6(6 index + 1 row) + 6(5 index + 1 row) + 6(5 index + 1) row = 128 IO’s
Manufacturer
Orders
OrderItems
<m
<f5
>c11
<d112
Order_num=e1234567
<e4
>e120 Products
Retailer
Neo4j – Index Free Adjacency
6 direct pointers
1 outer node + 6 inner nodes + 6(p nodes) + 6(m nodes) + 6 (r nodes) = 25 operations
1 direct pointer
1 direct pointer
1 direct pointer
-[:HAS_ITEMS]!
-[:ORDERED_PRODUCT]!
-[:MADE_BY]!
-[:SOLD_BY]!
7
Native Storage and Processing
Neo4j disk and
memory structures
link data directly,
allowing
millions graph
traversals per
second per core.
Graph data and
paths between data
do not have to be
pre-defined before
they can be used.
Property Graph Model Components
Nodes
• Relate nodes by type and direction
• Can have Properties
• Can have Labels
• Can have Properties
name:”Dan”
born: May 29, 1970
twitter:”@dan”
name:”Ann”
born: Dec 5, 1975
Since:
Jan 10, 2011
brand: “Volvo”
model: “V70”
Car
LOVES
LIVES_WITH
D
RIVES
Person
O
W
NS
Relationships
Person
The Most Important Slide
about Neo4j you will ever see
Fixed Sized Records
“Joins” on Creation
Spin Spin Spin through this
data structure
Pointers instead of Searches
1
2
3
4
Partitions
Each Node’s
relationships are
partitioned by
type and direction.
Getting around in Neo4j Desktop &
Browser
Neo4j Desktop: UI for developers & DB management
Supports “plugins”
• Neo4j official plugins
• Neo4j labs plugins
• 3rd party plugins
✓ Will ship with free Bloom plugin for use with local databases
managed by desktop only
Allows you to manage local databases
• Create, stop, start, manage
• Add apoc, procedures, etc.
• See log files, configuration, etc.
Allows you to connect to remote databases
• You can’t manage – but you can open browser
Supports organization via “projects”
Managing Databases
Only for local databases (on desktop)
Quick access to
• Logs
• (Configuration) Settings
• Plugins
• Upgrade
• Administration
Also quick access to file locations
• Database parent directory
• …or one of several common subdirectories
✓ Import (for copying csv’s for loading)
✓ Plugins
✓ Logs
✓ Configuration
Finding logs
Displays neo4j.log
Clicking on neo4j.log link…
• Takes you to the log directory
Fast access to
• Debug log
• Query log
• (Neo4j log)
• Security log
(Configuration) Settings
Modifies neo4j.conf
• Not an online operation
• Makes changes and the reboots system
For dynamic configurations:
• Use browser
• Cypher procs
✓ Call dbms.listConfig(<text pattern>)
✓ Call dbms.setConfigValue
Plugins
Fast way to install Neo4j plugins
What it does
• Gets correct version of plugins for Neo4j version
• Installs .jar file in ./plugins folder
• Modifies config file
• Restarts database
Note: After any upgrade
• Check – if it says install – install them (upgraded ones)
• You may need to delete the OLD jar first
✓ “Open Folder” ! Plugins
✓ Delete apoc*.jar
• If you don’t, server will fail on boot
✓ Due to duplicate jar files
Upgrade
Automates update process
Process
• Downloads version selected if not in repository
• Installs the version
• Copies data from old version to new version
• Performs upgrade
If it fails….
• Since it did a copy….not a problem
• Simply points back to original version
Administration
Only function is to set database password
• I always use “swordfish”
Alternate method for resetting password
• ./bin/neo4j-admin set-initial-password
How to get more plugins
Click on Graph Applications tab
• Click on “Discover more graph apps” link
It will launch install.graphapp.io
Pick and install
Recommended:
• Halin ! monitoring
• Query log analyzer ! analyzing query.log
• DB Analyzer ! space/disk/database monitoring
• Cloud Tool ! managing cloud instances of Neo4j
Notifications
Lets you know…
• If upgrades are available for any applications
• If upgrades are available for desktop overall
License keys
License keys
• Select “Add software key”
• Copy/paste link
• Only manages license keys for local database instances
managed by Neo4j desktop
Neo4j Browser
In reality
• Light weight web/javascript application
Purpose
• Cypher coding
• Quick/small visualizations
• Exporting result sets
Limitations – only one at a time
Available via your favorite web browser
• Same bolt protocol & UI
• Easy way to bypass the above limitation
https://www.youtube.com/watch?v=oHo-lQ79zf0&feature=youtu.be
Cypher vs. Client commands
Client commands are prefaced with “:”
• :exit
• :use <database>
• :params
• :auto
• …etc….
These are not cypher
• They are instructions to the client utility tool
✓ Neo4j Browser, cypher-shell, ….
• Which ones are supported depend on which utility and which
versions
The “:use” command
• When issued, it disconnects/reconnects with new database
name
Recommendation of things to do immediately
Enable multi-statement query editor
• Or scripts with more than one query will fail
✓ ….those create constraint scripts with ;’s
Clean initial command of :play start
• ….annoying…..
If performance is an issue
• De-select “Connect result nodes”
✓ In case you wondered…
✓ After every query executes, the browser executes a separate query
for each node to find any connections between them that the
query didn’t explicitly expose
✓ With any complicated result set – this can slow things considerably
• You can also reduce the result set size
✓ …or increase if exporting
Node Labels/Relationship Types
Displays a list of…
• Node labels
• Relationship types
• Properties
Runs a quick query
• Returns the first 25 values it finds
✓ Not necessarily in any order and not necessarily the same ones
each time…
• Be careful with relationship types
✓ Uses a label scan….so may not be fast…might be a few seconds to
return
Saved Scripts
Lets you save frequent queries
• …as typical
You can organize in folders
• Better than trying to find the needle in haystack
You can export to a zip archive
• Good to do before any upgrades to desktop!
No import capability
• Not sure where they are saved normally
• Workaround is to ….
✓ Export to zip
✓ Unzip to individual cypher queries
✓ Copy paste into browser and resave
Documentation
Quick access to links for Neo4j documentation
• Links – not local – opens web browser
List of browser commands
Access to online resources
• GraphGists
• Developer site
Query Editor in Browser
Careful!!!
• <ENTER> executes the query
✓ Source of a lot of syntax errors
✓ Wish this was configurable
• <Shift><ENTER> for CR/LF in editor
• <ESCAPE> full screen editor mode
Controls
• Star ! favorites
✓ If using a favorite query, it changes to a pencil to signify editing the
query
• Eraser ! clears query editor
• Play ! executes query
Query Execution
If running for a long time
• X will try to cancel…
• ….but if it is the layout taking time (very often) the only way to
kill is to quit the browser and reopen
Controls
• Download ! Export
• Pin ! pins to top
✓ Useful with db.schema.visualization
✓ Also useful with outputs using as literals for next queries
• Full screen mode
• Collapse ! used with query plan output
• Re-run ! reruns the query reusing same output window
✓ One side effect is that it immediately scrolls to top of browser
unless you change setting
• X/Close ! Closes window; attempts to abort query and
close window if still running in database
✓ If you want to analyze query for slowness click on query text first
to reload into browser before hitting X
Query Results
Graph
• A rudimentary graph depiction of the results
• Can be customized (colors, sizes, captions, etc.)
Table
• Displays output in tabular form
• If return is a node or relationship, a JSON map will be
displayed
Text
• Pure ascii text in JSON map format
• Display width is configurable
Code
• Query text
• Summary of query impact/write operations
• Response: JSON structure of results
Working with Neo4j Customer Success Team
Slow query?
Execute with EXPLAIN or PROFILE
• EXPLAIN ! query plan only without execution
✓ Estimates only
• PROFILE ! query plan & stats after execution
• Adds “Plan” tab to results
Sending
• Expand all
• Export to JSON
✓ For quick access to stats values, etc.
• Export to SVG
✓ Graphic representation that can be emailed
Collapse/Expand
• Collapses, expands query tree as desired
Who ya gonna call??
Sales
• Overall account management
✓ Can help coordinate with anyone needed
• Has pre-sales engineer for technical assistance
• Can coordinate with consulting for short term engagements
✓ Or facilitate with partner for long term projects
Pre-sales
• Assists with pre-sales and some post-sales activity
• Can come on site (if allowed and no covid-19)
• Short (1 week or less) POC’s, demo’s, how-to’s
• Has lots of accounts (many 10’s) – “interrupt driven”
Customer Success
• Best practices, architecture, review data modeling
• Help with cluster setup, configuration, architecture
• They don’t fix bugs – they can help you open a case though
• They mainly help you when stumped with implementation
problems
✓ Help with query tuning/diagnosing performance issues
✓ Help diagnose problems you are having
Engineering
Sales
Pre-Sales Engineer
(aka Field Engineer)
Professional Services
Customer Success
Engineer
(aka Field Engineer)
Customer Success Manager
Support Engineer
Developer Relations
Neo4j Community
https://community.neo4j.com
Community Edition Enterprise Edition
Enhancementrequests
Questions, Defects
Enhancement requests
Assigns
Coordinates
Short technical
Knowledge Base
Articles
Points you to…
Setup
Create a new empty database instance
Start Neo4j Desktop
Create a new empty database
• Create a local graph
• Change the database name to whatever you want
• Enter a password you will remember
• Optional: Select the version
✓ Note that if you pick a version other than the one downloaded with
desktop, desktop will need to download the specified version –
which can take a while
• Press “Create”….
Starting the database server
Press Start
• Spawns JVM (neo4j is native java)
Database goes through recovery
• Reads config file
• Opens database files
• Plays back any missing transactions from log
• Loads plugins
• Etc. ….
Started….
Open it
CREATE (:User {username:”max”})
MATCH (u:User {username:”max”})
RETURN u
WITH ["Jennifer","Michelle","Tanya","Julie",
"Christie","Sophie","Amanda","Khloe","Sarah",
"Kaylee"] AS names
FOREACH (r IN range(0,100000) |
CREATE (:User {username:names[r %
size(names)]+r}))
WITH ["Jennifer","Michelle","Tanya","Julie",
"Christie","Sophie","Amanda","Khloe","Sarah",
"Kaylee"] AS names
FOREACH (r IN range(0,100000) |
CREATE (:User {username:names[r %
size(names)]+r}))
PROFILE MATCH (u:User {username:”max”})
RETURN u
CREATE INDEX ON :User(username);
PROFILE MATCH (u:User {username:”max”})
RETURN u
MATCH (u1:User),(u2:User)
WITH u1,u2
LIMIT 5000000
WHERE rand() < 0.1
CREATE (u1)-[:FRIENDS]->(u2)
Run it
MATCH (u:User)-[*1..4]->(c)
WHERE u.username = 'max'
RETURN count(DISTINCT c)
MATCH (u:User)-[*]-(c)
WHERE u.username = 'max'
RETURN count(DISTINCT c)
Introduction to Cypher
What is Cypher?
Declarative query language
• Focuses on what, not how to retrieve
• E.g. optimizer figures out best approach
Uses CLAUSE keywords such as
• MATCH, WHERE, CREATE, UNWIND, WITH, MERGE, …
Is a dataflow/pipelined language
• Results of each clause in a statement flow into next as
materialized
Multi-statement batches supported
• Each statement terminated with “;” (semi-colon)
Runs in the database for the graph
• Each database has its own query engine
• As of 4.0, no cross-db queries (due to the above)
Graph Model ! Cypher
• Nodes ! nodes or node labels (depending on context)
• Edges ! relationship types
Foundation for ISO standard query language
• For graph databases
MATCH <pattern> [, <pattern> [, …]]
[WHERE [NOT] <expression> <op> <expression>
[[AND | OR | XOR] [NOT] …]
[OPTIONAL MATCH [WHERE …]]
[WITH [DISTINCT]
[WHERE]
[ORDER BY]
[LIMIT]]
[MATCH […]]
[CALL [YIELDING]]
RETURN [DISTINCT]
[ORDER BY … [ASC|DESC]]
[[SKIP] LIMIT]
[UNION [ALL]]
…
What is Cypher? (cont)
Similar to other declarative languages
• SQL like syntax….sort of
✓ MATCH ! FROM + JOIN
✓ WHERE ! WHERE
✓ RETURN ! SELECT
✓ CREATE ! atomic insert
✓ MERGE ! upsert/merge
✓ DELETE ! delete
Supports expressions, functions, procedures
• Including CASE expressions
• Large set of pre-defined functions/procedures
• User Defined Functions (UDF) or custom stored procedures
can be developed in Java
✓ Support for direct API’s into graph structure vs. sending cypher for
interpretation
CREATE
[WITH MATCH …]
MATCH
[WHERE]
[MERGE
[ON MATCH SET]
[ON CREATE SET]]
[SET | REMOVE]
[[DETACH] DELETE]
[MERGE …]
[WITH]
[MATCH …]
52
Cypher is ASCII Art
In graph model
• Nodes are circles with labels
✓ nouns, objects, stuff
✓ In neo4j, the standard is to use “WordCaps” capitalization
✓ A “node” is a specific instance of a label
• Relationship types are arrows with types
✓ verbs, association
✓ In neo4j, the standard is to use “ALL_CAPS” capitalization
✓ A “relationship” is a specific instance of a relationship type
• Properties are attributes of either label or relationship type
✓ In neo4j, the standard is to use “camelCase” capitalization
Cypher ASCII art (MATCH clause)
• ( ) – [ ] -> ( )
• (label)-[relationship_type]->(label)
ASCII art meets Schema
• Cypher uses “labels” to identify the node/relationship type
being referenced if not referring to a node/relationship from
an earlier clause
• (:label)-[:rel_label]->(:label)
(:AccountHolder)-[:HAS_ACCOUNT]->(:Account)
(:Account)-[:WITHDRAWALS]->(:Transaction)
Cypher is ASCII Art
A
B C
LIKESLIKES
LIKES
(A)-[:LIKES]->(B),(A)-[:LIKES]->(C),(B)-[:LIKES]->(C)
(A)-[:LIKES]->(B)-[:LIKES]->(C)<-[:LIKES]-(A)
Neo4j is a NoSQL/Schema-less/ “Schema-optional” DBMS
• A loose schema can be created using a combination of constraints & indices
✓ E.g. You can enforce that certain properties exist (not null) and are unique, but you cannot enforce a datatype nor a relationship
Classic schema components supported
• Unique constraints
• Node key constraints (aka “Primary Key”) – multi-column
• Indexes (multiple-column) – but only on node properties
• Exists constraints (aka “not null column”)
Labels, Relationship Types, Property & Variable Names are Strings
• Must begin with a letter (1stbase ! no; Base1 ! yes)
• Cannot contain symbols – except underscore (@myvar ! no; my_var ! yes)
• Can contain numbers
• Can be really really really long (64k)
• Exception: references to parameters begin with $
• Weird values/spaces can be escaped with backticks (`this is a valid name`)
✓ Equivalent to the “double quotes” in SQL
Properties can be one of the supported datatypes
Cypher & Schemas
Unique Constraint & Node Key
Unique Constraint
• Only supports a single column
• Useful for Nodes properties that are “identities” (username, email
address, product number, serial number, etc)
✓ Creates an index automatically
Node Key Constraint
• Essentially, the primary key
✓ Specifies the columns (one or more) that comprise the unique attributes to
identify a node
• Except more than one allowed (alternate keys)
• Essentially a unique + exist constraint
✓ Creates an index the same as a unique constraint does
Constraint Admin
• Recreating (same columns) simply updates existing one
✓ No need to do if exists() DROP CONSTRAINT in scripts
• Recreating with different columns creates a second node key
• Call db.constraints to see list
// 3.5 Syntax
CREATE CONSTRAINT ON ( city:City ) ASSERT
(city.cityName, city.stateCode, city.countryCode) IS NODE KEY;
DROP CONSTRAINT ON ( city:City ) ASSERT
(city.cityName, city.stateCode, city.countryCode) IS NODE KEY;
// 4.0 Syntax
CREATE CONSTRAINT City_key ON ( city:City ) ASSERT
(city.cityName, city.stateCode, city.countryCode) IS NODE KEY;
DROP CONSTRAINT City_key;
// See all constraints
CALL db.constraints
// Unique Constraint
CREATE CONSTRAINT ON ( acct:DepositAccount ) ASSERT
(acct.accountNumber) IS UNIQUE;
Exists Constraints
Exists Property Constraint
• Equivalent to NOT NULL in SQL for column attribute
• Attempting to create a node without a value for this property
or removing this property fails
Both Nodes & Relationship Exists Property
• On a node, enforces that the property exists (and has a value)
• Same thing for a relationship
• No way to enforce that a relationship exists
✓ E.g. no equivalent to an Fkey!Pkey enforcement
✓ That’s because both nodes have to exist before the relationship can
be created (chicken & egg problem)
// 3.5 Syntax
// 4.0 Syntax …is similar to previous examples
CREATE CONSTRAINT ON (acct:DepositAccount) ASSERT
EXISTS(acct.balance);
CREATE CONSTRAINT ON ()-[er:ENTITY_RESOLUTION]-() ASSERT
EXISTS(er.confidenceFactor);
DROP CONSTRAINT ON (acct:DepositAccount) ASSERT
EXISTS(acct.balance);
DROP CONSTRAINT ON ()-[er:ENTITY_RESOLUTION]-() ASSERT
EXISTS(er.confidenceFactor);
// This works
CREATE CONSTRAINT ON (acct:DepositAccount) ASSERT
EXISTS(acct.balance);
// This or anything like it doesn’t work – strictly exists()
CREATE CONSTRAINT ON (acct:DepositAccount) ASSERT
EXISTS(toFloat(acct.balance));
Indices (Normal Indexes)
Indexes
• Created on a LABEL with properties
✓ Be careful with multiple labels
• Supports multiple columns
• Builds a standard B-Tree
• Equivalent to Non-Unique Indexes in SQL
Index for performance
• Only used if LABEL is referenced in MATCH/MERGE/DELETE/
CREATE clauses
✓ When using multiple labels, mention both the desired label and
ultimate parent node label
✓ Examples of this later
• Used for all the usual predicates
✓ equality check: n.prop = value
✓ list membership check: n.prop IN list
✓ existence check: exists(n.prop)
✓ range search: n.prop > value
✓ prefix search: STARTS WITH
✓ suffix search*: ENDS WITH
✓ substring search*: CONTAINS
*Note: effectively both of these do index leaf scans vs. B-tree traversal
Index Notes
• No equivalent to “Clustered” Indexing
• CALL db.indexes to see indices
// 3.5 Syntax
// accountName property for ALL account holders is indexed
CREATE INDEX ON :AccountHolder(accountName);
// businessName property is only indexed for BusinessCustomer
// labeled account holders (sparse indexing?)
CREATE INDEX ON :BusinessCustomer(businessName);
// lastName & firstName properties are only indexed for Customer
// labeled account holders (sparse indexing?)
CREATE INDEX ON :Customer(lastName,firstName);
DROP INDEX ON :AccountHolder(accountName);
DROP INDEX ON :BusinessCustomer(businessName);
DROP INDEX ON :Customer(lastName,firstName);
// See all indices
CALL db.indexes
// 4.0 Syntax
CREATE INDEX AcctName_idx FOR (acct:AccountHolder) ON (acct.accountName);
CREATE INDEX BusName_idx FOR (bc:BusinessCustomer) ON (bc.businessName);
CREATE INDEX CustName_idx FOR (cust:Customer)
ON (cust.lastName,cust.firstName);
DROP INDEX AcctName_idx;
DROP INDEX BusName_idx;
DROP INDEX CustName_idx;
Indices (Node Key, Unique Constraints & Index)
Critical for finding starting point(s)
• While it is true we don’t need indices for traversals, we do
need them for unique enforcement as well as anchor points
An index on :Account.accountHolderID is not need – just
traverse from :AccountHolder instead
Critical for using MERGE statements
• MERGE statements without Indexes must Scan all nodes of a
particular label
Text Indexing
Full Text Indexes
• Can span more than one node
• Can have multiple properties
• Supports different languages, etc.
• Maintained automatically
• Only index available for Relationships
✓ But effectively only good for near equality matches as range,
etc. won’t work correctly due to lexical sequencing vs. numeric,
etc.
When to use
• When storing paragraphs of text to search (e.g. knowledge
graphs)
• Finding relationships based on String datatype property
values
Can be used with Linkurious
• Scales better than included ElasticSearch
• Supports Linkurious clustering better – and allows virtual
clusters
• Used for AlternateNodeID’s for retrieving visualizations
saved to disk when DB id’s change (e.g. if rebuilding
database each night)
• Allows full query integration
Usage Procedure Description
Create full-text node
index
db.index.fulltext.createNodeIndex Create a node fulltext index for the given labels and properties.
The optional 'config' map parameter can be used to supply
settings to the index. Supported settings are 'analyzer', for
specifying what analyzer to use when indexing and querying.
Use the db.index.fulltext.listAvailableAnalyzers procedure to
see what options are available. And 'eventually_consistent'
which can be set to 'true' to make this index eventually
consistent, such that updates from committing transactions are
applied in a background thread.
Create full-text
relationship index
db.index.fulltext.createRelationshipIndex Create a relationship fulltext index for the given relationship
types and properties. The optional 'config' map parameter can
be used to supply settings to the index. Supported settings are
'analyzer', for specifying what analyzer to use when indexing
and querying. Use
the db.index.fulltext.listAvailableAnalyzers procedure to see
what options are available. And 'eventually_consistent' which
can be set to 'true' to make this index eventually consistent,
such that updates from committing transactions are applied in a
background thread.
List available analyzers db.index.fulltext.listAvailableAnalyzers List the available analyzers that the full-text indexes can be
configured with.
Use full-text node
index
db.index.fulltext.queryNodes Query the given full-text index. Returns the matching nodes
and their Lucene query score, ordered by score.
Use full-text
relationship index
db.index.fulltext.queryRelationships Query the given full-text index. Returns the matching
relationships and their Lucene query score, ordered by score.
Drop full-text index db.index.fulltext.drop Drop the specified index.
Eventually consistent
indexes
db.index.fulltext.awaitEventuallyConsisten
tIndexRefresh
Wait for the updates from recently committed transactions to be
applied to any eventually-consistent full-text indexes.
Node/Relationship properties
• Do not have to have the same datatype for the same property name on different object
CREATE (:Person {name: “Jeff Tallman”, ssn: “123-45-6789”, born: date(“1900-01-01”)}) // born is a date
CREATE (:Person {name: “Julius Ceasar”, born: “12 July 100 BC”, died: “15 March 44 BC”}) // born/died are strings
CREATE (:Person {name: “Leonhard Euler”, born: date(“1707-04-15”), died: date(“1783-09-18”) }) // born/died are dates
• Two objects do not need to have the same property list
CREATE (:Customer {lastName: “Tallman”, firstName: “Jeff”})
CREATE (:Customer {businessName: “Neo4j”})
• Allows a lot of flexibility, but can cause queries to fail/break
If a property doesn’t exist, the value is considered to be NULL
MATCH (p:Person) WHERE p.name=“Your Name”
RETURN p.name, p.born, p.died //returns NULL for “died”
A unique constraint only constrains nodes where the property exists!!
• Therefore a unique constraint can be created on Person.ssn even though Julius Ceasar and
Leonhard Euler both lack the “ssn” property
Schema-less/Schema Optional & Properties
Storage Types
• Very basic java types
✓ Number subtypes Integer and Float
✓ String
✓ Boolean
✓ The spatial type Point
✓ Temporal types: Date, Time, LocalTime, DateTime, LocalDateTime and Duration
• The default type is String – you will need to cast to other types when loading
✓ toInteger(), toFloat(), date(), datetime()
• Storing of arrays/lists of basic types allowed, but no maps (JSON)
Structural (Dynamic) Types
• ‘Dynamic’ types can be used in cypher (e.g. variables), but not stored natively
✓ Nodes, Relationships, Paths
✓ Maps (e.g. JSON)
✓ Lists/arrays of more than one homogeneous type
Missing datatypes
• Use a “String” or use an Array of Bytes
Cypher Schema: Datatypes
Working with date parts
• Use dot notation
• E.g. myDate.month, myDate.day, ….
Date math
• First, compute duration (e.g. duration.inDays(date1,date2))
✓ E.g. WITH duration.inDays(account.dueDate,account.paymentDate) as myDuration
• Then use dot notation much like with dates
✓ E.g. myDuration.days > 30
• Other things that work
✓ Date comparisons ! date1 [<|>|=|<=|+>] date2
This is also true of any “structure” datatype in Cypher
• Node.property, Relationship.property
• Map.property
Date Datatypes in Cypher
Movies
Create a new database (graph) (1)
Launch browser
• You will automatically be connected to the “default” database
– which by default is “neo4j”
Enter “:use system”
• Don’t forget the colon
✓ This tells the browser it is a client command vs. cypher to send to
the server
• This will switch your connection to system database
New databases can only be created in the
system database
• Along with a few other commands that are restricted to
system database environment
• If you forget, Neo4j will tell you
Create a new database (graph) (2)
Enter “create database movies”
If it succeeds…you will still be in system db
Connect to “movies” database
• Type “:use movies” (note colon)
…or…
• Select the metadata menu (database icon)
• Select movies from drop down
Prompt should read “movies”
Load the Movies data (1)
Use the command
• :play movies
Pin the result pane
Press “NEXT” Button (>)
Load the Movies data (2)
Click on the code block
• Copies queries to editor
View cypher
• Click in editor
• Hit ESCAPE key
• Scroll around a bit and look at the cypher
✓ We will discuss the CREATE verbs later
• Hit ESCAPE to exit full screen editor
NOTE:
• There is only 1 semi-colon (line 508)
• Yes – this IS one big Cypher statement
✓ Not multiple statements …..ONE statement
✓ Hundreds of clauses – one statement
✓ One statement ! it has to be one transaction
Load the Movies data (3)
Execute the cypher
• Creates 171 nodes of 2 LABELS
• Creates 253 relationships in 6 relationship types
View results
• Default is graph mode due to RETURN statement
• Select table button
• Select text button
• Re-select graph button
Style the results (1)
Go full screen mode
• This can be done in normal pane mode as well
Select the Person label at top left
• Notice the bottom edge is now a style guide with colors, sizes
and caption properties
Select a new color
Repeat for Movies
Style the results (2)
Select the DIRECTED relationship type
Select a new width for line
• Not too wide
Select a new color for line
…this is visualization in most primitive form
• …but it does provide fast visual acuity for looking at results
when in graph form
Press expand icon to reshrink pane
When there’s lots….
When there’s lots (continued)…..
Simple MATCH statements
Go to next page
Select & execute each statement
• One at a time
NOTE: 3rd (& 4th) query results are tabular
• Any query that returns a “node” defaults to graph mode
• Any query that doesn’t return a node, but rather a list of
properties defaults to tabular mode
Questions
Thank you!

More Related Content

What's hot

What's hot (20)

Outrageous Ideas for Graph Databases
Outrageous Ideas for Graph DatabasesOutrageous Ideas for Graph Databases
Outrageous Ideas for Graph Databases
 
Intro to Neo4j
Intro to Neo4jIntro to Neo4j
Intro to Neo4j
 
Training Week: Introduction to Neo4j
Training Week: Introduction to Neo4jTraining Week: Introduction to Neo4j
Training Week: Introduction to Neo4j
 
Workshop - Neo4j Graph Data Science
Workshop - Neo4j Graph Data ScienceWorkshop - Neo4j Graph Data Science
Workshop - Neo4j Graph Data Science
 
Spark streaming + kafka 0.10
Spark streaming + kafka 0.10Spark streaming + kafka 0.10
Spark streaming + kafka 0.10
 
Building social network with Neo4j and Python
Building social network with Neo4j and PythonBuilding social network with Neo4j and Python
Building social network with Neo4j and Python
 
Transforming AI with Graphs: Real World Examples using Spark and Neo4j
Transforming AI with Graphs: Real World Examples using Spark and Neo4jTransforming AI with Graphs: Real World Examples using Spark and Neo4j
Transforming AI with Graphs: Real World Examples using Spark and Neo4j
 
Graphs in Telecommunications - Jesus Barrasa, Neo4j
Graphs in Telecommunications - Jesus Barrasa, Neo4jGraphs in Telecommunications - Jesus Barrasa, Neo4j
Graphs in Telecommunications - Jesus Barrasa, Neo4j
 
Introduction to Neo4j and .Net
Introduction to Neo4j and .NetIntroduction to Neo4j and .Net
Introduction to Neo4j and .Net
 
Neo4j Graph Platform Overview, Kurt Freytag, Neo4j
Neo4j Graph Platform Overview, Kurt Freytag, Neo4jNeo4j Graph Platform Overview, Kurt Freytag, Neo4j
Neo4j Graph Platform Overview, Kurt Freytag, Neo4j
 
Supply Chain and Logistics Management with Graph & AI
Supply Chain and Logistics Management with Graph & AISupply Chain and Logistics Management with Graph & AI
Supply Chain and Logistics Management with Graph & AI
 
Real-time Analytics with Trino and Apache Pinot
Real-time Analytics with Trino and Apache PinotReal-time Analytics with Trino and Apache Pinot
Real-time Analytics with Trino and Apache Pinot
 
Scaling into Billions of Nodes and Relationships with Neo4j Graph Data Science
Scaling into Billions of Nodes and Relationships with Neo4j Graph Data ScienceScaling into Billions of Nodes and Relationships with Neo4j Graph Data Science
Scaling into Billions of Nodes and Relationships with Neo4j Graph Data Science
 
Introducing Neo4j
Introducing Neo4jIntroducing Neo4j
Introducing Neo4j
 
Danish Business Authority: Explainability and causality in relation to ML Ops
Danish Business Authority: Explainability and causality in relation to ML OpsDanish Business Authority: Explainability and causality in relation to ML Ops
Danish Business Authority: Explainability and causality in relation to ML Ops
 
NOSQLEU - Graph Databases and Neo4j
NOSQLEU - Graph Databases and Neo4jNOSQLEU - Graph Databases and Neo4j
NOSQLEU - Graph Databases and Neo4j
 
Optimizing the Supply Chain with Knowledge Graphs, IoT and Digital Twins_Moor...
Optimizing the Supply Chain with Knowledge Graphs, IoT and Digital Twins_Moor...Optimizing the Supply Chain with Knowledge Graphs, IoT and Digital Twins_Moor...
Optimizing the Supply Chain with Knowledge Graphs, IoT and Digital Twins_Moor...
 
How Graph Databases efficiently store, manage and query connected data at s...
How Graph Databases efficiently  store, manage and query  connected data at s...How Graph Databases efficiently  store, manage and query  connected data at s...
How Graph Databases efficiently store, manage and query connected data at s...
 
How Dell Used Neo4j Graph Database to Redesign Their Pricing-as-a-Service Pla...
How Dell Used Neo4j Graph Database to Redesign Their Pricing-as-a-Service Pla...How Dell Used Neo4j Graph Database to Redesign Their Pricing-as-a-Service Pla...
How Dell Used Neo4j Graph Database to Redesign Their Pricing-as-a-Service Pla...
 
Optimizing Cypher Queries in Neo4j
Optimizing Cypher Queries in Neo4jOptimizing Cypher Queries in Neo4j
Optimizing Cypher Queries in Neo4j
 

Similar to Neo4j Training Cypher

August Webinar - Water Cooler Talks: A Look into a Developer's Workbench
August Webinar - Water Cooler Talks: A Look into a Developer's WorkbenchAugust Webinar - Water Cooler Talks: A Look into a Developer's Workbench
August Webinar - Water Cooler Talks: A Look into a Developer's Workbench
Howard Greenberg
 
Driving application development through behavior driven development
Driving application development through behavior driven developmentDriving application development through behavior driven development
Driving application development through behavior driven development
Einar Ingebrigtsen
 

Similar to Neo4j Training Cypher (20)

August Webinar - Water Cooler Talks: A Look into a Developer's Workbench
August Webinar - Water Cooler Talks: A Look into a Developer's WorkbenchAugust Webinar - Water Cooler Talks: A Look into a Developer's Workbench
August Webinar - Water Cooler Talks: A Look into a Developer's Workbench
 
Part of the DLM story: Get your Database under Source Control - SQL In The City
Part of the DLM story: Get your Database under Source Control - SQL In The City Part of the DLM story: Get your Database under Source Control - SQL In The City
Part of the DLM story: Get your Database under Source Control - SQL In The City
 
Build a modern data platform.pptx
Build a modern data platform.pptxBuild a modern data platform.pptx
Build a modern data platform.pptx
 
7 steps to simplifying your AI workflows
7 steps to simplifying your AI workflows7 steps to simplifying your AI workflows
7 steps to simplifying your AI workflows
 
Oracle SQL Developer Tips and Tricks: Data Edition
Oracle SQL Developer Tips and Tricks: Data EditionOracle SQL Developer Tips and Tricks: Data Edition
Oracle SQL Developer Tips and Tricks: Data Edition
 
KYSUC - Keep Your Schema Under Control
KYSUC - Keep Your Schema Under ControlKYSUC - Keep Your Schema Under Control
KYSUC - Keep Your Schema Under Control
 
Alm with tfs 2013
Alm with tfs 2013Alm with tfs 2013
Alm with tfs 2013
 
Connections Upgrades and Migrations the Easy Way
Connections Upgrades and Migrations the Easy WayConnections Upgrades and Migrations the Easy Way
Connections Upgrades and Migrations the Easy Way
 
Connections Migrations the easy way Soccnx10
Connections Migrations the easy way Soccnx10Connections Migrations the easy way Soccnx10
Connections Migrations the easy way Soccnx10
 
Intro to SharePoint 2010 development for .NET developers
Intro to SharePoint 2010 development for .NET developersIntro to SharePoint 2010 development for .NET developers
Intro to SharePoint 2010 development for .NET developers
 
How to Use Selenium, Successfully
How to Use Selenium, SuccessfullyHow to Use Selenium, Successfully
How to Use Selenium, Successfully
 
Utilizing the open ntf domino api
Utilizing the open ntf domino apiUtilizing the open ntf domino api
Utilizing the open ntf domino api
 
DNN Summit 2021: DNN Upgrades Made Simple
DNN Summit 2021: DNN Upgrades Made SimpleDNN Summit 2021: DNN Upgrades Made Simple
DNN Summit 2021: DNN Upgrades Made Simple
 
#ESPC18 How to do #devops with the #SharePoint Framework and why it matters?
#ESPC18 How to do #devops with the #SharePoint Framework and why it matters?#ESPC18 How to do #devops with the #SharePoint Framework and why it matters?
#ESPC18 How to do #devops with the #SharePoint Framework and why it matters?
 
Rails tools
Rails toolsRails tools
Rails tools
 
2020 oct zowe quarterly webinar series
2020 oct zowe quarterly webinar series2020 oct zowe quarterly webinar series
2020 oct zowe quarterly webinar series
 
Let's get along
Let's get alongLet's get along
Let's get along
 
PostgreSQL worst practices, version PGConf.US 2017 by Ilya Kosmodemiansky
PostgreSQL worst practices, version PGConf.US 2017 by Ilya KosmodemianskyPostgreSQL worst practices, version PGConf.US 2017 by Ilya Kosmodemiansky
PostgreSQL worst practices, version PGConf.US 2017 by Ilya Kosmodemiansky
 
Driving application development through behavior driven development
Driving application development through behavior driven developmentDriving application development through behavior driven development
Driving application development through behavior driven development
 
Silicon Valley Code Camp 2016 - MongoDB in production
Silicon Valley Code Camp 2016 - MongoDB in productionSilicon Valley Code Camp 2016 - MongoDB in production
Silicon Valley Code Camp 2016 - MongoDB in production
 

More from Max De Marzi

More from Max De Marzi (20)

DataDay 2023 Presentation
DataDay 2023 PresentationDataDay 2023 Presentation
DataDay 2023 Presentation
 
DataDay 2023 Presentation - Notes
DataDay 2023 Presentation - NotesDataDay 2023 Presentation - Notes
DataDay 2023 Presentation - Notes
 
Developer Intro Deck-PowerPoint - Download for Speaker Notes
Developer Intro Deck-PowerPoint - Download for Speaker NotesDeveloper Intro Deck-PowerPoint - Download for Speaker Notes
Developer Intro Deck-PowerPoint - Download for Speaker Notes
 
Neo4j Training Modeling
Neo4j Training ModelingNeo4j Training Modeling
Neo4j Training Modeling
 
Neo4j Training Introduction
Neo4j Training IntroductionNeo4j Training Introduction
Neo4j Training Introduction
 
Detenga el fraude complejo con Neo4j
Detenga el fraude complejo con Neo4jDetenga el fraude complejo con Neo4j
Detenga el fraude complejo con Neo4j
 
Data Modeling Tricks for Neo4j
Data Modeling Tricks for Neo4jData Modeling Tricks for Neo4j
Data Modeling Tricks for Neo4j
 
Fraud Detection and Neo4j
Fraud Detection and Neo4j Fraud Detection and Neo4j
Fraud Detection and Neo4j
 
Detecion de Fraude con Neo4j
Detecion de Fraude con Neo4jDetecion de Fraude con Neo4j
Detecion de Fraude con Neo4j
 
Neo4j Data Science Presentation
Neo4j Data Science PresentationNeo4j Data Science Presentation
Neo4j Data Science Presentation
 
Neo4j Stored Procedure Training Part 2
Neo4j Stored Procedure Training Part 2Neo4j Stored Procedure Training Part 2
Neo4j Stored Procedure Training Part 2
 
Neo4j Stored Procedure Training Part 1
Neo4j Stored Procedure Training Part 1Neo4j Stored Procedure Training Part 1
Neo4j Stored Procedure Training Part 1
 
Decision Trees in Neo4j
Decision Trees in Neo4jDecision Trees in Neo4j
Decision Trees in Neo4j
 
Neo4j y Fraude Spanish
Neo4j y Fraude SpanishNeo4j y Fraude Spanish
Neo4j y Fraude Spanish
 
Data modeling with neo4j tutorial
Data modeling with neo4j tutorialData modeling with neo4j tutorial
Data modeling with neo4j tutorial
 
Neo4j Fundamentals
Neo4j FundamentalsNeo4j Fundamentals
Neo4j Fundamentals
 
Neo4j Presentation
Neo4j PresentationNeo4j Presentation
Neo4j Presentation
 
Fraud Detection Class Slides
Fraud Detection Class SlidesFraud Detection Class Slides
Fraud Detection Class Slides
 
Neo4j in Depth
Neo4j in DepthNeo4j in Depth
Neo4j in Depth
 
Bootstrapping Recommendations OSCON 2015
Bootstrapping Recommendations OSCON 2015Bootstrapping Recommendations OSCON 2015
Bootstrapping Recommendations OSCON 2015
 

Recently uploaded

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Recently uploaded (20)

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 

Neo4j Training Cypher

  • 2. github.com/maxdemarzi About 200 public repositories Max De Marzi Neo4j Field Engineer About Me ! 01 02 03 04 maxdemarzi.com @maxdemarzi About 175 blog posts
  • 3. • Relational Databases (quick version) • Graph Databases (quick version) • The most important Slide about Neo4j you will ever see • Neo4j Desktop • Setup • Introduction to Cypher • The Movies Dataset • Questions Agenda
  • 5. Orders OrderItemsOrderItems PK index {Order_num, Item_num} <m <f5 >c11 <d112 Order_num=e1234567 <e4 >e120 Products Products PK index {product_num} Manufacturer Retailer SQL Joins – using Primary Indexes 6 IO’s traversing the index (plus a few if leaf is >1 page) (do 6 times – 1 for each row) 7 IO’s traversing the index (plus a few if leaf is >1 page) 5 IO’s traversing the index (plus a few if leaf is >1 page) (do 6 times – 1 for each row) 5 IO’s traversing the index (plus a few if leaf is >1 page) (do 6 times – 1 for each row)1 outer row + 7 index + 6 inner rows + 6(6 index + 1 row) + 6(5 index + 1 row) + 6(5 index + 1) row = 128 IO’s
  • 6. Manufacturer Orders OrderItems <m <f5 >c11 <d112 Order_num=e1234567 <e4 >e120 Products Retailer Neo4j – Index Free Adjacency 6 direct pointers 1 outer node + 6 inner nodes + 6(p nodes) + 6(m nodes) + 6 (r nodes) = 25 operations 1 direct pointer 1 direct pointer 1 direct pointer -[:HAS_ITEMS]! -[:ORDERED_PRODUCT]! -[:MADE_BY]! -[:SOLD_BY]!
  • 7. 7 Native Storage and Processing Neo4j disk and memory structures link data directly, allowing millions graph traversals per second per core. Graph data and paths between data do not have to be pre-defined before they can be used.
  • 8. Property Graph Model Components Nodes • Relate nodes by type and direction • Can have Properties • Can have Labels • Can have Properties name:”Dan” born: May 29, 1970 twitter:”@dan” name:”Ann” born: Dec 5, 1975 Since: Jan 10, 2011 brand: “Volvo” model: “V70” Car LOVES LIVES_WITH D RIVES Person O W NS Relationships Person
  • 9. The Most Important Slide about Neo4j you will ever see
  • 10. Fixed Sized Records “Joins” on Creation Spin Spin Spin through this data structure Pointers instead of Searches 1 2 3 4
  • 11.
  • 13. Getting around in Neo4j Desktop & Browser
  • 14. Neo4j Desktop: UI for developers & DB management Supports “plugins” • Neo4j official plugins • Neo4j labs plugins • 3rd party plugins ✓ Will ship with free Bloom plugin for use with local databases managed by desktop only Allows you to manage local databases • Create, stop, start, manage • Add apoc, procedures, etc. • See log files, configuration, etc. Allows you to connect to remote databases • You can’t manage – but you can open browser Supports organization via “projects”
  • 15. Managing Databases Only for local databases (on desktop) Quick access to • Logs • (Configuration) Settings • Plugins • Upgrade • Administration Also quick access to file locations • Database parent directory • …or one of several common subdirectories ✓ Import (for copying csv’s for loading) ✓ Plugins ✓ Logs ✓ Configuration
  • 16. Finding logs Displays neo4j.log Clicking on neo4j.log link… • Takes you to the log directory Fast access to • Debug log • Query log • (Neo4j log) • Security log
  • 17. (Configuration) Settings Modifies neo4j.conf • Not an online operation • Makes changes and the reboots system For dynamic configurations: • Use browser • Cypher procs ✓ Call dbms.listConfig(<text pattern>) ✓ Call dbms.setConfigValue
  • 18. Plugins Fast way to install Neo4j plugins What it does • Gets correct version of plugins for Neo4j version • Installs .jar file in ./plugins folder • Modifies config file • Restarts database Note: After any upgrade • Check – if it says install – install them (upgraded ones) • You may need to delete the OLD jar first ✓ “Open Folder” ! Plugins ✓ Delete apoc*.jar • If you don’t, server will fail on boot ✓ Due to duplicate jar files
  • 19. Upgrade Automates update process Process • Downloads version selected if not in repository • Installs the version • Copies data from old version to new version • Performs upgrade If it fails…. • Since it did a copy….not a problem • Simply points back to original version
  • 20. Administration Only function is to set database password • I always use “swordfish” Alternate method for resetting password • ./bin/neo4j-admin set-initial-password
  • 21. How to get more plugins Click on Graph Applications tab • Click on “Discover more graph apps” link It will launch install.graphapp.io Pick and install Recommended: • Halin ! monitoring • Query log analyzer ! analyzing query.log • DB Analyzer ! space/disk/database monitoring • Cloud Tool ! managing cloud instances of Neo4j
  • 22. Notifications Lets you know… • If upgrades are available for any applications • If upgrades are available for desktop overall
  • 23. License keys License keys • Select “Add software key” • Copy/paste link • Only manages license keys for local database instances managed by Neo4j desktop
  • 24. Neo4j Browser In reality • Light weight web/javascript application Purpose • Cypher coding • Quick/small visualizations • Exporting result sets Limitations – only one at a time Available via your favorite web browser • Same bolt protocol & UI • Easy way to bypass the above limitation https://www.youtube.com/watch?v=oHo-lQ79zf0&feature=youtu.be
  • 25. Cypher vs. Client commands Client commands are prefaced with “:” • :exit • :use <database> • :params • :auto • …etc…. These are not cypher • They are instructions to the client utility tool ✓ Neo4j Browser, cypher-shell, …. • Which ones are supported depend on which utility and which versions The “:use” command • When issued, it disconnects/reconnects with new database name
  • 26. Recommendation of things to do immediately Enable multi-statement query editor • Or scripts with more than one query will fail ✓ ….those create constraint scripts with ;’s Clean initial command of :play start • ….annoying….. If performance is an issue • De-select “Connect result nodes” ✓ In case you wondered… ✓ After every query executes, the browser executes a separate query for each node to find any connections between them that the query didn’t explicitly expose ✓ With any complicated result set – this can slow things considerably • You can also reduce the result set size ✓ …or increase if exporting
  • 27. Node Labels/Relationship Types Displays a list of… • Node labels • Relationship types • Properties Runs a quick query • Returns the first 25 values it finds ✓ Not necessarily in any order and not necessarily the same ones each time… • Be careful with relationship types ✓ Uses a label scan….so may not be fast…might be a few seconds to return
  • 28. Saved Scripts Lets you save frequent queries • …as typical You can organize in folders • Better than trying to find the needle in haystack You can export to a zip archive • Good to do before any upgrades to desktop! No import capability • Not sure where they are saved normally • Workaround is to …. ✓ Export to zip ✓ Unzip to individual cypher queries ✓ Copy paste into browser and resave
  • 29. Documentation Quick access to links for Neo4j documentation • Links – not local – opens web browser List of browser commands Access to online resources • GraphGists • Developer site
  • 30. Query Editor in Browser Careful!!! • <ENTER> executes the query ✓ Source of a lot of syntax errors ✓ Wish this was configurable • <Shift><ENTER> for CR/LF in editor • <ESCAPE> full screen editor mode Controls • Star ! favorites ✓ If using a favorite query, it changes to a pencil to signify editing the query • Eraser ! clears query editor • Play ! executes query
  • 31. Query Execution If running for a long time • X will try to cancel… • ….but if it is the layout taking time (very often) the only way to kill is to quit the browser and reopen Controls • Download ! Export • Pin ! pins to top ✓ Useful with db.schema.visualization ✓ Also useful with outputs using as literals for next queries • Full screen mode • Collapse ! used with query plan output • Re-run ! reruns the query reusing same output window ✓ One side effect is that it immediately scrolls to top of browser unless you change setting • X/Close ! Closes window; attempts to abort query and close window if still running in database ✓ If you want to analyze query for slowness click on query text first to reload into browser before hitting X
  • 32. Query Results Graph • A rudimentary graph depiction of the results • Can be customized (colors, sizes, captions, etc.) Table • Displays output in tabular form • If return is a node or relationship, a JSON map will be displayed Text • Pure ascii text in JSON map format • Display width is configurable Code • Query text • Summary of query impact/write operations • Response: JSON structure of results
  • 33. Working with Neo4j Customer Success Team Slow query? Execute with EXPLAIN or PROFILE • EXPLAIN ! query plan only without execution ✓ Estimates only • PROFILE ! query plan & stats after execution • Adds “Plan” tab to results Sending • Expand all • Export to JSON ✓ For quick access to stats values, etc. • Export to SVG ✓ Graphic representation that can be emailed Collapse/Expand • Collapses, expands query tree as desired
  • 34. Who ya gonna call?? Sales • Overall account management ✓ Can help coordinate with anyone needed • Has pre-sales engineer for technical assistance • Can coordinate with consulting for short term engagements ✓ Or facilitate with partner for long term projects Pre-sales • Assists with pre-sales and some post-sales activity • Can come on site (if allowed and no covid-19) • Short (1 week or less) POC’s, demo’s, how-to’s • Has lots of accounts (many 10’s) – “interrupt driven” Customer Success • Best practices, architecture, review data modeling • Help with cluster setup, configuration, architecture • They don’t fix bugs – they can help you open a case though • They mainly help you when stumped with implementation problems ✓ Help with query tuning/diagnosing performance issues ✓ Help diagnose problems you are having Engineering Sales Pre-Sales Engineer (aka Field Engineer) Professional Services Customer Success Engineer (aka Field Engineer) Customer Success Manager Support Engineer Developer Relations Neo4j Community https://community.neo4j.com Community Edition Enterprise Edition Enhancementrequests Questions, Defects Enhancement requests Assigns Coordinates Short technical Knowledge Base Articles Points you to…
  • 35. Setup
  • 36. Create a new empty database instance Start Neo4j Desktop Create a new empty database • Create a local graph • Change the database name to whatever you want • Enter a password you will remember • Optional: Select the version ✓ Note that if you pick a version other than the one downloaded with desktop, desktop will need to download the specified version – which can take a while • Press “Create”….
  • 37. Starting the database server Press Start • Spawns JVM (neo4j is native java) Database goes through recovery • Reads config file • Opens database files • Plays back any missing transactions from log • Loads plugins • Etc. …. Started…. Open it
  • 40. WITH ["Jennifer","Michelle","Tanya","Julie", "Christie","Sophie","Amanda","Khloe","Sarah", "Kaylee"] AS names FOREACH (r IN range(0,100000) | CREATE (:User {username:names[r % size(names)]+r}))
  • 41. WITH ["Jennifer","Michelle","Tanya","Julie", "Christie","Sophie","Amanda","Khloe","Sarah", "Kaylee"] AS names FOREACH (r IN range(0,100000) | CREATE (:User {username:names[r % size(names)]+r}))
  • 42. PROFILE MATCH (u:User {username:”max”}) RETURN u
  • 43. CREATE INDEX ON :User(username); PROFILE MATCH (u:User {username:”max”}) RETURN u
  • 44.
  • 45. MATCH (u1:User),(u2:User) WITH u1,u2 LIMIT 5000000 WHERE rand() < 0.1 CREATE (u1)-[:FRIENDS]->(u2)
  • 47. MATCH (u:User)-[*1..4]->(c) WHERE u.username = 'max' RETURN count(DISTINCT c)
  • 48. MATCH (u:User)-[*]-(c) WHERE u.username = 'max' RETURN count(DISTINCT c)
  • 50. What is Cypher? Declarative query language • Focuses on what, not how to retrieve • E.g. optimizer figures out best approach Uses CLAUSE keywords such as • MATCH, WHERE, CREATE, UNWIND, WITH, MERGE, … Is a dataflow/pipelined language • Results of each clause in a statement flow into next as materialized Multi-statement batches supported • Each statement terminated with “;” (semi-colon) Runs in the database for the graph • Each database has its own query engine • As of 4.0, no cross-db queries (due to the above) Graph Model ! Cypher • Nodes ! nodes or node labels (depending on context) • Edges ! relationship types Foundation for ISO standard query language • For graph databases MATCH <pattern> [, <pattern> [, …]] [WHERE [NOT] <expression> <op> <expression> [[AND | OR | XOR] [NOT] …] [OPTIONAL MATCH [WHERE …]] [WITH [DISTINCT] [WHERE] [ORDER BY] [LIMIT]] [MATCH […]] [CALL [YIELDING]] RETURN [DISTINCT] [ORDER BY … [ASC|DESC]] [[SKIP] LIMIT] [UNION [ALL]] …
  • 51. What is Cypher? (cont) Similar to other declarative languages • SQL like syntax….sort of ✓ MATCH ! FROM + JOIN ✓ WHERE ! WHERE ✓ RETURN ! SELECT ✓ CREATE ! atomic insert ✓ MERGE ! upsert/merge ✓ DELETE ! delete Supports expressions, functions, procedures • Including CASE expressions • Large set of pre-defined functions/procedures • User Defined Functions (UDF) or custom stored procedures can be developed in Java ✓ Support for direct API’s into graph structure vs. sending cypher for interpretation CREATE [WITH MATCH …] MATCH [WHERE] [MERGE [ON MATCH SET] [ON CREATE SET]] [SET | REMOVE] [[DETACH] DELETE] [MERGE …] [WITH] [MATCH …]
  • 52. 52 Cypher is ASCII Art In graph model • Nodes are circles with labels ✓ nouns, objects, stuff ✓ In neo4j, the standard is to use “WordCaps” capitalization ✓ A “node” is a specific instance of a label • Relationship types are arrows with types ✓ verbs, association ✓ In neo4j, the standard is to use “ALL_CAPS” capitalization ✓ A “relationship” is a specific instance of a relationship type • Properties are attributes of either label or relationship type ✓ In neo4j, the standard is to use “camelCase” capitalization Cypher ASCII art (MATCH clause) • ( ) – [ ] -> ( ) • (label)-[relationship_type]->(label) ASCII art meets Schema • Cypher uses “labels” to identify the node/relationship type being referenced if not referring to a node/relationship from an earlier clause • (:label)-[:rel_label]->(:label) (:AccountHolder)-[:HAS_ACCOUNT]->(:Account) (:Account)-[:WITHDRAWALS]->(:Transaction)
  • 53. Cypher is ASCII Art A B C LIKESLIKES LIKES (A)-[:LIKES]->(B),(A)-[:LIKES]->(C),(B)-[:LIKES]->(C) (A)-[:LIKES]->(B)-[:LIKES]->(C)<-[:LIKES]-(A)
  • 54. Neo4j is a NoSQL/Schema-less/ “Schema-optional” DBMS • A loose schema can be created using a combination of constraints & indices ✓ E.g. You can enforce that certain properties exist (not null) and are unique, but you cannot enforce a datatype nor a relationship Classic schema components supported • Unique constraints • Node key constraints (aka “Primary Key”) – multi-column • Indexes (multiple-column) – but only on node properties • Exists constraints (aka “not null column”) Labels, Relationship Types, Property & Variable Names are Strings • Must begin with a letter (1stbase ! no; Base1 ! yes) • Cannot contain symbols – except underscore (@myvar ! no; my_var ! yes) • Can contain numbers • Can be really really really long (64k) • Exception: references to parameters begin with $ • Weird values/spaces can be escaped with backticks (`this is a valid name`) ✓ Equivalent to the “double quotes” in SQL Properties can be one of the supported datatypes Cypher & Schemas
  • 55. Unique Constraint & Node Key Unique Constraint • Only supports a single column • Useful for Nodes properties that are “identities” (username, email address, product number, serial number, etc) ✓ Creates an index automatically Node Key Constraint • Essentially, the primary key ✓ Specifies the columns (one or more) that comprise the unique attributes to identify a node • Except more than one allowed (alternate keys) • Essentially a unique + exist constraint ✓ Creates an index the same as a unique constraint does Constraint Admin • Recreating (same columns) simply updates existing one ✓ No need to do if exists() DROP CONSTRAINT in scripts • Recreating with different columns creates a second node key • Call db.constraints to see list // 3.5 Syntax CREATE CONSTRAINT ON ( city:City ) ASSERT (city.cityName, city.stateCode, city.countryCode) IS NODE KEY; DROP CONSTRAINT ON ( city:City ) ASSERT (city.cityName, city.stateCode, city.countryCode) IS NODE KEY; // 4.0 Syntax CREATE CONSTRAINT City_key ON ( city:City ) ASSERT (city.cityName, city.stateCode, city.countryCode) IS NODE KEY; DROP CONSTRAINT City_key; // See all constraints CALL db.constraints // Unique Constraint CREATE CONSTRAINT ON ( acct:DepositAccount ) ASSERT (acct.accountNumber) IS UNIQUE;
  • 56. Exists Constraints Exists Property Constraint • Equivalent to NOT NULL in SQL for column attribute • Attempting to create a node without a value for this property or removing this property fails Both Nodes & Relationship Exists Property • On a node, enforces that the property exists (and has a value) • Same thing for a relationship • No way to enforce that a relationship exists ✓ E.g. no equivalent to an Fkey!Pkey enforcement ✓ That’s because both nodes have to exist before the relationship can be created (chicken & egg problem) // 3.5 Syntax // 4.0 Syntax …is similar to previous examples CREATE CONSTRAINT ON (acct:DepositAccount) ASSERT EXISTS(acct.balance); CREATE CONSTRAINT ON ()-[er:ENTITY_RESOLUTION]-() ASSERT EXISTS(er.confidenceFactor); DROP CONSTRAINT ON (acct:DepositAccount) ASSERT EXISTS(acct.balance); DROP CONSTRAINT ON ()-[er:ENTITY_RESOLUTION]-() ASSERT EXISTS(er.confidenceFactor); // This works CREATE CONSTRAINT ON (acct:DepositAccount) ASSERT EXISTS(acct.balance); // This or anything like it doesn’t work – strictly exists() CREATE CONSTRAINT ON (acct:DepositAccount) ASSERT EXISTS(toFloat(acct.balance));
  • 57. Indices (Normal Indexes) Indexes • Created on a LABEL with properties ✓ Be careful with multiple labels • Supports multiple columns • Builds a standard B-Tree • Equivalent to Non-Unique Indexes in SQL Index for performance • Only used if LABEL is referenced in MATCH/MERGE/DELETE/ CREATE clauses ✓ When using multiple labels, mention both the desired label and ultimate parent node label ✓ Examples of this later • Used for all the usual predicates ✓ equality check: n.prop = value ✓ list membership check: n.prop IN list ✓ existence check: exists(n.prop) ✓ range search: n.prop > value ✓ prefix search: STARTS WITH ✓ suffix search*: ENDS WITH ✓ substring search*: CONTAINS *Note: effectively both of these do index leaf scans vs. B-tree traversal Index Notes • No equivalent to “Clustered” Indexing • CALL db.indexes to see indices // 3.5 Syntax // accountName property for ALL account holders is indexed CREATE INDEX ON :AccountHolder(accountName); // businessName property is only indexed for BusinessCustomer // labeled account holders (sparse indexing?) CREATE INDEX ON :BusinessCustomer(businessName); // lastName & firstName properties are only indexed for Customer // labeled account holders (sparse indexing?) CREATE INDEX ON :Customer(lastName,firstName); DROP INDEX ON :AccountHolder(accountName); DROP INDEX ON :BusinessCustomer(businessName); DROP INDEX ON :Customer(lastName,firstName); // See all indices CALL db.indexes // 4.0 Syntax CREATE INDEX AcctName_idx FOR (acct:AccountHolder) ON (acct.accountName); CREATE INDEX BusName_idx FOR (bc:BusinessCustomer) ON (bc.businessName); CREATE INDEX CustName_idx FOR (cust:Customer) ON (cust.lastName,cust.firstName); DROP INDEX AcctName_idx; DROP INDEX BusName_idx; DROP INDEX CustName_idx;
  • 58. Indices (Node Key, Unique Constraints & Index) Critical for finding starting point(s) • While it is true we don’t need indices for traversals, we do need them for unique enforcement as well as anchor points An index on :Account.accountHolderID is not need – just traverse from :AccountHolder instead Critical for using MERGE statements • MERGE statements without Indexes must Scan all nodes of a particular label
  • 59. Text Indexing Full Text Indexes • Can span more than one node • Can have multiple properties • Supports different languages, etc. • Maintained automatically • Only index available for Relationships ✓ But effectively only good for near equality matches as range, etc. won’t work correctly due to lexical sequencing vs. numeric, etc. When to use • When storing paragraphs of text to search (e.g. knowledge graphs) • Finding relationships based on String datatype property values Can be used with Linkurious • Scales better than included ElasticSearch • Supports Linkurious clustering better – and allows virtual clusters • Used for AlternateNodeID’s for retrieving visualizations saved to disk when DB id’s change (e.g. if rebuilding database each night) • Allows full query integration Usage Procedure Description Create full-text node index db.index.fulltext.createNodeIndex Create a node fulltext index for the given labels and properties. The optional 'config' map parameter can be used to supply settings to the index. Supported settings are 'analyzer', for specifying what analyzer to use when indexing and querying. Use the db.index.fulltext.listAvailableAnalyzers procedure to see what options are available. And 'eventually_consistent' which can be set to 'true' to make this index eventually consistent, such that updates from committing transactions are applied in a background thread. Create full-text relationship index db.index.fulltext.createRelationshipIndex Create a relationship fulltext index for the given relationship types and properties. The optional 'config' map parameter can be used to supply settings to the index. Supported settings are 'analyzer', for specifying what analyzer to use when indexing and querying. Use the db.index.fulltext.listAvailableAnalyzers procedure to see what options are available. And 'eventually_consistent' which can be set to 'true' to make this index eventually consistent, such that updates from committing transactions are applied in a background thread. List available analyzers db.index.fulltext.listAvailableAnalyzers List the available analyzers that the full-text indexes can be configured with. Use full-text node index db.index.fulltext.queryNodes Query the given full-text index. Returns the matching nodes and their Lucene query score, ordered by score. Use full-text relationship index db.index.fulltext.queryRelationships Query the given full-text index. Returns the matching relationships and their Lucene query score, ordered by score. Drop full-text index db.index.fulltext.drop Drop the specified index. Eventually consistent indexes db.index.fulltext.awaitEventuallyConsisten tIndexRefresh Wait for the updates from recently committed transactions to be applied to any eventually-consistent full-text indexes.
  • 60. Node/Relationship properties • Do not have to have the same datatype for the same property name on different object CREATE (:Person {name: “Jeff Tallman”, ssn: “123-45-6789”, born: date(“1900-01-01”)}) // born is a date CREATE (:Person {name: “Julius Ceasar”, born: “12 July 100 BC”, died: “15 March 44 BC”}) // born/died are strings CREATE (:Person {name: “Leonhard Euler”, born: date(“1707-04-15”), died: date(“1783-09-18”) }) // born/died are dates • Two objects do not need to have the same property list CREATE (:Customer {lastName: “Tallman”, firstName: “Jeff”}) CREATE (:Customer {businessName: “Neo4j”}) • Allows a lot of flexibility, but can cause queries to fail/break If a property doesn’t exist, the value is considered to be NULL MATCH (p:Person) WHERE p.name=“Your Name” RETURN p.name, p.born, p.died //returns NULL for “died” A unique constraint only constrains nodes where the property exists!! • Therefore a unique constraint can be created on Person.ssn even though Julius Ceasar and Leonhard Euler both lack the “ssn” property Schema-less/Schema Optional & Properties
  • 61. Storage Types • Very basic java types ✓ Number subtypes Integer and Float ✓ String ✓ Boolean ✓ The spatial type Point ✓ Temporal types: Date, Time, LocalTime, DateTime, LocalDateTime and Duration • The default type is String – you will need to cast to other types when loading ✓ toInteger(), toFloat(), date(), datetime() • Storing of arrays/lists of basic types allowed, but no maps (JSON) Structural (Dynamic) Types • ‘Dynamic’ types can be used in cypher (e.g. variables), but not stored natively ✓ Nodes, Relationships, Paths ✓ Maps (e.g. JSON) ✓ Lists/arrays of more than one homogeneous type Missing datatypes • Use a “String” or use an Array of Bytes Cypher Schema: Datatypes
  • 62. Working with date parts • Use dot notation • E.g. myDate.month, myDate.day, …. Date math • First, compute duration (e.g. duration.inDays(date1,date2)) ✓ E.g. WITH duration.inDays(account.dueDate,account.paymentDate) as myDuration • Then use dot notation much like with dates ✓ E.g. myDuration.days > 30 • Other things that work ✓ Date comparisons ! date1 [<|>|=|<=|+>] date2 This is also true of any “structure” datatype in Cypher • Node.property, Relationship.property • Map.property Date Datatypes in Cypher
  • 64. Create a new database (graph) (1) Launch browser • You will automatically be connected to the “default” database – which by default is “neo4j” Enter “:use system” • Don’t forget the colon ✓ This tells the browser it is a client command vs. cypher to send to the server • This will switch your connection to system database New databases can only be created in the system database • Along with a few other commands that are restricted to system database environment • If you forget, Neo4j will tell you
  • 65. Create a new database (graph) (2) Enter “create database movies” If it succeeds…you will still be in system db Connect to “movies” database • Type “:use movies” (note colon) …or… • Select the metadata menu (database icon) • Select movies from drop down Prompt should read “movies”
  • 66. Load the Movies data (1) Use the command • :play movies Pin the result pane Press “NEXT” Button (>)
  • 67. Load the Movies data (2) Click on the code block • Copies queries to editor View cypher • Click in editor • Hit ESCAPE key • Scroll around a bit and look at the cypher ✓ We will discuss the CREATE verbs later • Hit ESCAPE to exit full screen editor NOTE: • There is only 1 semi-colon (line 508) • Yes – this IS one big Cypher statement ✓ Not multiple statements …..ONE statement ✓ Hundreds of clauses – one statement ✓ One statement ! it has to be one transaction
  • 68. Load the Movies data (3) Execute the cypher • Creates 171 nodes of 2 LABELS • Creates 253 relationships in 6 relationship types View results • Default is graph mode due to RETURN statement • Select table button • Select text button • Re-select graph button
  • 69. Style the results (1) Go full screen mode • This can be done in normal pane mode as well Select the Person label at top left • Notice the bottom edge is now a style guide with colors, sizes and caption properties Select a new color Repeat for Movies
  • 70. Style the results (2) Select the DIRECTED relationship type Select a new width for line • Not too wide Select a new color for line …this is visualization in most primitive form • …but it does provide fast visual acuity for looking at results when in graph form Press expand icon to reshrink pane
  • 72. When there’s lots (continued)…..
  • 73. Simple MATCH statements Go to next page Select & execute each statement • One at a time NOTE: 3rd (& 4th) query results are tabular • Any query that returns a “node” defaults to graph mode • Any query that doesn’t return a node, but rather a list of properties defaults to tabular mode