Now that we've explored the Blazegraph Workbench, let's do something more interesting: query a graph.
To keep things simple, we'll build a tiny Avengers knowledge graph and use it to learn how the Query tab works.
Don't worry if you don't understand every detail yet. The goal here is simply to become comfortable running SPARQL queries and interpreting the results.
Step 1: Load Some Avengers Data
Open the Update tab and insert the following RDF data:
Avengers.turtle
PREFIX : <http://example.org/marvel/> # Teams :Avengers a :Team ; :name "Avengers" . :Guardians_of_the_Galaxy a :Team ; :name "Guardians of the Galaxy" . # Heroes :Iron_Man a :Superhero ; :name "Iron Man" . :Captain_America a :Superhero ; :name "Captain America" . :Thor a :Superhero ; :name "Thor" . :Hulk a :Superhero ; :name "Hulk" . :Black_Widow a :Superhero ; :name "Black Widow" . :Star_Lord a :Superhero ; :name "Star-Lord" . # People :Tony_Stark a :Person ; :name "Tony Stark" ; :alias :Iron_Man ; :memberOf :Avengers ; :portrayedBy :Robert_Downey_Jr . :Steve_Rogers a :Person ; :name "Steve Rogers" ; :alias :Captain_America ; :memberOf :Avengers ; :portrayedBy :Chris_Evans . :Bruce_Banner a :Person ; :name "Bruce Banner" ; :alias :Hulk ; :memberOf :Avengers ; :portrayedBy :Mark_Ruffalo . :Natasha_Romanoff a :Person ; :name "Natasha Romanoff" ; :alias :Black_Widow ; :memberOf :Avengers ; :portrayedBy :Scarlett_Johansson . :Peter_Quill a :Person ; :name "Peter Quill" ; :alias :Star_Lord ; :memberOf :Guardians_of_the_Galaxy ; :portrayedBy :Chris_Pratt . # Actors :Robert_Downey_Jr a :Actor ; :name "Robert Downey Jr." . :Chris_Evans a :Actor ; :name "Chris Evans" . :Mark_Ruffalo a :Actor ; :name "Mark Ruffalo" . :Scarlett_Johansson a :Actor ; :name "Scarlett Johansson" . :Chris_Pratt a :Actor ; :name "Chris Pratt" . # Villains :Thanos a :Villain ; :name "Thanos" . :Loki a :Villain ; :name "Loki" . # Relationships :Avengers :foughtAgainst :Thanos . :Avengers :foughtAgainst :Loki . :Guardians_of_the_Galaxy :foughtAgainst :Thanos . :Iron_Man :friendOf :Captain_America . :Thor :friendOf :Hulk . # Movies :Avengers_Endgame a :Movie ; :title "Avengers: Endgame" ; :releasedIn 2019 . :Infinity_War a :Movie ; :title "Avengers: Infinity War" ; :releasedIn 2018 . :Tony_Stark :appearsIn :Infinity_War ; :appearsIn :Avengers_Endgame . :Steve_Rogers :appearsIn :Infinity_War ; :appearsIn :Avengers_Endgame . :Thanos :appearsIn :Infinity_War ; :appearsIn :Avengers_Endgame .
Select the type as ‘RDF Data’ and format a Turtle, and click on Update button to insert the data.
Understanding Your First RDF Turtle File
a. What is PREFIX?
The first line is:
PREFIX : <http://example.org/marvel/>
This declares a namespace. Think of it as a shortcut. Without the prefix declaration, we'd have to write following line, every time we wanted to refer to the Avengers.
<http://example.org/marvel/Avengers>
That would become very tedious. Instead, we declare:
PREFIX : <http://example.org/marvel/>
and then simply write:
:Avengers
The colon (:) means, use the default namespace defined above.
:Avengers is shorthand for <http://example.org/marvel/Avengers> :Team is shorthand for <http://example.org/marvel/Team> :name is shorthand for <http://example.org/marvel/name>
b. What is the Comment?
This line "# Teams" is a comment. Anything after a hash symbol (#) is ignored by RDF parsers. Comments are useful for organizing RDF files and helping humans understand the data.
c. Understanding the First Triple
Now let's look at:
:Avengers a :Team ;
:name "Avengers" .
This describes the Avengers.
:Avengers is the subject. The subject is the thing we're talking about.
c.1 What Does a Mean?
This is probably the most confusing part for beginners. The keyword "a" is a special Turtle shortcut.
It means "rdf:type", So
:Avengers a :Team
is exactly the same as
:Avengers rdf:type :Team
And in plain English it means "Avengers is a Team".
c.2 What Does :name Mean?
The next line is:
:name "Avengers"
Here :name is the predicate (relationship) and "Avengers" is the object (value).
In plain English, Avengers has the name "Avengers".
c.3 Why Is There a Semicolon (;)?
Notice this:
:Avengers a :Team ;
:name "Avengers" .
This is actually shorthand. Without the semicolon, we'd write:
:Avengers a :Team . :Avengers :name "Avengers" .
Notice how :Avengers is repeated. Since both triples have the same subject, Turtle allows us to write:
:Avengers a :Team ;
:name "Avengers" .
The semicolon means:
"Keep using the same subject."
c.4 Why Is There a Period (.)?
Every RDF triple ends with a dot (.), the period means "this statement is complete". It's similar to the full stop at the end of an English sentence.
Step 2: Sparql Examples
This Marvel RDF dataset contains:
· Classes (:Person, :Actor, :Movie, :Team)
· Relationships (:memberOf, :alias, :portrayedBy)
· Literals (:name, :title, :releasedIn)
· Multiple connected nodes
Below are beginner-friendly SPARQL examples, progressing from simple to more advanced.
1. Get All People
Equivalent SQL thinking:
SELECT * FROM Person
SPARQL:
PREFIX : <http://example.org/marvel/>
SELECT ?person
WHERE {
?person a :Person .
}
2. Get Names of All People
PREFIX : <http://example.org/marvel/>
SELECT ?name
WHERE {
?person a :Person ;
:name ?name .
}
You will see following results.
Bruce Banner Natasha Romanoff Peter Quill Steve Rogers Tony Stark
3. Get Person and Their Alias
PREFIX : <http://example.org/marvel/>
SELECT ?person ?hero
WHERE {
?person :alias ?hero .
}
You will see following results.
person hero <http://example.org/marvel/Natasha_Romanoff> <http://example.org/marvel/Black_Widow> <http://example.org/marvel/Steve_Rogers> <http://example.org/marvel/Captain_America> <http://example.org/marvel/Bruce_Banner> <http://example.org/marvel/Hulk> <http://example.org/marvel/Tony_Stark> <http://example.org/marvel/Iron_Man> <http://example.org/marvel/Peter_Quill> http://example.org/marvel/Star_Lord
4. Show Human-Friendly Names
PREFIX : <http://example.org/marvel/>
SELECT ?personName ?heroName
WHERE {
?person :name ?personName ;
:alias ?hero .
?hero :name ?heroName .
}
You will see following results.
personName heroName Natasha Romanoff Black Widow Steve Rogers Captain America Bruce Banner Hulk Tony Stark Iron Man Peter Quill Star-Lord
5. Find All Avengers Members
Graph pattern:
PREFIX : <http://example.org/marvel/>
SELECT ?name
WHERE {
?person :memberOf :Avengers ;
:name ?name .
}
You will see following results.
name Bruce Banner Natasha Romanoff Steve Rogers Tony Stark
6. Find Which Team Each Person Belongs To
PREFIX : <http://example.org/marvel/>
SELECT ?personName ?teamName
WHERE {
?person :name ?personName ;
:memberOf ?team .
?team :name ?teamName .
}
You will see following results.
personName teamName Bruce Banner Avengers Natasha Romanoff Avengers Steve Rogers Avengers Tony Stark Avengers Peter Quill Guardians of the Galaxy
7. Find Actors Who Played Characters
PREFIX : <http://example.org/marvel/>
SELECT ?character ?actor
WHERE {
?person :name ?character ;
:portrayedBy ?actorNode .
?actorNode :name ?actor .
}
You will see following results.
character actor Steve Rogers Chris Evans Peter Quill Chris Pratt Bruce Banner Mark Ruffalo Tony Stark Robert Downey Jr. Natasha Romanoff Scarlett Johansson
8. Find Villains
PREFIX : <http://example.org/marvel/>
SELECT ?villainName
WHERE {
?villain a :Villain ;
:name ?villainName .
}
You will see following results.
villainName Loki Thanos
9. Find Who Avengers Fought Against
PREFIX : <http://example.org/marvel/>
SELECT ?villainName
WHERE {
:Avengers :foughtAgainst ?villain .
?villain :name ?villainName .
}
You will see following results.
villainName Loki Thanos
10. Find All Friendships
PREFIX : <http://example.org/marvel/>
SELECT ?hero1 ?hero2
WHERE {
?hero1 :friendOf ?hero2 .
}
You will see following results.
hero1 hero2 <http://example.org/marvel/Iron_Man> <http://example.org/marvel/Captain_America> <http://example.org/marvel/Thor> <http://example.org/marvel/Hulk>
11. Get Friend Names
PREFIX : <http://example.org/marvel/>
SELECT ?friend1 ?friend2
WHERE {
?hero1 :friendOf ?hero2 .
?hero1 :name ?friend1 .
?hero2 :name ?friend2 .
}
You will see following results.
friend1 friend2 Iron Man Captain America Thor Hulk
12. Find All Movies
PREFIX : <http://example.org/marvel/>
SELECT ?title ?year
WHERE {
?movie a :Movie ;
:title ?title ;
:releasedIn ?year .
}
You will see following results.
title year Avengers: Endgame 2019 Avengers: Infinity War 2018
13. Movies Released After 2018
PREFIX : <http://example.org/marvel/>
SELECT ?title ?year
WHERE {
?movie a :Movie ;
:title ?title ;
:releasedIn ?year .
FILTER(?year > 2018)
}
You will see following results.
title year Avengers: Endgame 2019
14. Find Characters Appearing in Infinity War
PREFIX : <http://example.org/marvel/>
SELECT ?name
WHERE {
?character :appearsIn :Infinity_War ;
:name ?name .
}
You will see following results.
name Steve Rogers Thanos Tony Stark
15. Count Number of Avengers
Aggregation example:
PREFIX : <http://example.org/marvel/>
SELECT (COUNT(?person) AS ?memberCount)
WHERE {
?person :memberOf :Avengers .
}
You will see following results.
memberCount 4
16. Count Members Per Team
Similar to SQL GROUP BY.
PREFIX : <http://example.org/marvel/>
SELECT ?teamName (COUNT(?person) AS ?members)
WHERE {
?person :memberOf ?team .
?team :name ?teamName .
}
GROUP BY ?teamName
You will see following results.
teamName members Avengers 4 Guardians of the Galaxy 1
17. Find Everything About Tony Stark
Very useful debugging query.
PREFIX : <http://example.org/marvel/>
SELECT ?predicate ?object
WHERE {
:Tony_Stark ?predicate ?object .
}
You will see following results.
predicate object <http://example.org/marvel/alias> <http://example.org/marvel/Iron_Man> <http://example.org/marvel/appearsIn> <http://example.org/marvel/Avengers_Endgame> <http://example.org/marvel/appearsIn> <http://example.org/marvel/Infinity_War> <http://example.org/marvel/memberOf> <http://example.org/marvel/Avengers> <http://example.org/marvel/name> Tony Stark <http://example.org/marvel/portrayedBy> <http://example.org/marvel/Robert_Downey_Jr> rdf:type <http://example.org/marvel/Person>
18. "Join" Example (Most Important)
This demonstrates SPARQL joins through shared variables.
PREFIX : <http://example.org/marvel/>
SELECT ?personName ?heroName ?actorName
WHERE {
?person :name ?personName ;
:alias ?hero ;
:portrayedBy ?actor .
?hero :name ?heroName .
?actor :name ?actorName .
}
You will see following results.
personName heroName actorName Steve Rogers Captain America Chris Evans Peter Quill Star-Lord Chris Pratt Bruce Banner Hulk Mark Ruffalo Tony Stark Iron Man Robert Downey Jr. Natasha Romanoff Black Widow Scarlett Johansson
Previous Next Home


No comments:
Post a Comment