Load and query some data

Query data through the Workbench

Tip

SPARQL is an SQL-like query language for RDF graph databases with the following query forms:

  • SELECT: returns tabular results

  • CONSTRUCT: creates a new RDF graph based on query results

  • ASK: returns YES if the query has a solution, otherwise “NO”

  • DESCRIBE: returns RDF data about a resource; useful when you do not know the RDF data structure in the data source

  • INSERT: inserts triples into a graph

  • DELETE: deletes triples from a graph

For more information, see The SPARQL query language.

Let’s create a repository, load some data into it, and then query that data.

First, create a GraphDB repository as described in the previous section, Create your first repository using the Workbench. For this new repository, enter “starwars” in its Repository ID field and “Star Wars data” in its Repository description field.

After you have created it and see starwars listed on the Repositories screen, it won’t necessarily be the currently connected one. The Connected repository icon connectedRepositoryIcon shows which is currently connected and the Connect repository icon connectRepositoryIcon shows which ones are available but not connected. If necessary, click the Connect repository icon next to starwars.

To load a sample dataset into this repository:

  1. Download the starwars.ttl file and remember where you stored it.

  2. Select Import from the Workbench’s main menu on the left.

  3. On the Import screen, select Upload RDF files, navigate to the folder where you stored the downloaded file, and select it. This will add the file to the Import screen with a checkbox on its left.

  4. Click the Import button on the right of the starwars.ttl entry on the screen’s list.

  5. Click Import on the Import settings dialog box. This should return you to the Import screen, where the message “Imported successfully in less than a second” gets added to the starwars.ttl row.

Let’s query the data that we’ve loaded into this repository.

  1. Click SPARQL on the Workbench’s main menu.

  2. Paste the following SPARQL query into the query field:

    PREFIX voc:  <https://swapi.co/vocabulary/>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    SELECT ?title ?director
    WHERE {
       ?movie a voc:Film ;
              rdfs:label ?title ;
              voc:director ?director .
    }
    
  3. Click the Run button in the lower right of the field. You will see the list of titles and directors that your query requested in the result area underneath the query:

_images/StarWarsTitlesDirectors.png

Congratulations! Your copy of GraphDB is up and running and waiting for more queries, data, and enrichment of that data as you build knowledge graphs to drive your applications. See the Tutorials section in this documentation’s main menu to learn more.

Query data programmatically

SPARQL is not only a standard query language, but also a protocol for communicating with RDF databases. GraphDB stays compliant with the protocol specification and allows querying data with standard HTTP requests.

This first example uses the curl utility to execute a query (a URL-encoded version of the query CONSTRUCT {?s ?p ?o} WHERE {?s ?p ?o} LIMIT 10) with an HTTP GET request:

curl -G -H "Accept:application/x-trig" \
  -d query=CONSTRUCT+%7B%3Fs+%3Fp+%3Fo%7D+WHERE+%7B%3Fs+%3Fp+%3Fo%7D+LIMIT+10 \
  http://localhost:7200/repositories/yourrepository

This query executes the example query with a POST operation:

curl -X POST --data-binary @file.sparql -H "Accept: application/rdf+xml" \
  -H "Content-type: application/x-www-form-urlencoded" \
  http://localhost:7200/repositories/yourrepository

In that example, file.sparql contains this encoded query:

query=CONSTRUCT+%7B%3Fs+%3Fp+%3Fo%7D+WHERE+%7B%3Fs+%3Fp+%3Fo%7D+LIMIT+10

Tip

See GraphDB APIs for more information on use the GraphDB APIs.