Introduction to Knowledge Graphs
Knowledge graphs capture relationships between entities of the world. A method for modelling knowledge graphs is the Resource Description Framework (RDF). In RDF, the relationships between the entities of a graph are captured via human- and machine-readable subject-predicate-object triples, which are also called statements. An example of a knowledge graph is provided in the image below.1 Note that each edge between two vertices of the graph represents a statement (e.g., "Bob knows Alice" and "The Mona Lisa is an artwork").
Data Formats
There are multiple data formats defined for the serialization of RDF knowledge graphs.
In the example provided below, which corresponds to the knowledge graph of the previous image, Turtle is used.
RDF leverages IRIs to ensure that entities are globally identifiable.
In this example, the IRI http://dbpedia.org/resource/Mona_Lisa (or dbr:Mona_Lisa
) is used for representing the painting of Mona Lisa in the knowledge graph.
@prefix ex: <http://example.org/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix dbr: <http://dbpedia.org/resource/> .
@prefix dbo: <http://dbpedia.org/ontology/> .
ex:alice
a dbo:Person ;
foaf:name "Alice Example" .
ex:bob
a dbo:Person ;
dbo:birthDate "1990-07-04"^^xsd:date ;
foaf:name "Bob Example" ;
foaf:knows ex:alice ;
foaf:topic_interest dbr:Mona_Lisa .
dbr:Mona_Lisa
a dbo:Work ;
rdfs:label "Mona Lisa"@en ;
dbo:author dbr:Leonardo_da_Vinci .
dbr:Leonardo_da_Vinci
a dbo:Person ;
foaf:name "Leonardo da Vinci" .
<http://en.wikipedia.org/wiki/Mona_Lisa>
a dbo:Document ;
foaf:primaryTopic dbr:Mona_Lisa .
Querying
SPARQL is the query language for RDF. Examples of SPARQL queries are provided below.
The following SPARQL query finds the names of all people in the above knowledge graph.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name WHERE {
?person a dbo:Person ;
foaf:name ?name .
}
The results to the previous query are provided below in TSV format.
?name
"Leonardo da Vinci"
"Bob Example"
"Alice Example"
The following SPARQL query finds the name of the person who created the paining of Mona Lisa.
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?name WHERE {
dbr:Mona_Lisa dbo:author ?person .
?person foaf:name ?name .
}
The results to the previous query are provided below in TSV format.
?name
"Leonardo da Vinci"
Further Reading
-
Based on the example from RDF Primer ↩