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 Data Sets
  • Data sources: Anzo System Data Source, AnzoGraph, Anzo System Tables, Data Profiling Metrics, LDAP Primary Data Source

Note: 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 Query Templates and Best Practices.

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

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

  2. At the top of the screen, click the radio button for 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 data layer or layers to target.

    • To query data that is in a linked data set, select the Linked Dataset radio button.

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

    • To run queries against the system data source, data metrics volume, 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 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. In the text box below the target, compose the SPARQL query. For information about the supported SPARQL functions, see Supported Functions and Formulas.

    Tip: When typing entity URIs, the query builder 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:

  4. If you want to format the query for readability, click Format Query and select one of 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:

  5. 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:

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

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

  7. To save the query for later use, click Save Query. Anzo displays the Save Query dialog box.

  8. In the Save Query dialog box, specify a name for the query in the Title field and an optional description in the Description field. Then click Save. The query is saved in the gray Queries list on the left side of the screen and is collapsed by default.

    Click the arrow or anywhere on the gray tab to expand the list. For example:

    Select a query to open it in the query editor. You can delete a query by clicking the trashcan icon next to the query name. If you change a query and want to save it as a new query, click Save as New Query.

Related Topics