Search⌘ K
AI Features

Query the Local RDF Service: Major Query Forms

Explore how to query RDF data locally using SPARQL’s major query forms such as SELECT and CONSTRUCT. Learn to execute these queries on a local RDF service, process the results, and understand how to manipulate RDF graphs within Elixir. This lesson provides hands-on experience with essential SPARQL commands for working efficiently with semantic graph data.

We'll cover the following...

Load the data

We’ll need some RDF data. To keep things simple, let’s take the RDF description for the book graph:

iex> graph_service RDFGraph
iex> graph_create read_graph("book.ttl")
iex> graph_info
Queries to load the book graph

Execute the commands above in the terminal below to load the book graph:

#---
# Excerpted from "Exploring Graphs with Elixir",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material,
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose.
# Visit http://www.pragmaticprogrammer.com/titles/thgraphs for more book information.
#---
defmodule RDFGraph.Vocab do
  use RDF.Vocabulary.Namespace

  alias RDF.NS.XSD

  defvocab(DC,
    base_iri: "http://purl.org/dc/elements/1.1/",
    file: "dc.ttl"
    # terms: ~w[
    #   contributor coverage creator date description format
    #   identifier language publisher relation rights source
    #   subject title type
    # ]
  )

  defvocab(BIBO,
    base_iri: "http://purl.org/ontology/bibo/",
    file: "bibo.ttl",
    case_violations: :ignore
  )

  defvocab(DCTERMS,
    base_iri: "http://purl.org/dc/terms/",
    file: "bibo.ttl",
    case_violations: :ignore
  )

  defvocab(EVENT,
    base_iri: "http://purl.org/NET/c4dm/event.owl#",
    file: "bibo.ttl"
  )

  defvocab(FOAF,
    base_iri: "http://xmlns.com/foaf/0.1/",
    file: "bibo.ttl"
  )

  defvocab(PRISM,
    base_iri: "http://prismstandard.org/namespaces/1.2/basic/",
    file: "bibo.ttl"
  )

  defvocab(SCHEMA,
    base_iri: "http://schemas.talis.com/2005/address/schema#",
    file: "bibo.ttl"
  )

  defvocab(STATUS,
    base_iri: "http://purl.org/ontology/bibo/status/",
    file: "bibo.ttl",
    case_violations: :ignore
  )

  ## book function defintions

  def book() do

    import RDF.Sigils
    alias RDF.NS.{XSD}

    ~I<urn:isbn:978-1-68050-252-7>
    |> RDF.type(BIBO.Book)
    |> DC.creator(
      ~I<https://twitter.com/bgmarx>
    )
    |> DC.date("2018-03-14")
    |> DC.format(~L"Paper")
    |> DC.identifier(~L"adopting_elixir")
    |> DC.identifier(RDF.literal("urn:isbn:978-1-68050-252-7",
        datatype: XSD.anyURI()))
    |> DC.publisher(~I<https://pragprog.com/>)
    |> DC.title(~L"Adopting Elixir"en)
  end

  def book_long() do

    alias RDF.NS.{XSD}

    s = RDF.iri("urn:isbn:978-1-68050-252-7")

    t0 = {s, RDF.type(), RDF.iri(BIBO.Book)}
    t1 = {s, DC.creator(), RDF.iri("https://twitter.com/bgmarx")}
    t2 = {s, DC.creator(), RDF.iri("https://twitter.com/josevalim")}
    t3 = {s, DC.creator(), RDF.iri("https://twitter.com/redrapids")}
    t4 = {s, DC.date(), RDF.literal("2018-03-14", datatype: XSD.date())}
    t5 = {s, DC.format(), RDF.literal("Paper")}
    t6 = {s, DC.identifier(), RDF.literal("adopting_elixir")}
    t7 = {s, DC.identifier(), RDF.literal("urn:isbn:978-1-68050-252-7", datatype: XSD.anyURI())}
    t8 = {s, DC.publisher(), RDF.iri("https://pragprog.com/")}
    t9 = {s, DC.title(), RDF.literal("Adopting Elixir", language: "en")}

    RDF.Description.new([t0, t1, t2, t3, t4, t5, t6, t7, t8, t9])
  end

end
Load the book graph

SELECT

Let’s have a look at the SELECT query ...