Using SPARQL to Query and Modify Data

SPARQL Query Panel

This panel lets you compose, run and save SPARQL queries. For example, a query shown below will identify assets that do not have a preferred label in Russian, effectively providing a translation list.

TopBraid EDG SPARQL Query Panel

TopBraid EDG SPARQL Query Panel

You can use either URIs or Qnames. The panel will automatically resolve prefixes and put out PREFIX statements. The panel will detect and report query syntax errors. Query parts can be collapsed and expanded for readability.

See also

To see results of query execution, use SPARQL Results Panel.

You can use SPARQL Layout which already integrates SPARQL Query and SPARQL Results panels as well as SPARQL Library panel.

To save a query for re-use, use Save icon. You will be able to name the query and decide the scope of its sharing. Your query can be parametrized. This will let users enter a parameter when they run saved query. To indicate that a variable is a parameter, replace “J” with “$” e.g., use $label instead of ?label.

Use the algebra button to see the SPARQL algebra associated with the query for advanced debugging. This will show how the SPARQL engine is executing the query allowing optimizations to be discovered.

TopBraid EDG Save SPARQL Query to Library Dialog

TopBraid EDG Save SPARQL Query to Library Dialog

TopBraid EDG supports SPARQL 1.1, including SPARQL Update. If the EDG server has been configured (see Administrator Guide, Server Configuration Parameters) to allow SPARQL updates, editors (and managers) can run them, but viewers cannot.

GeoSPARQL is also supported. To enable your asset collection for Geospatial queries, use the Manage tab to enable GeoSPARQL. This will create and maintain the geo spatial index.

Note

The Pivot Table and Geo functions can be slow on some platforms and are not supported for Internet Explorer.

SPARQL Results Panel

This panel shows results of SPARQL query execution from the SPARQL Query Panel in a tabular format and lets you export them. Table columns are sortable and rows are clickable so that you could see more information about returned resource on, let’s say, a form.

TopBraid EDG SPARQL Results Panel

TopBraid EDG SPARQL Results Panel

By default, returned resources are displayed using their labels. Settings menu lets you request that IDs (QNames) are displayed instead of labels. Export menu lets you export query results in a variety of formats. Results of SPARQL SELECT queries can be exported in any of the standard export formats as defined by the W3C specifications: TSV and CSV, JSON and XML. Results of SPARQL CONSTRUCT and DESCRIBE queries can be exported in one of the standard RDF serializations: Turtle, JSON-LD, RDF/XML.

SPARQL Library Panel

This panel will show the list of queries saved using the SPARQL query panel. What it shows is determined by the scope of the saved queries and the asset collection you are currently in.

You will be able to select and run a query. You will also be able to delete saved queries.

TopBraid EDG SPARQL Library Panel

TopBraid EDG SPARQL Library Panel

The settings menu lets you configure how much information about a query should be displayed.

The following buttons can be used to show the query and to copy the URL for the query results.

TopBraid EDG Show Query and Copy URL Buttons

TopBraid EDG Show Query and Copy URL Buttons

TopBraid EDG SPARQL Library Query Example

The SPARQL Library panel

SPARQL Endpoint

In addition to the built-in panels of the main application, TopBraid EDG includes a general SPARQL endpoint at /tbl/sparql.

SPARQL Endpoint Page

When called from a browser, /tbl/sparql opens an interactive editor for entering and executing SPARQL queries.

Screenshot of TopBraid's SPARQL endpoint page

TopBraid’s SPARQL Endpoint page

Note

Only use the SPARQL endpoint if you need to make arbitrary SPARQL queries that are not only about a specific asset collection. Or if you need to query other graphs such as the change history data as shown in the screenshot above. For most users, the SPARQL Query Panel is typically a more user-friendly choice.

SPARQL Endpoint Web Service

The SPARQL endpoint can also be called programmatically from external systems, for example to query terms from a Taxonomy.

Using SPARQL is similar to using a public endpoint of a database where you can perform your own queries instead of having to learn and use the (often restrictive) web service APIs. All you need to know is the query language (SPARQL). Results of SELECT SPARQL queries are tabular structures that are delivered in standardized formats as described in the SPARQL 1.1 Protocol.

See also

