0% found this document useful (0 votes)
32 views

CS343 Querying Graph

This document discusses querying graphs using Gremlin and Cypher query languages. It provides examples of using MATCH, RETURN, and WITH clauses to query patterns in a graph and return nodes and relationships. Key graph query concepts covered include traversing graphs, expressing patterns and relationships between nodes, and filtering and sorting result sets.

Uploaded by

razi haider
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

CS343 Querying Graph

This document discusses querying graphs using Gremlin and Cypher query languages. It provides examples of using MATCH, RETURN, and WITH clauses to query patterns in a graph and return nodes and relationships. Key graph query concepts covered include traversing graphs, expressing patterns and relationships between nodes, and filtering and sorting result sets.

Uploaded by

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

CS343 Graph Data Science

Spring 2024

Querying Graph – Part 1


Chapter 3, Ian Robinson
Chapter 3, Tomaz

Muhammad Qasim Pasta


[email protected]

Slides are intended to be filled during the lectures. Certain details are intentionally omitted for in-class discussions. These slides are not meant to be used as reading material.
Courses: Graph Model
Graph Query Languages: Gremlin vs Cypher

• Gremlin:
– Part of the Apache TinkerPop graph computing framework
– Compatible with various graph databases.
– A graph traversal language with an imperative style.
– User specifies the sequence of steps to traverse the graph and retrieve information.
Graph Query Languages: Gremlin vs Cypher

• Cypher:
– Associated with Neo4j
– Declarative Language
– User define patterns to find in the graph
– focuses on expressing relationships and patterns between nodes
Patterns as ASCII Art

(emil)<-[:KNOWS]-(jim)-[:KNOWS]->(ian)-[:KNOWS]->(emil)
Patterns as ASCII Art

• Generate graph for the following patterns:

• (ahmed) –[:KNOWS]->(basit)<-[:KNOWS]-(bilal)

• (ahmed) –[:WORKS]->(PNS)<-[:LOCATED]-(KARACHI)

• (:Person{name:”Karim”}) -[:DRIVES]->(:Car{number:”ABC123”})
Cypher Clauses:

• MATCH: Specifies the patterns to match in the graph. It is used to find nodes, relationships, and paths
that meet certain criteria.
• OPTIONAL MATCH: Similar to MATCH, but it allows for patterns that may not exist, and it does not
affect the overall query result if the pattern is not matched.
• RETURN: Specifies what data to include in the result set. It is used to define the structure of the
output, including nodes, relationships, properties, etc.
• WHERE: Filters the results based on specified conditions. It is used to include only the data that
satisfies the specified criteria.
• WITH: Breaks the query into multiple parts. It is used to pass results from one part of the query to
another, allowing for more complex queries.
• ORDER BY: Sorts the result set based on specified criteria. It is used to control the order of the output.
• LIMIT: Limits the number of results returned by the query. It is used to reduce the size of the result
set.
• SKIP: Skips a specified number of results in the result set. It is often used in conjunction with LIMIT for
pagination.
Movie Database
RETURN

WITH 10 as x
RETURN x

WITH 10 as x, 20 as y
RETURN x,y

WITH 10 as x, 20 as y
RETURN *
MATCH

Specifies the patterns to match in the graph. It is used to find nodes, relationships, and
paths that meet certain criteria.

MATCH (n)
RETURN n

MATCH (p:Person)
RETURN p

MATCH (p:Person {name: 'Tom Hanks'})


RETURN p

MATCH (p:Person {name: 'Tom Hanks’})


RETURN p.born
MATCH

MATCH (p:Person {name: 'Tom Hanks'})-->()


RETURN *

MATCH (p:Person {name: 'Tom Hanks'})-->(m)


RETURN *

MATCH (p:Person {name: 'Tom Hanks'})-[:ACTED_IN]->(m)


RETURN *

You might also like