Skip to main content

Stream Data Access

Stream data provides access to time-series data stored in CrateDB. While runtime queries retrieve the current state of entities from MongoDB, stream data queries retrieve historical measurements and events recorded over time — such as sensor readings, machine metrics, or energy production values.

Stream Data vs Runtime Queries

Stream DataRuntime
PurposeHistorical time-series dataCurrent entity state
StorageCrateDBMongoDB
Key fieldtimestamprtId
Typical dataSensor readings, measurements, eventsEntities, configurations, relationships
Query rootstreamDataruntime

API Approaches

OctoMesh provides three ways to query stream data:

ApproachEndpointUse Case
TypedstreamData.[typeName]Strongly typed queries for a known CK type. Returns typed fields directly.
TransientstreamData.transientStreamDataQueryAd-hoc queries where columns and filters are specified at request time.
PersistedstreamData.streamDataQueryExecute a saved query definition by its rtId.

Query Types

Query TypeDescriptionDetail Page
Simple QueryRetrieve raw time-series rows with column selection, filtering, and sortingSimple Query
Aggregation QueryCompute aggregated values (AVG, MIN, MAX, COUNT, SUM) across data pointsAggregation Queries
Grouped Aggregation QueryAggregate values grouped by one or more columnsAggregation Queries
Downsampling QueryReduce data density by bucketing time ranges with aggregationDownsampling Query

All query types are available as both transient (ad-hoc) and persisted (saved) variants.


Typed Query

The streamData query root exposes typed fields for each CK type that has stream data enabled. Field names are derived from the CK type's full name (with dots and slashes removed) in camelCase. For example, the CK type Industry.Energy/EnergyMeter becomes industryEnergyEnergyMeter.

query {
streamData {
industryEnergyEnergyMeter {
items {
rtId
ckTypeId
timeStamp
voltage
}
}
}
}

A response to this query can look like the following:

{
"data": {
"streamData": {
"industryEnergyEnergyMeter": {
"items": [
{
"rtId": "65dc6d24cc529cdc46c84fcc",
"ckTypeId": "Industry.Energy/EnergyMeter",
"timeStamp": "2024-03-21T16:22:47.676Z",
"voltage": 4.04196210149963
},
{
"rtId": "65dc6d24cc529cdc46c84fcc",
"ckTypeId": "Industry.Energy/EnergyMeter",
"timeStamp": "2024-03-21T16:23:50.675Z",
"voltage": -9.00808094383129
}
]
}
}
}
}

Typed queries return attribute values as named fields (e.g., voltage). For more flexible queries where you choose columns at request time, use the transient query approach instead.


Pagination

Stream data queries support cursor-based pagination using the first and after parameters. The first parameter specifies the number of items to return, while after indicates the cursor position to start fetching from.

query {
streamData {
industryEnergyEnergyMeter(first: 2, after: "YXJyYXljb25uZWN0aW9uOjE=") {
pageInfo {
endCursor
hasNextPage
}
items {
rtId
ckTypeId
timeStamp
voltage
}
}
}
}

The response includes pageInfo with endCursor (use as the after value for the next page) and hasNextPage (indicates whether more data is available). The totalCount field is also available on all stream data connections.


Common Parameters

StreamDataArguments

The StreamDataArguments input type is used to specify time filtering and limits when executing queries:

FieldTypeRequiredDescription
fromDateTimeNoStart of the time range to query
toDateTimeNoEnd of the time range to query
limitIntNoMaximum number of data points to return
queryModeQueryModeYesQuery execution mode (use DEFAULT)
intervalSecondsNoTime interval for downsampling

FieldFilter

Field-level filters allow you to restrict results based on attribute values:

FieldTypeRequiredDescription
attributePathStringYesThe attribute to filter on
operatorFieldFilterOperatorsYesComparison operator
comparisonValueSimpleScalarNoValue to compare against

Available operators: EQUALS, NOT_EQUALS, GREATER_THAN, GREATER_EQUAL_THAN, LESS_THAN, LESS_EQUAL_THAN, LIKE, NOT_IN, IN, ANY_EQ, ANY_LIKE, MATCH_REG_EX

Sort

FieldTypeRequiredDescription
attributePathStringYesThe attribute to sort by
sortOrderSortOrdersNoASCENDING, DESCENDING, or DEFAULT

Result Structure

All stream data queries return a StreamDataQueryRowDtoConnection with the following structure:

FieldTypeDescription
totalCountIntTotal number of matching rows
pageInfoPageInfoPagination cursor information
items[StreamDataQueryRow]The result rows

Each StreamDataQueryRow contains:

FieldTypeDescription
rtIdOctoObjectIdThe runtime entity this data point belongs to
ckTypeIdRtCkTypeIdThe CK type identifier
timestampDateTimeThe timestamp of the data point
rtWellKnownNameStringOptional well-known name of the entity
cellsRtQueryCellDtoConnectionData values as attributePath / value pairs

The cells connection is used by transient and persisted queries. Each cell contains an attributePath (the column name) and a value (the data point value). Typed queries return values as named fields directly instead.

note

Typed queries use timeStamp (capital S) as the field name, while transient and persisted queries use timestamp (lowercase). This difference reflects the underlying data types used by each query approach.


Further Reading