MarkLogic Semantics is a great data format for storing metadata, improving data integration, and building applications using that integrated, highly connected data. MarkLogic Semantic technologies and APIs include:
This tutorial contains a series of exercises to introduce MarkLogic Semantic features, with a number of challenges for you to work through. We assume you:
The first exercise is a simple "Hello World"
style introduction. The second has you go through the necessary steps to load the data needed by the subsequent exercises, which include some basic use of SPARQL, use of related XQuery and/or JavaScript APIs and the underlying triple index.
For your convenience in working through the exercises, we’ve provided a download-able semantics-exercises.zip (~5 MB) file that includes
NOTE: This is not a SPARQL language tutorial or an introduction to the Semantic Web.
In this exercise, you will:
This is a highly-simplified example. We’ll break some rules, but that’s OK!
localhost
with the hostname).Documents
database in the Databases section.triple index
option.false
, click the radio button for true
to enable the triple index. If the value is already true
, there is no need to change it.localhost
with the hostname of your MarkLogic server if necessary). If you’re doing this from a fresh install, Query Console will be pointing at the Documents
database (look at the top left in the dropdown). If the Query Type is set to the default, JavaScript
, the text box shows a single line with a JavaScript comment. If you change the Query Type to XQuery
a “hello world” XQuery example displays in the text area. Both XQuery and JavaScript examples are given so select which one you would like in the Query Type dropdown.import module namespace sem = "http://marklogic.com/semantics" at "/MarkLogic/semantics.xqy"; sem:rdf-insert( ( sem:triple( sem:iri("http://example.org/marklogic/people/John_Smith"), sem:iri("http://example.org/marklogic/predicate/livesIn"), "London" ) , sem:triple( sem:iri("http://example.org/marklogic/people/Jane_Smith"), sem:iri("http://example.org/marklogic/predicate/livesIn"), "London" ) , sem:triple( sem:iri("http://example.org/marklogic/people/Jack_Smith"), sem:iri("http://example.org/marklogic/predicate/livesIn"), "Glasgow" ) ) )
declareUpdate(); const sem = require('/MarkLogic/semantics.xqy'); sem.rdfInsert( [ sem.triple( sem.iri('http://example.org/marklogic/people/John_Smith'), sem.iri('http://example.org/marklogic/predicate/livesIn'), 'London' ), sem.triple( sem.iri('http://example.org/marklogic/people/Jane_Smith'), sem.iri('http://example.org/marklogic/predicate/livesIn'), 'London' ), sem.triple( sem.iri('http://example.org/marklogic/people/Jack_Smith'), sem.iri('http://example.org/marklogic/predicate/livesIn'), 'Glasgow' ) ] );
Here, you are inserting 3 triples. Each triple is of the form (“subject”, “predicate”, “object”) where the subject and predicate are IRIs (like URIs, but internationalized) and the object is, in this case, a simple string. It will generate output, something like:
/triplestore/b2a859731e9449c1.xml
Check that you have some triples.
fn:count( cts:triples() )
fn.count( cts.triples() );
It should return 3
.
Let’s run a query that asks “Who lives in London?”.
import module namespace sem = "http://marklogic.com/semantics" at "/MarkLogic/semantics.xqy"; sem:sparql(' SELECT ?person WHERE { ?person <http://example.org/marklogic/predicate/livesIn> "London" } ')
const sem = require('/MarkLogic/semantics.xqy'); sem.sparql( 'SELECT ?person WHERE { ?person <http://example.org/marklogic/predicate/livesIn> "London" }' );
It should return:
<http://example.org/marklogic/people/Jane_Smith> <http://example.org/marklogic/people/John_Smith>
If you want to work through more examples using Semantics, or simply learn more about it, there are a good number of online resources for learning SPARQL, including:
As well, we recommend the following books:
And, of course, there is the W3C SPARQL spec and their published Glossary of Linked Data terms. Again, we assume you can learn SPARQL syntax elsewhere.
By continuing to use this website you are giving consent to cookies being used in accordance with the MarkLogic Privacy Statement.