Use a Query Context

When accessing data sources that require sensitive connection and authorization information such as keys, tokens, and user credentials, you can create Query Contexts for storing the sensitive information. A Query Context has a number of key-value pairs, such as username, password, and connection URL. Queries can then reference the keys from a context file and the connection values are abstracted from the requests. This topic provides instructions for creating contexts and referring to a context in a query.

Creating a Query Context

Follow the steps below to create a query context from the user interface.

You can also create a context file in JSON format and save it on the AnzoGraph DB leader server. To reference a context file when using the AZGI command line interface, use the -context <filename>.json option.

  1. In the Query & Admin Console, click the Admin tab. Then click the Query Contexts menu item. The Context Configuration screen is displayed.

  2. Click the Add Context Config button to create a new context. A new context, named Context N, is added. For example:

  3. At the top of the screen, click the edit icon () next to the context title and specify a name for this context. Queries that connect to this source will use this name when referencing keys in the file. Click Save to save the change.
  4. Click the edit icon () next to the gray contents field. In the field, specify the appropriate key-value pairs to use to connect to the data source. The contents must be in valid JSON format. For example:
    {
      "url": "jdbc:mysql://10.111.4.9:3306/NORTHWIND",
      "username": "sysadmin",
      "password": "admin123"
    }
  5. When you have finished adding key-value pairs, click the checkmark icon () to save the changes. For example:

  6. If you want to create additional Query Contexts, click the Add Context Config button and repeat the steps above. For details about referencing contexts in queries, see Referencing Context Keys in a Query below.

Referencing Context Keys in a Query

To reference the keys from a Query Context in a query, you use the following syntax to specify a variable in the object of a triple pattern:

"{{@context_name:key_name}}"

Where context_name is the title of the context file, and key_name is the key whose value should be used as the object. For example, the following query excerpt from a Graph Data Interface (GDI) query refers to the sample context that was created in Creating a Query Context:

PREFIX s: <http://cambridgesemantics.com/ontologies/DataToolkit#>
​SELECT DISTINCT  *
​WHERE 
{
    SERVICE <http://cambridgesemantics.com/services/DataToolkit>
    { 
       ?data a s:DbSource ;
	  s:url "{{@NorthwindDB:url}}" ;
	  s:username "{{@NorthwindDB:username}}" ;
	  s:password "{{@NorthwindDB:password}}" ;
	  ...
    }
}

At runtime, the GDI refers to the context file to find the values that are associated with the specified keys. For more information about running GDI queries, see Getting Started with GDI Queries.