AWS IoT TwinMaker knowledge graph additional resources
This section provides basic examples of the PartiQL syntax used to write queries in the knowledge graph, as well as links to PartiQL documentation that provide information on the knowledge graph data model.
This set of examples shows basic queries with their responses. Use this as a reference to write your own queries.
- Basic queries
-
-
Get all entities with a filter
SELECT entity FROM EntityGraph MATCH (entity) WHERE entity.entityName = 'room_0'This query returns all the entities in a workspace with the name
room_0.FROMclause:EntityGraphis the graph collection that contains all the entities and their relationships in a workspace. This collection is automatically created and managed by AWS IoT TwinMaker based on the entities in your workspace.MATCHclause: specifies a pattern that matches a portion of the graph. In this case, the pattern(entity)matches every node in the graph and is bound to the entity variable. TheFROMclause must be followed by theMATCHclause.WHEREclause: specifies a filter on theentityNamefield of the node, where the value must matchroom_0.SELECTclause: specifies theentityvariable so the whole entity node is returned.Response:
{ "columnDescriptions": [ { "name": "entity", "type": "NODE" } ], "rows": [ { "rowData": [ { "arn": "arn:aws:iottwinmaker:us-east-1: 577476956029: workspace / SmartBuilding8292022 / entity / room_18f3ef90 - 7197 - 53 d1 - abab - db9c9ad02781 ", "creationDate": 1661811123914, "entityId": "room_18f3ef90-7197-53d1-abab-db9c9ad02781", "entityName": "room_0", "lastUpdateDate": 1661811125072, "workspaceId": "SmartBuilding8292022", "description": "", "components": [ { "componentName": "RoomComponent", "componentTypeId": "com.example.query.construction.room", "properties": [ { "propertyName": "roomFunction", "propertyValue": "meeting" }, { "propertyName": "roomNumber", "propertyValue": 0 } ] } ] } ] } ] }The
columnDescriptionsreturns metadata about the column, such as the name and type. The type returned isNODE. This indicates that the whole node has been returned. Other values for the type can beEDGEwhich would indicate a relationship orVALUEwhich would indicate a scalar value such as an integer or string.The
rowsreturns a list of rows. As only one entity was matched, onerowDatais returned which contains all the fields in an entity.Note
Unlike SQL where you can only return scalar values, you can return an object (as JSON) using PartiQL.
Each node contains all the entity-level fields such as
entityId,arnandcomponents, component-level fields such ascomponentName,componentTypeIdandpropertiesas well as property-level fields such aspropertyNameandpropertyValue, all as a nested JSON. -
Get all relationships with a filter:
SELECT relationship FROM EntityGraph MATCH (e1)-[relationship]->(e2) WHERE relationship.relationshipName = 'isLocationOf'This query returns all the relationships in a workspace with relationship name
isLocationOf.The
MATCHclause: specifies a pattern that matches two nodes (indicated by()) that are connected by a directed edge (indicated by-[]->) and bound to a variable calledrelationship.The
WHEREclause: specifies a filter on therelationshipNamefield of the edge, where the value isisLocationOf.The
SELECTclause: specifies the relationship variable so the whole edge node is returned.Response
{ "columnDescriptions": [{ "name": "relationship", "type": "EDGE" }], "rows": [{ "rowData": [{ "relationshipName": "isLocationOf", "sourceEntityId": "floor_83faea7a-ea3b-56b7-8e22-562f0cf90c5a", "targetEntityId": "building_4ec7f9e9-e67e-543f-9d1b- 235df7e3f6a8", "sourceComponentName": "FloorComponent", "sourceComponentTypeId": "com.example.query.construction.floor" }] }, ... //rest of the rows are omitted ] }The type of the column in
columnDescriptionsis anEDGE.Each
rowDatarepresents an edge with fields likerelationshipName. This is the same as the relationship property name defined on the entity. ThesourceEntityId,sourceComponentNameandsourceComponentTypeIdgive information about which entity and component the relationship property was defined on. ThetargetEntityIdspecifies which entity this relationship is pointing towards. -
Get all entities with a specific relationship to a specific entity
SELECT e2.entityName FROM EntityGraph MATCH (e1)-[r]->(e2) WHERE relationship.relationshipName = 'isLocationOf' AND e1.entityName = 'room_0'This query returns all the entity names of all entities that have an
isLocationOfrelationship with theroom_0entity.The
MATCHclause: specifies a pattern that matches any two nodes (e1,e2) that have a directed edge (r).The
WHEREclause: specifies a filter on the relationship name and source entity’s name.The
SELECTclause: returns theentityNamefield in thee2node.Response
{ "columnDescriptions": [ { "name": "entityName", "type": "VALUE" } ], "rows": [ { "rowData": [ "floor_0" ] } ] }In the columnDescriptions, the type of the column is
VALUEsinceentityNameis a string.One entity,
floor_0, is returned.
-
- MATCH
-
The following patterns are supported in a
MATCHclause:-
Match node 'b' pointing to node 'a':
FROM EntityGraph MATCH (a)-[rel]-(b) -
Match node 'a' pointing to node 'b':
FROM EntityGraph MATCH (a)-[]->(b)There is no variable bound to a relationship assuming a filter doesn’t need to be specified on the relationship.
-
Match node 'a' pointing to node 'b' and node 'b' pointing to node 'a':
FROM EntityGraph MATCH (a)-[rel]-(b)This will return two matches: one from 'a' to 'b' and another from 'b' to 'a', so the recommendation is to use directed edges wherever possible.
-
The relationship name is also a label of the property graph
EntityGraph, so you can simply specify the relationship name following a colon (:) instead of specifying a filter onrel.relationshipNamein theWHEREclause.FROM EntityGraph MATCH (a)-[:isLocationOf]-(b) -
Chaining: patterns can be chained to match on multiple relationships.
FROM EntityGraph MATCH (a)-[rel1]->(b)-[rel2]-(c) -
Variable hop patterns can span multiple nodes and edges as well:
FROM EntityGraph MATCH (a)-[]->{1,5}(b)This query matches any pattern with outgoing edges from node 'a' within 1 to 5 hops. The allowed quantifiers are:
{m,n}- between m and n repetitions{m,}- m or more repetitions.
-
- FROM:
-
An entity node can contain nested data, such as components which themselves contain further nested data such as properties. These can be accessed by unnesting the result of the MATCH pattern.
SELECT e FROM EntityGraph MATCH (e), e.components AS c, c.properties AS p WHERE c.componentTypeId = 'com.example.query.construction.room', AND p.propertyName = 'roomFunction' AND p.propertyValue = 'meeting'Access nested fields by dotting
.into a variable. A comma (,) is used to unnest (or join) entities with the components inside and then the properties inside those components.ASis used to bind a variable to the unnested variables so that they can be used in theWHEREorSELECTclauses. This query returns all entities that contains a property namedroomFunctionwith valuemeetingin a component with component type idcom.example.query.construction.roomTo access multiple nested fields of a field such as multiple components in an entity, use the comma notation to do a join.
SELECT e FROM EntityGraph MATCH (e), e.components AS c1, e.components AS c2 - SELECT:
-
-
Return a node:
SELECT e FROM EntityGraph MATCH (e) -
Return an edge:
SELECT r FROM EntityGraph MATCH (e1)-[r]->(e2) -
Return a scalar value:
SELECT floor.entityName, room.description, p.propertyValue AS roomfunction FROM EntityGraph MATCH (floor)-[:isLocationOf]-(room), room.components AS c, c.properties AS pFormat the name of the output field by aliasing it using
AS. Here, instead ofpropertyValueas column name in the response,roomfunctionis returned. -
Return aliases:
SELECT floor.entityName AS floorName, luminaire.entityName as luminaireName FROM EntityGraph MATCH (floor)-[:isLocationOf]-(room)-[:hasPart]- (lightingZone)-[:feed]-(luminaire) WHERE floor.entityName = 'floor_0' AND luminaire.entityName like 'lumin%'Using aliases is highly recommended to be explicit, increase readability, and avoid any ambiguities in your queries.
-
- WHERE:
-
The supported logical operators are
AND,NOT, andOR.The supported comparison operators are
<,<=,>,=>,=, and!=.Use the
INkeyword if you want to specify multipleORconditions on the same field.-
Filter on an entity, component or property field:
FROM EntityGraph MATCH (e), e.components AS c, c.properties AS p WHERE e.entityName = 'room_0' AND c.componentTypeId = 'com.example.query.construction.room', AND p.propertyName = 'roomFunction' AND NOT p.propertyValue = 'meeting' OR p.propertyValue = 'office' -
Filter on the
configurationproperty. Hereunitis the key in the configuration map andCelsiusis the value.WHERE p.definition.configuration.unit = 'Celsius' -
Check if a map property contains a given key and value:
WHERE p.propertyValue.length = 20.0 -
Check if a map property contains a given key:
WHERE NOT p.propertyValue.length IS MISSING -
Check if a list property contains a given value:
WHERE 10.0 IN p.propertyValue -
Use the
lower()function for case insensitive comparisons. By default, all comparisons are case sensitive.WHERE lower(p.propertyValue) = 'meeting'
- LIKE:
-
Useful if you do not know the exact value for a field and can perform full text search on the specified field.
%represents zero or more.WHERE e.entityName LIKE '%room%'-
Infix search:
%room% -
Prefix search:
room% -
Suffix search:
%room -
If you have '%' in your values, then put an escape character in the
LIKEand specify the escape character withESCAPE.
WHERE e.entityName LIKE 'room\%' ESCAPE '\' -
- DISTINCT:
-
SELECT DISTINCT c.componentTypeId FROM EntityGraph MATCH (e), e.components AS c-
The
DISTINCTkeyword eliminates duplicates from the final result.DISTINCTis not supported on complex data types.
-
- COUNT
-
SELECT COUNT(e), COUNT(c.componentTypeId) FROM EntityGraph MATCH (e), e.components AS cThe
COUNTkeyword computes the number of items in a query result.COUNTis not supported on nested complex fields and graph pattern fields.COUNTaggregation is not supported withDISTINCTand nested queries.For example,
COUNT(DISTINCT e.entityId)is not supported.
- PATH
-
The following pattern projections are supported in querying using path projection:
Variable hop queries
SELECT p FROM EntityGraph MATCH p = (a)-[]->{1, 3}(b)This query matches and projects nodes metadata of any patterns with outgoing edges from node a within 1 to 3 hops.
Fixed hop queries
SELECT p FROM EntityGraph MATCH p = (a)-[]->(b)<-[]-(c)This query matches and projects metadata of entities and incoming edges to b.
Undirected queries
SELECT p FROM EntityGraph MATCH p = (a)-[]-(b)-[]-(c)This query matches and projects the metadata of nodes in 1 hop patterns connecting a and c via b.
{ "columnDescriptions": [ { "name": "path", "type": "PATH" } ], "rows": [ { "rowData": [ { "path": [ { "entityId": "a", "entityName": "a" }, { "relationshipName": "a-to-b-relation", "sourceEntityId": "a", "targetEntityId": "b" }, { "entityId": "b", "entityName": "b" } ] } ] }, { "rowData": [ { "path": [ { "entityId": "b", "entityName": "b" }, { "relationshipName": "b-to-c-relation", "sourceEntityId": "b", "targetEntityId": "c" }, { "entityId": "c", "entityName": "c" } ] } ] } ] }This
PATHquery response comprises of only metadata that identifies all the nodes and edges of each path/pattern between a and c via b.
- LIMIT and OFFSET:
-
SELECT e.entityName FROM EntityGraph MATCH (e) WHERE e.entityName LIKE 'room_%' LIMIT 10 OFFSET 5LIMITspecifies the number of results to be returned in the query, andOFFSETspecifies the number of results to skip. - LIMIT and maxResults:
-
The following example shows a query that returns 500 results in total, but only displays 50 at a time per API call. This pattern can be used where you need to limit the amount of displayed results, for example if you are only able to display 50 results in a UI.
aws iottwinmaker execute-query \ --workspace-id exampleWorkspace \ --query-statement "SELECT e FROM EntityGraph MATCH (e) LIMIT 500"\ --max-results 50-
The
LIMITkeyword affects the query and limits the resulting rows. If you need to control the number of results returned per API call without limiting the total number of returned results, useLIMIT. -
max-resultsis an optional parameter for the ExecuteQuery API action.max-resultsonly applies to the API and how results are read within the bounds of the above query.Using
max-resultsin a query allows you to reduce the number of displayed results without limiting the actual number of returned results.
The query below iterates through the next page of results. This query uses the
ExecuteQueryAPI call to return rows 51-100, where the next page of results is specified by thenext-token– in this case the token is:"H7kyGmvK376L".aws iottwinmaker execute-query \ --workspace-id exampleWorkspace \ --query-statement "SELECT e FROM EntityGraph MATCH (e) LIMIT 500"\ --max-results 50 --next-token "H7kyGmvK376L"-
The
next-tokenstring specifies the next page of results. For more information, see the ExecuteQuery API action.
-
AWS IoT TwinMaker knowledge graph query has the following limits:
| Limit Name | Quota | Adjustable |
|---|---|---|
|
Query execution timeout |
10 seconds | No |
|
Maximum number of hops |
10 | Yes |
|
Maximum number of self |
20 | Yes |
|
Maximum number of projected fields |
20 | Yes |
|
Maximum number of conditional expressions ( |
10 | Yes |
|
Maximum length of a |
20 | Yes |
Maximum number of items that can be specified in an IN clause |
10 | Yes |
Maximum value for OFFSET |
3000 | Yes |
|
Maximum value for |
3000 | Yes |
|
Maximum value for traversals ( |
3000 | Yes |