Running SPARQL Queries in the Query Builder

The Query Builder includes a Query tab for writing and running SPARQL queries. The query editor provides syntax assistance, type-ahead suggestions for model entity names, and automated prefix creation and query formatting for readability. It also includes the option to save queries for later use.

The Query tab supports running queries against the following data sources:

  • Graphmarts and specific data layers within graphmarts
  • Linked Datasets
  • Data sources: Anzo System Data Source, AnzoGraph, Anzo System Tables, Data Profiling Metrics, LDAP Primary Data Source

To ensure that queries perform well and do not consume too many resources on the system, keep the following guidelines in mind when developing and testing queries:

  • Set a limit on the number of results to return.
  • Avoid cross-product joins
  • Consider using VALUES clauses instead of FILTER clauses.
  • When retrieving a large number of values, use subqueries instead of OPTIONAL clauses.

For query templates and additional details about best practices, see SPARQL Best Practices and Query Templates.

Follow the instructions below to write and run SPARQL queries against any of the supported data sources.

  1. In the Anzo application, expand the Access menu and click Query Builder. Anzo displays the query editor.

  2. At the top of the screen, click the edit () icon next to the Target data source:
    • To query data that is in a graphmart, select the Graphmart radio button.

      Click the Select Graphmart drop-down list and select the graphmart to query. If you want to narrow the scope of the query by selecting one or more data layers in the graphmart, click the Select Layers drop-down list and select the layer or layers to target.

    • To query data that is in the Datasets catalog, select the Linked Dataset radio button.

      Click the Select linked dataset drop-down list and select the linked dataset to query.

    • To run queries against the system data source, data profiling metrics, Anzo system tables, LDAP server, or AnzoGraph, select the Datasource radio button.

      Click the Datasource drop-down list and select the target source:

      • Select System Datasource to search the local Anzo system volume.
      • Select the name of an AnzoGraph instance to search for data in graphmarts that are loaded to that instance.
      • Select Data Profiling Metrics to search the data metrics volume.
      • Select LDAP Primary Datasource to search the directory server.
      • Select System Tables to search Anzo system table data.

      By default, the Named Graphs and Default Named Graphs values are set to all named graphs (http://openanzo.org/namedGraphs/reserved/graphs/ALL). If you want to narrow the scope of the query, you can replace the values with specific graph URIs. To list multiple graphs, separate URIs with a space.

  3. When you have finished configuring the target, click outside of the dialog box to return to the Query screen.
  4. In the text box below the target, compose the SPARQL query. See SPARQL Best Practices and Query Templates for tips on writing queries. For information about the supported SPARQL functions, see Function and Formula Reference.

    When adding PREFIX statements, once you type prefix followed by a space Anzo displays a tooltip that lists all of the global prefixes that are defined for your system. Clicking a prefix in the list inserts a PREFIX statement into the query. For example:

    In addition, typing the abbreviation for a global prefix followed by a colon (:) automatically inserts the PREFIX statement into the query without opening the tooltip. For more information about global prefixes, see Configure Global Prefixes.

    When typing entity URIs in the WHERE clause, the query builder also offers suggestions by listing the properties in the data source. You can click an item in the list to insert that entity. For example:

    When a red exclamation mark icon () is displayed next to a line number, you can hover the pointer over the icon to view guidance on how to resolve the issue. For example:

  5. If you want to format the query for readability, click the Format button and select from the following options:
    • Format: Auto-creates prefixes, inserts URI abbreviations, and restructures the query for readability.
    • Format with simplified variable names: Auto-creates prefixes, inserts URI abbreviations, simplifies variable names by changing them to ?_var1, ?_var2, ?_varN, and restructures the query for readability.

    For example, the image below shows a query before it is formatted.

    After the query is formatted, prefixes and URI abbreviations are added. For example:

  6. If the query is an INSERT or DELETE query, the Dry Run button becomes active. You can click Dry Run to do a test run of the update. In a test run, Anzo runs a version of the query where INSERT or DELETE is replaced with CONSTRUCT, and the results report the number of statements that the query affects, i.e., the number of additions or removals per graph. If the results are unexpected, you can adjust the query before clicking running the query and committing the updates.
  7. If necessary, change the query limit. By default, query results are limited to 500. To adjust the limit, click the Limit results to drop-down list below the query editor and select a value. For example:

  8. To run the query, click Run. The results appear at the bottom of the screen. For example:

    You can click any value in the result list to copy that value to the clipboard.

  9. To save the query for later use, click the Save button at the top of the screen. Anzo displays the New Query dialog box.

  10. In the dialog box, specify a name for the query in the Title field and an optional description in the Description field.
  11. You can click the Folder drop-down list to select a different folder or create a new one to save the query in. Then click Save. The query is saved and added to the Queries panel on the left side of the screen.