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:

  • A SPARQL query language interpreter
  • Tools and APIs for directly loading semantic data encoded in a variety of RDF triple formats
  • A set of REST endpoints for interacting and querying stored RDF triples
  • Tight integration of this with MarkLogic’s document store, indexes, and search and analytics APIs

Prerequisites

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:

    • Have installed MarkLogic and the mlcp tool.
    • Are generally familiar with MarkLogic architectural concepts and terms.
    • Maintain some fluency in XQuery or JavaScript and can use Query Console.

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

  • The necessary datasets and utility scripts for loading the data, along with
  • A set of Query Console workspaces that contain the queries used by each exercise

NOTE: This is not a SPARQL language tutorial or an introduction to the Semantic Web.

Semantics: “Hello World”

In this exercise, you will:

  1. Insert RDF triples into a database
  2. Query the triples using SPARQL

This is a highly-simplified example. We’ll break some rules, but that’s OK!

Enable the Triple Index

  1. Point your browser to the MarkLogic Administrative Interface on http://localhost:8001(replace localhost with the hostname).
  2. Choose the Documents database in the Databases section.
  3. In the Configure tab, scroll down the page until you see the triple index option.
  4. If the value is 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.
  5. Click the ok button at the top of the page to save any changes.

Inserting Triples

  1. Point your browser to http://localhost:8000 (Replace 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.
  2. Remove any code from the text box.
  3. Paste this into the text box and hit “run”:
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

Verify Triple Count

Check that you have some triples.

  1. In Query Console, click “+” to open a new tab.
  2. Select XQuery or JavaScript as a Query Type and remove any code from the text box.
  3. Paste this query into the text box and hit “run”:
fn:count( cts:triples() )
fn.count( cts.triples() );

It should return 3.

Run a SPARQL Query

Let’s run a query that asks “Who lives in London?”.

  1. In Query Console, click “+” to open a new tab.
  2. Select XQuery or JavaScript as a Query Type and remove any code from the text box.
  3. Paste this query into the text box and hit “run”:
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>

Next Steps

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.

Learn More

Semantic Technical Resources

View the technical resources involving Semantics in MarkLogic.

Documentation

Read the Semantic Graph Developer’s Guide to learn how to load, query, and work with semantic graph data in MarkLogic Server.

Using MarkLogic Semantics

Use RDF data in your MarkLogic applications. Learn to create, manage, index and query triples in order to build applications.

This website uses cookies.

By continuing to use this website you are giving consent to cookies being used in accordance with the MarkLogic Privacy Statement.