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
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.
- In the Anzo application, expand the Access menu and click Query Builder. Anzo displays the query editor.
- 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.
- To query data that is in a graphmart, select the Graphmart radio button.
- In the text box below the target, compose the SPARQL query. For information about the supported SPARQL functions, see Supported Functions and Formulas.
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:
- 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:
- If the query is an INSERT or DELETE query, the Dry Run Query button becomes active. You can click Dry Run Query 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 Run Query and committing the updates.
- 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:
- To run the query, click Run Query. 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.
- To save the query for later use, click Save Query. Anzo displays the Save Query dialog box.
- 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.