Skip to main content

Edge queries and functions

Once edges have been connected to a federation, it is possible to define functions and execute queries on them from a client. To do this, the edge_cq function is used to create Edge queries.

To follow this part of the guide, an edge instance called "edge1" needs to be connected to the federation. Connect an SA Engine instance in the SA Studio sandbox environment with that name to continue. listening_edges should return ['EDGE1'].

listening_edges();
Not connected

To run this code block you must be logged in and your studio instance must be started.

Starting and stopping edge queries

Start an edge query using the edge name and a string with the query. The result of the query is returned as a stream from the edge device.

edge_cq("edge1", "select heartbeat(0.1) limit 10");
Not connected

To run this code block you must be logged in and your studio instance must be started.

Queries can also be started using the peer notation

// peer: edge1
select heartbeat(0.1) limit 10;
Not connected

To run this code block you must be logged in and your studio instance must be started.

Note

In the graphical client interfaces, SA Studio and VS Code, it is also possible to select what peer a query or function should be executed on in dropdowns. When using this interface, edge queries are started under the hood.

To view running queries, use edge_status().

First start a query, leave it running.

edge_cq("edge1", "heartbeat(1)");
Not connected

To run this code block you must be logged in and your studio instance must be started.

Then look at edge status. This will show a record with an entry for each edge in the federation.

edge_status();
Not connected

To run this code block you must be logged in and your studio instance must be started.

Use a query to filter the result.

select r from record r where r in edge_status() and r['id'] = 'EDGE1';
Not connected

To run this code block you must be logged in and your studio instance must be started.

The record contains connection data for the edge, and a vector with requests which are the running queries. A limited amount of queries can execute on each edge simultaneously, the limit is shown in max_queries. There is also a vector of stopped_requests which are queries that are about to be cancelled.

Each request contains an id number and the query string. The id number can be used to cancel the query. Make sure to change the id number below to the correct id before running it.

cancel_edge_cq("edge1",3);
Not connected

To run this code block you must be logged in and your studio instance must be started.

The request vector will now be empty.

select r["requests"] from record r where r in edge_status() and r['id'] = 'EDGE1';
Not connected

To run this code block you must be logged in and your studio instance must be started.

Also note that the heartbeat query above has stopped running.

Defining functions

Functions must be defined on the SA Engine instance were it will be used. To call a function on the edge, it must first be defined on that edge. To observe this, define an example function.

create function my_example(integer input) -> integer as input + 7;
Not connected

To run this code block you must be logged in and your studio instance must be started.

my_example(3);
Not connected

To run this code block you must be logged in and your studio instance must be started.

Running my_example on edge1 will return an error, since it has only been defined in this client.

edge_cq("edge1", "my_example(3)");
Not connected

To run this code block you must be logged in and your studio instance must be started.

The function can be defined on the edge in an edge query. Run the query below then try the function call above again.

edge_cq("edge1", "create function my_example(integer input) -> integer as input + 7;");
Not connected

To run this code block you must be logged in and your studio instance must be started.

Simlilarily, a function that has only been defined on an edge cannot be called in a client.

edge_cq("edge1", "create function my_other_example(integer input) -> integer as input + 8;");
Not connected

To run this code block you must be logged in and your studio instance must be started.

my_other_example(3);
Not connected

To run this code block you must be logged in and your studio instance must be started.

Using models

It is convenient to use User models to store the functions that will be used to analyze data on the edge. Then all the functions in the model can be loaded together with models:load, or deployed to an edge with models:deploy. Read more in the Model Management chapter.