Java Guide
Installation
bash
cmake -B build -DDDSTREAM_BUILD_JAVA=ON -DCMAKE_BUILD_TYPE=Release
cmake --build build -j$(nproc)
export LD_LIBRARY_PATH="$(pwd)/build/source/bindings/java:$LD_LIBRARY_PATH"
# Compile and run
javac -cp build/source/bindings/java/ddstream.jar MyApp.java
java -cp ".:build/source/bindings/java/ddstream.jar" \
-Djava.library.path=build/source/bindings/java \
MyAppBuilder Pattern
DDStreamConfig uses a fluent builder. All fields have defaults matching the C++ defaults.
java
import com.ddstream.*;
import java.time.Instant;
long now = Instant.now().getEpochSecond();
DDStreamConfig cfg = new DDStreamConfig.Builder()
.apiKey("YOUR_DD_API_KEY")
.appKey("YOUR_DD_APP_KEY")
.query("avg:system.cpu.user{*}")
.rangeStart(now - 86400) // last 24 hours
.rangeEnd(now)
.granularity(60)
.chunkSeconds(14400)
.enablePrefetch(true)
.logLevel(0)
.build();
// Validate credentials without streaming
String err = DDStream.validate(cfg);
if (err != null) {
throw new RuntimeException("Invalid credentials: " + err);
}Iteration
DDStream implements AutoCloseable and Iterable. Use try-with-resources
to guarantee the native handle is freed even on exceptions.
java
try (DDStream stream = new DDStream(cfg)) {
// Range-for (simplest)
for (DDStream.Batch batch : stream) {
System.out.printf("Batch: %d points%n", batch.timestamps.length);
}
}
// Explicit pull with error checking
try (DDStream stream = new DDStream(cfg)) {
stream.reset();
while (stream.hasNext()) {
DDStream.Batch batch = stream.nextBatch();
if (batch == null) {
System.err.println("Error: " + stream.lastError());
break;
}
System.out.printf("Progress: %.0f%%%n", stream.progress() * 100);
}
}Primitive Arrays
Each Batch contains parallel primitive arrays. The JNI binding uses
GetPrimitiveArrayCritical to write directly into JVM-managed arrays
without intermediate copies.
java
DDStream.Batch batch = stream.nextBatch();
long[] timestamps = batch.timestamps; // Unix epoch seconds
double[] avg = batch.avg;
double[] min = batch.min;
double[] max = batch.max;
double[] sum = batch.sum;
// Chunk metadata
long startMs = batch.chunkStartMs;
long endMs = batch.chunkEndMs;
boolean isLast = batch.isLast;
// Compute stats
double total = 0;
for (double v : avg) total += v;
System.out.printf("Mean CPU: %.2f%%%n", total / avg.length);
// BatchStats
DDStream.BatchStats stats = stream.lastBatchStats();
System.out.printf("Batch #%d, %d points, %d retries%n",
stats.batchNumber, stats.pointsCount, stats.httpRetries);