An alternative to using SPARQL is GraphQL, which is suitable for use cases such as fetching an array of instances with certain characteristics. An even more flexible alternative is using Scripting with JavaScript and ADS, which offers the ultimate flexibility to produce any type of output.

If you want to query a TopBraid server from arbitrary SPARQL 1.1 processors, you can use the SERVICE keyword. To make this work, you just need to know the URL of the TopBraid server and the name/URI of the graph that you want to query. The general pattern of the graphs representing the EDG asset collections is urn:x-evn-master:XY where you substitute XY with the identifier that you see before .editor in the URL of the main editor of your asset collection.

For example, if you have EDG installed at the domain name ourserver.com on port 8080, use the following query to send a query to that endpoint requesting a list of Continent resources in the Geography vocabulary:

1SELECT ?continent
2WHERE {
3   SERVICE <http://ourserver.com:8080/edg/tbl/sparql> {
4      GRAPH <urn:x-evn-master:geo> {
5          ?continent a <http://topquadrant.com/ns/examples/geography#Continent>
6      }
7   }
8}

If you do not want to use the SERVICE keyword, you can invoke the SPARQL endpoint service directly. For example, let’s say you wanted to send the following query to the endpoint http://ourserver.com:8080/EDG/tbl/sparql:

1SELECT ?continent
2WHERE {
3   GRAPH <urn:x-evn-master:geo> {
4      ?continent a <http://topquadrant.com/ns/examples/geography#Continent>
5   }
6}

You can add an escaped version as a query parameter value to the endpoint URL shown above, resulting in the following URL. The best practice is to use a URL encoding function to properly format the query for HTTP:

http://ourserver.com:8080/edg/tbl/sparql?query=SELECT+?continent+WHERE+{GRAPH+<urn:x-evn-master:geo>+{?continent+a+<http://topquadrant.com/ns/examples/geography%23Continent>}}

An option to specifying the named graph with the GRAPH keyword, you can use an endpoint specific to the asset collection you want to query.

To do so, name the graph in a default-graph-uri parameter added to the URL. For example, let’s look at this simpler query that does not mention the graph to query:

1SELECT ?continent
2WHERE {
3   ?continent a <http://topquadrant.com/ns/examples/geography#Continent>
4}

Escaping this and adding it to the SPARQL endpoint URL with the named graph specified using the default-graph-uri parameter gives us the following URL (split here for display, but to be treated as one line):

http://ourserver.com:8080/edg/tbl/sparql?default-graph-uri=urn:x-evn-master:geo&query=SELECT+?continent+WHERE+{?continent+a+<http://topquadrant.com/ns/examples/geography%23Continent>}

As an alternative to those GET-style requests you may send a POST request with the query string in the request body and the optional default-graph-uri parameter.

Assuming that SPARQL UPDATEs have been enabled in the server administration pages, you can also use the tbl/sparql endpoint for UPDATE requests.

SPARQL Update statements use the sparql servlet with the update parameter for specifying the SPARQL Update query. Note that the query must therefore explicitly specify a graph context to execute the update in using the GRAPH <...> syntax. An example update call is shown in the following URL:

tbl/sparql?update=INSERT{GRAPH <urn:x-evn-master:schemaexample>{<http://topbraid.org/examples/topquadrant%23TBEVN> <http://www.w3.org/2000/01/rdf-schema%23comment> "a comment"}}WHERE {}

Note this assumes a user is logged in, either via a browser or a basic authentication request. SPARQL Updates can be included in the change history by appending the user name to the end of the graph name as in the following example. The change will be added to the change graph with the currently logged in user as the creator of the change.

tbl/sparql?update=INSERT{GRAPH <urn:x-evn-master:schemaexample:Administrator>{<http://topbraid.org/examples/topquadrant%23TBEVN> <http://www.w3.org/2000/01/rdf-schema%23comment> "a second comment"}}WHERE {}

Access controls can also be defined for SPARQL Update using the Rights Management page and the SPARQLUpdateAllowGrp and SPARQLUpdateDenyGrp groups. For more information see the Rights Management document.

Hint

A good way of learning the details of the SPARQL endpoint syntax is by using the interactive SPARQL Endpoint Page and look at the Network tab of your browser’s inspection tool.