Prerequisites
The prerequisites for running this query graph example are listed below:- Graph server enabled
- KiSQL or other SQL client
- SQL query graph script
Overview
This example is going to demonstrate querying a cinema-based graph of relationships between actors, directors, & shows for:-
directors and titles of movies in which a given actor has acted, using a
hop-based search
- the same query, but applying a fuzzy search, employing Kinetica’s full text search capability via the FILTER_BY_STRING function
- the same query, but applying a SQL join to the results of a graph query to supplement the result set
- titles & release years of movies directed by directors of movies in which a given actor has acted, demonstrating the nesting of graph queries
Table Setup
Three source data tables will be used to construct the graph:qsgs_person- contains actors, directors, & producersqsgs_show- contains the genre, release date, & score of movies & TV showsqsgs_role- contains the relationships (production roles) between the people and the shows on which they have worked; e.g., X acted in show Y or A directed show B
Person Table
This table contains the name, semicolon-separated list of production roles, and date of birth of actors, directors, & producers used in this example. The list of production roles will be used later in graph creation to associate all of a given person’s career-wide production roles with them, not just their production role on any particular show.Create Person Table
Populate Person Table
Show Table
This table contains the title, show type (movie or TV series), semicolon-separated list of genres, year of release, and score for the shows used in this example. The list of genres will be used in graph creation to associate each of the genres with the corresponding show.Create Show Table
Populate Show Table
Production Role Table
This table contains the production role each person had on each show within the example data set. The production role values are a semicolon-separated list of roles, each of which will be applied to the relation between the person and the show within the graph.Create Production Role Table
Populate Production Role Table
Graph Construction
This example will demonstrate both constructing a graph from source tables and amending the graph with additional data after construction.Creation
One graph is used for these query graph examples,qsgs_cinema, which will
use several queries against the source tables to build the edges, nodes, &
labels. The graph is created with the CREATE GRAPH command:
Create Cinema Graph
- show node - represents a movie or TV show, labeled with the type of show it is and each genre it encompasses
- release year node - represents the release year of the connected show
- person node - represents an actor, director, or producer; labeled with
personto indicate a person node, as well as each profession the person has held - birth year node - represents the date of birth of the connected person
- participation edge - represents the connection of a person to a show, labeled with the production role of the person in the show
- released edge - represents the connection of a show to the year it was
released; labeled with
releasedto indicate it as a released edge - born edge - represents the connection of a person to the year of birth;
labeled with
bornto indicate it as a born edge
- It is directed, depicting relationships between people and the shows on which they have worked as person-to-show; e.g., X directed show Y. Since all edges are directed, the other edge types will follow suit; e.g., X was born in year Y and X was released in year Y.
- It has no
weightsbecause this example doesn’t favor any production roles or linked attributes over others - It has no inherent
restrictionsfor any of the nodes or edges in the graph - It will use
:as the delimiter when parsing each of the node & edge labels upon creation - It will have a corresponding table,
qsgs_cinema_graph_table, created for it containing its edges andqsgs_cinema_graph_table_nodescreated for it containing its node endpoints
Alteration
The created graph can have data (nodes, edges, & labels) added to it after creation, as demonstrated with the ALTER GRAPH command:Alter Cinema Graph
CREATE GRAPH & ALTER GRAPH.
Though not required for supplementing the graph data, a backing record for
Stargate is added to the show table for use in a supplemental data query later
in this guide.
Insert Supplemental Data
Querying
This example will demonstrate four ways graph queries can be used to answer relationship questions:Hop-Based
To find the directors and titles of movies (no TV series) in which James Spader has acted:Hop Query
-
Start with James Spader.
NODE_NAMEofJames Spader
-
Find shows in which he has acted.
HOP_IDof1with anEDGE_LABELofacted
-
Isolate the shows to just movies.
HOP_IDof1with aNODE_LABELofmovie
-
Find directors of those movies.
HOP_IDof-2with anEDGE_LABELofdirected; the negative sign indicates the query should be allowed to traverse these edges of the directed graph in reverse—from show to person, whereas the edge is defined topologically as one-way, from person to show.
-
Orient the director/show relationships in the second hop with the director on
the right-hand side (as the target node).
TARGET_NODE_LABELofdirector
-
Stop the query at this second hop.
RINGSof2
-
Select only the second hop entries (the director-to-movie relationships in
which we are interested), filtering out the first hop entries (connecting
James Spader to these movies), which are otherwise returned as part of the
queried path.
WHERE RING_ID = 2
-
Extract the director & title of the movies from the query results.
QUERY_NODE2_NAMErepresents the director (as it is the target node) andQUERY_NODE1_NAMEthe movie in the director-to-movie directed edge relation.
Supplemental Functions
To perform the same query as in Query by Hops above, but employing the Kinetica full text search capability using the FILTER_BY_STRING function to find the same James Spader starting node:Fuzzy Search Query
-
Perform a fuzzy search on the person table for a name like James Spdar
FILTER_BY_STRINGon theqsgs_persontable using a fuzzysearchforJames Spdaracross all of its columns
-
Start with the name returned by the fuzzy search
NODE_NAMEof the value in thenamecolumn of theqsgs_persontable that matchedJames Spdar
- Perform the rest of the query the same way as was done in Query by Hops
Supplemental Data
To perform the same query as in Query by Hops above, but employing a SQL join to supplement the query results with movie scores from the show table:Supplemental Data Query
- Perform the query the same way as was done in Query by Hops
-
Associate the movies returned by the query with their corresponding entries in
the show table, matching by movie title
JOIN qsgs_show s ON s.title = QUERY_NODE1_NAME
-
Add each movie’s score to the results
s.score AS Score
Nested Graph Queries
To perform the same query as in Query by Hops above, but nesting it within another graph query to find the titles and release years of all shows directed by directors of James Spader movies:Nested Graph Queries
-
Perform an inner graph query the same way as was done in
Query by Hops, with three modifications:
-
Find shows directed by directors of James Spader movies.
HOP_IDof3with anEDGE_LABELofdirected; move from the directors of James Spader movies returned by the original graph query to all of the shows those directors have directed
-
Orient the director/movie relationships in the third hop with the movie on
the right-hand side (as the target node).
TARGET_NODE_LABELofmovie
-
Stop the query after this additional hop, from directors to shows
RINGSof3
-
Find shows directed by directors of James Spader movies.
-
Perform an outer graph query on the list of shows returned by the inner graph
query, finding their associated release dates
-
Start with the names of shows returned by the inner graph query
NODE_NAMEofQUERY_NODE2_NAME(inner query result show’s name)
-
Find release years of those shows
TARGET_NODE_LABELofyear
-
Ensure the release years found are directly connected to the respective
shows
RINGSof1(only go one hop away from the show nodes)
-
Extract the title and release year of the shows from the query results
QUERY_NODE1_NAMErepresents the show title andQUERY_NODE2_NAMEthe show release year in the show-to-release-year directed edge relation
-
Start with the names of shows returned by the inner graph query
Download & Run
Included below is a complete example containing all the above requests and corresponding output. The script can be run via Workbooks in Workbench or any SQL client. To execute using KiSQL, download the SQL script, switch to the directory in which it has been downloaded, and run:Run Example