SPARQL 1.1 Update – examples – Graph Management

These examples use a small, simple data set loosely based on the examples in "Semantic Web for the Working Ontologist" by Dean Allemang and James Hendler. They assume the following prefixes:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX prod: <http://example.com/products/>
PREFIX ex: <http://example.com/>

The data is about products in a clothing factory.

The examples can be run from Query Console, in SPARQL Update mode.

SPARQL query results are shown here in JSON format, for convenience.

To check which graphs exist in your database:

  1. CREATE
  2. Example 1:

    ## reset:
    ## drop all graphs used in this exercise
    ## use "SILENT" so it doesn't give an error if the graphs don't exist
    DROP SILENT GRAPH <graph-1> ;
    DROP SILENT GRAPH <graph-2> ;
    DROP SILENT GRAPH <graph-3> ;

    Effect:

    ## show me all graphs using curl
    ## run this from the command line
    ## substitute your username, password, hostname, and the REST port
    curl --anyauth --user user:password -i -X GET 'http://localhost:9805/v1/graphs'
    =>
    Nothing

    Example 2:

    ## create <graph-1>
    CREATE GRAPH <graph-1> ;

    Example 3:

    ## insert some triples into <graph-1>INSERT DATA
    {
      GRAPH <graph-1>
      {
        prod:1001 rdf:type ex:Henley ;
        ex:color "blue" .
        prod:1002 rdf:type ex:Shirt ;
        ex:color "red" .
     }
    }
    ;

    Effect:

    ## show me all graphs using curl
    ## run this from the command line
    ## substitute your username, password, hostname, and the REST port
    curl --anyauth --user user:password -i -X GET 'http://localhost:9805/v1/graphs'
    =>
    graph-1
  3. COPY
  4. Example 1:

    ## copy <graph-1> into <graph-2>
    COPY GRAPH <graph-1> TO <graph-2>
    =>
    Empty

    Effect:

    ## show me all graphs using curl
    ## run this from the command line
    ## substitute your username, password, hostname, and the REST port
    curl --anyauth --user user:password -i -X GET 'http://localhost:9805/v1/graphs'
    =>
    graph-1
    graph-2
    ## show me all triples in the graph <graph-2>
    SELECT ?s ?p ?o
    FROM <graph-2>
    WHERE
      { ?s ?p ?o }
    =>
    [
      {
        "s": "<http://example.com/products/1001>",
        "p": "<http://example.com/color>",
        "o": ""blue""
      },
      {
        "s": "<http://example.com/products/1002>",
        "p": "<http://example.com/color>",
        "o": ""red""
      },
      {
        "s": "<http://example.com/products/1001>",
        "p": "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>",
        "o": "<http://example.com/Henley>"
      },{
        "s": "<http://example.com/products/1002>",
        "p": "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>",
        "o": "<http://example.com/Shirt>"
      }
    ]
  5. MOVE
  6. Example:

    ## move <graph-1> to <graph-2>
    MOVE GRAPH <graph-1> TO <graph-3>
    =>
    Empty

    Effect:

    ## show me all graphs using curl
    ## run this from the command line
    ## substitute your username, password, hostname, and the REST port
    curl --anyauth --user user:password -i -X GET 'http://localhost:9805/v1/graphs'
    =>
    graph-2
    graph-3
    ## show me all triples in the graph <graph-3>
    SELECT ?s ?p ?o
    FROM <graph-3>
    WHERE
      { ?s ?p ?o }
    =>
    [
      {
        "s": "<http://example.com/products/1001>",
        "p": "<http://example.com/color>",
        "o": ""blue""
      },
      {
        "s": "<http://example.com/products/1002>",
        "p": "<http://example.com/color>",
        "o": ""red""
      },
      {
        "s": "<http://example.com/products/1001>",
        "p": "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>",
        "o": "<http://example.com/Henley>"
      },
      {
        "s": "<http://example.com/products/1002>",
        "p": "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>",
        "o": "<http://example.com/Shirt>"
      }
    ]
  7. ADD
  8. Example 1:

    ## create <graph-4>
    CREATE GRAPH <graph-4>
    =>
    EmptyExample 2:
    ## insert some ontology data into graph-4
    INSERT DATA
    {
      GRAPH <graph-4>
      {
        ex:Henley rdfs:subClassOf ex:Shirt .
      }
    }

    Effect:

    ## show me all graphs using curl
    ## run this from the command line
    ## substitute your username, password, hostname, and the REST port
    curl --anyauth --user user:password -i -X GET 'http://localhost:9805/v1/graphs'
    =>
    graph-2
    graph-3
    graph-4

    Example 3:

    ## add <graph-4> to <graph-3>
    ADD GRAPH <graph-4> TO <graph-3>
    =>
    Empty

    Effect:

    ## show me all graphs using curl
    ## run this from the command line
    ## substitute your username, password, hostname, and the REST port
    curl --anyauth --user user:password -i -X GET 'http://localhost:9805/v1/graphs'
    =>
    graph-2
    graph-3
    graph-4
    ## show me all triples in the graph <graph-3>
    SELECT ?s ?p ?o
    FROM <graph-3>
    WHERE
      { ?s ?p ?o }
    =>
    [
      {
        "s": "<http://example.com/products/1001>",
        "p": "<http://example.com/color>",
        "o": ""blue""
      },
      {
        "s": "<http://example.com/products/1002>",
        "p": "<http://example.com/color>",
        "o": ""red""
      },
      {
        "s": "<http://example.com/products/1001>",
        "p": "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>",
        "o": "<http://example.com/Henley>"
      },
      {
        "s": "<http://example.com/products/1002>",
        "p": "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>",
        "o": "<http://example.com/Shirt>"
      },
      {
        "s": "<http://example.com/Henley>",
        "p": "<http://www.w3.org/2000/01/rdf-schema#subClassOf>",
        "o": "<http://example.com/Shirt>"
      }
    ]
  9. DROP
  10. Example:

    ## reset:
    ## remove all content from the graph <http://marklogic.com/semantics/sb/products/>
    ## use "SILENT" so it doesn't give an error if the graph doesn't exist
    DROP SILENT GRAPH <graph-1> ;
    DROP SILENT GRAPH <graph-2> ;
    DROP SILENT GRAPH <graph-3> ;

    Effect:

    ## show me all graphs using curl
    ## run this from the command line
    ## substitute your username, password, hostname, and the REST port
    curl --anyauth --user user:password -i -X GET 'http://localhost:9805/v1/graphs'
    =>
    Nothing

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.