Skip to main content

Streaming History Read

Overview​

IDE When loading historical data for a trend chart, loading ten-thousand data points all at once List can cause EDT lag OOM.readHistoryStream Returns Stream<OpcUaDataValue> to the business side and process forEach one data point at a time.

Corresponding section of the standard: OPC UA Part 11(Historical Access) §6.5.

API​

MethodsCategoryReading and WritingNote
ua.readHistoryStream(nodeId, start, end, batchSize)HistoryReadReturns Stream<OpcUaDataValue>
ua.readHistoryStream(nodeId, start, end)HistoryReadSame as above; batchSize is fixed at 1000
ParametersDefaultNote
nodeId—Historical Milestone (String)
start—Start Instant
end—End Instant
batchSize1000Maximum RPC Points Per Time

Exception / Null Case: When the value nodeId is null or an empty stringThrow IllegalArgumentException("nodeId"), Does not return an empty stream; start / Returns when start the value is end null or end no later than Stream.empty(); Also returns an empty stream if the underlying read fails.

Code Example​

import com.darra.opcua.*;
import java.time.*;
import java.util.Iterator;
import java.util.stream.Stream;

try (OpcUaSession ua = new OpcUaSession("opc.tcp://localhost:4840")) {
ua.connect();

Instant t1 = Instant.now();
Instant t0 = t1.minus(Duration.ofHours(24));

// 1) Render as you go
try (Stream<OpcUaDataValue> s = ua.readHistoryStream("ns=2;s=Temp", t0, t1, 1000)) {
s.forEach(dv -> {
try (dv) {
// Push to Trend Chart
System.out.println(dv.sourceTimestamp() + " = " + dv.variant().asDouble());
}
});
}

// 2) Stream filtering (Each DV must be closed, including those that are filtered out)
long badCount = 0;
try (Stream<OpcUaDataValue> s = ua.readHistoryStream("ns=2;s=Temp", t0, t1, 1000)) {
Iterator<OpcUaDataValue> it = s.iterator();
while (it.hasNext()) {
try (OpcUaDataValue dv = it.next()) {
if (!dv.isGood()) badCount++;
}
}
}
System.out.println("24h 内 " + badCount + " 个坏值");
}

Implementation Constraints​

  • Currently, readHistory no points are exposed ContinuationPoint; a single call returns RPC all points within the interval (subject to the batchSize limit).
  • Business units in large regions can slice their own data
  • You must close each DV; otherwise, a memory leak will occur.

Best Practices​

  • The try-with-resources package: Stream
  • For time intervals greater than 1 hour, it is recommended to use outer slices.
  • UI Render Throttling: Every 100 refreshes

Cross-Language Comparison​

C#PythonJavaC++RustC
ReadHistoryStreamAsyncread_history_streamreadHistoryStreamReadHistoryStreamread_history_streamDarraUa_HistoryStream_New/_Next/_IsEnd/_Free

Next Step​