require(["esri/rest/knowledgeGraph/GraphQueryStreaming"], (GraphQueryStreaming) => { /* code goes here */ });
import GraphQueryStreaming from "@arcgis/core/rest/knowledgeGraph/GraphQueryStreaming.js";
esri/rest/knowledgeGraph/GraphQueryStreaming
Defines a streaming query operation performed on a knowledge graph service's graph resource. The entities and relationships in the graph are queried by sending an Esri implementation of openCypher query.
Streaming query returns results faster than query and in small chunks that can be processed immediately. Streaming query also allows bind parameters.
// sample streaming query using bind parameters
// to search for entities with a the `name` property that matches a specific string
// or have their geometry within a bounding box.
// get all parts bought by each supplier.
// query returns both the supplier and the part it buys.
const query = `MATCH (s:Supplier)-[:buys_part]-(p:Part)
WHERE s.name=$name OR esri.graph.ST_Intersects($geom, s.geometry)
RETURN s,p`;
KnowledgeGraphModule.executeQueryStreaming(
knowledgeGraph,
{
openCypherQuery: query,
bindParameters: {
name: "Supplier 1",
geom: new Polygon({
rings: [
[
[-78, 38],
[-78, 39],
[-76, 39],
[-76, 38],
[-78, 38]
],
],
}),
}
}).then((streamingQueryResult)=>{
// streaming query returns a readableStream which must be read to access the returned graph data
readStream(streamingQueryResult);
})
// a function to read the readable stream returned from the above query
const readStream = async (streamingQueryResult) => {
let time = Date.now();
let reader = streamingQueryResult.resultRowsStream.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) {
console.log(`All records returned, stream closed: ${(Date.now() - time) / 1000} seconds`);
break;
}
console.log(`Streaming chunk returned in: ${(Date.now() - time) / 1000} seconds`, value);
// use the results
// list the parts bought by each supplier
let supplierParts = {};
// each element of the result array will contain one supplier and one part it buys
for (var v in value){
let supplier = value[v][0].properties.Name
let part = value [v][1].properties.Name
if(!(supplier in supplierParts)){
supplierParts[supplier] = [];
}
// collect parts by supplier that buys them
supplierParts[supplier].push(part);
console.log(supplierParts);
// result printed to the console: {Supplier 1:[Part1], Supplier 3:[Part2, Part3]}
}
}
} catch (err) {
if (err.name === "AbortError") {
console.log("Request aborted as expected");
} else {
throw err;
}
}
};
// result stream from above query after it has been read
"Streaming chunk returned in: 0.082 seconds"
[
[{
"declaredClass": "esri.rest.knowledgeGraph.Entity",
"properties": {
"Name": "Supplier 1",
"City": "Washington DC",
"EmployeeCount": 31
},
"typeName": "Supplier",
"id": "{57FDF2F3-34C8-48EF-9A3B-76ED9314C4D2}"
},{
"declaredClass": "esri.rest.knowledgeGraph.Entity",
"properties": {
"Part_ID": 695401,
"Name": "Part1",
"Minimum_quantity": 360
},
"typeName": "Part",
"id": "{IWN51W4-1AW8-A2W6-1AW5F-1AW8F9F4W51AS}",
}],
[{
"declaredClass": "esri.rest.knowledgeGraph.Entity",
"properties": {
"Name": "Supplier 2",
"City": "Baltimore",
"EmployeeCount": 53
},
"typeName": "Supplier",
"id": "{1A4W8F5W-1WA8-5W6A-A1W8-A1W5F8W3S482A}"
},{
"declaredClass": "esri.rest.knowledgeGraph.Entity",
"properties": {
"Part_ID": 695401,
"Name": "Part2",
"Minimum_quantity": 2500
},
"typeName": "Part",
"id": "{A1W5F8W4F-A1W8-1894-16A5-A86WF4A8SFWD}",
}],
[{
"declaredClass": "esri.rest.knowledgeGraph.Entity",
"properties": {
"Name": "Supplier 2",
"City": "Baltimore",
"EmployeeCount": 53
},
"typeName": "Supplier",
"id": "{1A4W8F5W-1WA8-5W6A-A1W8-L5H4G8RT1PK3}"
},{
"declaredClass": "esri.rest.knowledgeGraph.Entity",
"properties": {
"Part_ID": 695401,
"Name": "Part3",
"Minimum_quantity": 5000
},
"typeName": "Part",
"id": "{PTJ51FT-KY4H-1GY5-G1Y8-G1Y5K49G8Y4GHJ}",
}]
]
// aborting the query
// create instance of native browser abort controller
const controller = new AbortController();
// create query
KnowledgeGraphModule
.executeQueryStreaming(
knowledgeGraph,
{
openCypherQuery: "MATCH (n) RETURN n LIMIT 100",
},
{
signal: controller.signal,
}
)
.then((streamingQueryResult) => {
readStream(streamingQueryResult);
})
// indicate that the stream was aborted
.catch((err) => {
if (err.name === "AbortError") {
console.log("Request aborted as expected");
}
// abort the query after half a second
setTimeout(() => {
console.log("Sending abort signal");
controller.abort();
}, 500);
Constructors
-
Parameterproperties Objectoptional
See the properties for a full list of the properties that may be passed into the constructor.
Property Overview
Name | Type | Summary | Class |
---|---|---|---|
Custom quantization parameters for input geometry that compresses geometry for transfer to the server. | GraphQueryStreaming | ||
Specifies a set of parameters containing data to be included in the query. | GraphQueryStreaming | ||
The name of the class. | Accessor | ||
The Esri implementation of openCypher query to be executed against the knowledge graph. | GraphQuery | ||
Used to project the geometry onto a virtual grid, likely representing pixels on the screen. | GraphQueryStreaming | ||
Custom output spatial reference parameters for output geometry. | GraphQueryStreaming | ||
Enables provenance to be used and returned in the query. | GraphQueryStreaming |
Property Details
-
bindGeometryQuantizationParameters
bindGeometryQuantizationParameters InputQuantizationParameters
-
Custom quantization parameters for input geometry that compresses geometry for transfer to the server. Overrides the default lossless WGS84 quantization.
Example// sample implementation of an input quantization parameter // query entities within a bounding box const query = "MATCH (n) WHERE esri.graph.ST_Intersects($geom, n.geometry) RETURN n" KnowledgeGraphModule.executeQueryStreaming( knowledgeGraph, { openCypherQuery: query, bindParameters: { param_filter_geom: new Polygon({ rings: [ [ [-89, -89], [89, -89], [89, 89], [-89, 89], [-89, -89], ], ], }), }, inputQuantizationParameters: { xyResolution: 0.003, xFalseOrigin: 25, yFalseOrigin: 25, zResolution: 1, zFalseOrigin: 1, mResolution: 1, mFalseOrigin: 1, }, } } );
-
Specifies a set of parameters containing data to be included in the query.
Note
Check the service definition for the knowledgeGraphService for valid geometry types. Not all external graph stores will supported all geometry types.
Examples// bind a polygon to search for entities withing a bounding box. const query = "MATCH (n) WHERE esri.graph.ST_Intersects($geom, n.geometry) RETURN n"; KnowledgeGraphModule.executeQueryStreaming( knowledgeGraph, { openCypherQuery: query, bindParameters: { geom: new Polygon({ rings: [ [ [-89, -89], [89, -89], [89, 89], [-89, 89], [-89, -89], ], ], }), } }).then((streamingQueryResult)=>{ // streaming query returns a readableStream which must be read to access the returned graph data readStream(streamingQueryResult); })
// bind a list of ids // get all relationships between suppliers and a select list of parts. const query = "MATCH (s:Supplier)-[rel]-(p:Part) WHERE p.globalid IN $ids RETURN rel"; KnowledgeGraphModule.executeQueryStreaming( knowledgeGraph, { openCypherQuery: query, bindParameters: { ids:[ "{WIHPTR45-1RO5-9HY1-FK50-P1J5U8D1FER7}", "{SSOE5G4O-T7T8-4G8Y-2RD1-P8D1A7G4S7EA}", "{2SE8HE8D-81ES-9HY1-1SE8-OPAWON41869S}", "{1SE8E6G8-5DT8-S8E1-6S8E-D8R4FJ8S8S1S}", "{SE86I2BD-8R73-2SE8-6S8E-7R8DR86S4SD6}", "{N2T8K52R-2SDE-1SEG-S8ES-6SE8HUKY6AIN}", "{2AWAGP86-SESR-5SE8-4SE8-68RD66846SPL}", ], }), } }).then((streamingQueryResult)=>{ // streaming query returns a readableStream which must be read to access the returned graph data readStream(streamingQueryResult); })
-
openCypherQuery
InheritedPropertyopenCypherQuery String
Inherited from GraphQuery -
The Esri implementation of openCypher query to be executed against the knowledge graph.
Required
- knowledgeGraphService.executeQuery() will fail if not provided.
-
outputQuantizationParameters
outputQuantizationParameters OutputQuantizationParameters
-
Used to project the geometry onto a virtual grid, likely representing pixels on the screen. Geometry coordinates are converted to integers by building a grid with a resolution matching the
OutputQuantizationParameters.tolerance
. Each coordinate is then snapped to one pixel on the grid.Example// sample implementation of an output quantization parameter // query entities within a bounding box. const query = "MATCH (n) WHERE esri.graph.ST_Intersects($geom, n.geometry) RETURN n" KnowledgeGraphModule.executeQueryStreaming( knowledgeGraph, { openCypherQuery: query, bindParameters: { geom: new Polygon({ rings: [ [ [-89, -89], [89, -89], [89, 89], [-89, 89], [-89, -89], ], ], }), }, outputQuantizationParameters: { extent: { xmax: 30, xmin: 20, ymax: 30, ymin: 20, }, tolerance: 0.001, quantizeMode: "view", } } } );
-
outputSpatialReference
outputSpatialReference SpatialReference
-
Custom output spatial reference parameters for output geometry. Overrides the default lossless WGS84 quantization.
-
provenanceBehavior
provenanceBehavior String
-
Enables provenance to be used and returned in the query. This will only be available if the service has provenance enabled.
Possible Values:"exclude" |"include"
Example// query entities within a bounding box that have a specific provenance. const query = "MATCH (n), (prov:Provenance) WHERE esri.graph.ST_Intersects($geom, n.geometry) and prov.instanceID in $idList RETURN n, prov" KnowledgeGraphModule.executeQueryStreaming( knowledgeGraph, { openCypherQuery: query, provenanceBehavior: "include", bindParameters: { idList: ['{59ACF2B8-1A28-4825-9228-E0CE1AFF184A}', '{AFF1A752-1A28-4825-9228-EFFEE152184A}', '{4825AABB-1A28-4825-9228-E0CE1AFF9712}'], geom: new Polygon({ rings: [ [ [-89, -89], [89, -89], [89, 89], [-89, 89], [-89, -89], ], ], }), }, outputQuantizationParameters: { extent: { xmax: 30, xmin: 20, ymax: 30, ymin: 20, }, tolerance: 0.001, quantizeMode: "view", } } );
Method Overview
Name | Return Type | Summary | Class |
---|---|---|---|
Adds one or more handles which are to be tied to the lifecycle of the object. | Accessor | ||
Returns true if a named group of handles exist. | Accessor | ||
Removes a group of handles owned by the object. | Accessor |
Method Details
-
Inherited from Accessor
-
Adds one or more handles which are to be tied to the lifecycle of the object. The handles will be removed when the object is destroyed.
// Manually manage handles const handle = reactiveUtils.when( () => !view.updating, () => { wkidSelect.disabled = false; }, { once: true } ); this.addHandles(handle); // Destroy the object this.destroy();
ParametershandleOrHandles WatchHandle|WatchHandle[]Handles marked for removal once the object is destroyed.
groupKey *optionalKey identifying the group to which the handles should be added. All the handles in the group can later be removed with Accessor.removeHandles(). If no key is provided the handles are added to a default group.
-
hasHandles
InheritedMethodhasHandles(groupKey){Boolean}
Inherited from Accessor -
Returns true if a named group of handles exist.
ParametergroupKey *optionalA group key.
ReturnsType Description Boolean Returns true
if a named group of handles exist.Example// Remove a named group of handles if they exist. if (obj.hasHandles("watch-view-updates")) { obj.removeHandles("watch-view-updates"); }
-
Inherited from Accessor
-
Removes a group of handles owned by the object.
ParametergroupKey *optionalA group key or an array or collection of group keys to remove.
Exampleobj.removeHandles(); // removes handles from default group obj.removeHandles("handle-group"); obj.removeHandles("other-handle-group");