Five Common Time-Series Queries Made Simple With Enterprise Java
Explore Eclipse JNoSQL's five new Java fluent API operations for querying time-series data across InfluxDB, QuestDB, and Apache IoTDB.
Enterprise applications have typically stored only the current state of information, such as account balances, order statuses, or inventory quantities. However, monitoring changes to these values over time is also essential. As businesses increasingly depend on real-time analytics, observability, and continuous data processing, time-series databases provide a specialized solution for storing and querying time-based data.
Java developers and software architects should not have to learn a new query language for each database when working with temporal data. Eclipse JNoSQL introduces five new operations in its TimeSeries fluent API, making it easier to retrieve historical data, recent activity, and specific time ranges using familiar Java patterns. This article discusses the significance of time-series data for enterprise applications and demonstrates how these features simplify temporal queries while supporting polyglot persistence.
Why Time-Series Data Matters for Enterprise Applications
Traditional databases manage transactional information well, but temporal workloads require different access patterns. For example, a banking application may store an account balance. Knowing a customer has €5,000 is helpful, but tracking how that balance changes throughout the day can reveal unusual activity, spending patterns, or potential fraud. Rather than retrieving a single state, the application must analyze a sequence of observations over time.
This is the key distinction of time-series data: time is central to how information is stored, retrieved, and analyzed. Time-series databases are built for frequent ingestion, chronological retrieval, and time-based aggregations, though their features and trade-offs differ by implementation.
These features make time-series databases valuable in various enterprise scenarios:
- Financial services: Analyze transaction histories, market fluctuations, and unusual activity within specific time windows.
- Application observability: Monitor response times, error rates, and resource consumption to investigate performance degradation or incidents.
- E-commerce: Track purchasing activity, demand fluctuations, and inventory movements to identify trends and seasonal behavior.
- IoT and industrial systems: Collect temperature, pressure, energy consumption, and equipment measurements to support monitoring and predictive maintenance.
- Business analytics: Compare revenue, customer engagement, and operational performance across different periods.
Returning to the financial example, an application may need to retrieve the three most recent transactions, identify the earliest activity, review transactions since a specific timestamp, or analyze all activity from the past 24 hours. While these are common business needs, implementing them across different databases often requires unique query syntax and database-specific APIs.
Traditional relational databases can handle temporal queries, especially with proper indexing and partitioning. The choice of architecture depends on ingestion volume, retention needs, query patterns, and operational costs. Specialized time-series databases are most beneficial when temporal workloads are the primary requirement.
For enterprise Java developers, another challenge with databases like InfluxDB, QuestDB, and Apache IoTDB, as each offers distinct query capabilities and integration models. Eclipse JNoSQL addresses this by extending its fluent API with five temporal operations: latest(), earliest(), since(), until(), and last(). Let’s examine how these methods enable common temporal queries directly in Java.
Working With Time-Series Data in Eclipse JNoSQL
Eclipse JNoSQL extends its polyglot persistence to time-series databases, enabling Java developers to manage temporal data with familiar persistence patterns. The project currently integrates with InfluxDB, QuestDB, and Apache IoTDB. These databases offer distinct features, such as time-based analytics, high-throughput ingestion, and industrial telemetry. Eclipse JNoSQL delivers a consistent Java programming model across them.
This integration builds on three complementary Jakarta specifications:
- Jakarta NoSQL: Provides entity mapping, annotations, and template-based persistence.
- Jakarta Data: Introduces repository abstractions, reducing boilerplate for common persistence operations.
- Jakarta Query: Defines a common query foundation across Jakarta persistence technologies, with Eclipse JNoSQL integration under development.
For time-series operations, Eclipse JNoSQL introduces TimeSeriesTemplate, a specialized version of Jakarta NoSQL’s Template interface. It maintains standard persistence operations and adds features for temporal queries.
Mapping Temporal Data
Consider a financial application that tracks account transactions over time. Rather than storing only the current balance, each transaction is represented as an individual observation:
@Entity
public class AccountTransaction {
@Id
private Instant id;
@Column
private String account;
@Column
private BigDecimal amount;
@Column
private String currency;
@Column
private TransactionStatus status;
The mapping uses standard Jakarta NoSQL annotations, but the identifier must represent time. In this example, Instant indicates when the transaction occurred and serves as the temporal reference for queries.
Unlike conventional entity identifiers that use strings, UUIDs, or numbers, the time-series fluent API requires a supported temporal type for the @Id field. Supported types include Instant, LocalDateTime, LocalDate, OffsetDateTime, and ZonedDateTime. This enables the API to interpret operations chronologically without requiring developers to specify a timestamp field in each query.
Timestamps are not guaranteed to be unique across all observations. Applications handling concurrent events should consider timestamp precision, potential collisions, and the database provider's identity model.
Once the entity is mapped, you can persist an observation using TimeSeriesTemplate:
@Inject
TimeSeriesTemplate template;
template.insert(transaction);
For standard operations, developers can also use Jakarta Data repositories:
@Repository
public interface AccountTransactionRepository
extends BasicRepository<AccountTransaction, Instant> {
List<AccountTransaction> findByAccountOrderByIdDesc(
String account, Limit limit);
}
Both approaches complement each other: repositories simplify common persistence tasks, while TimeSeriesTemplate provide specialized temporal queries.
However, enterprise applications often need more than basic insert and retrieve operations. Common questions include: What are the latest transactions? What happened during a specific period? Which observations occurred within the last 24 hours?
Exploring the Five New Time-Series Operations
Eclipse JNoSQL extends TimeSeriesTemplate with five fluent operations: latest(), earliest(), since(), until(), and last(). These methods let developers express common temporal queries directly in Java, eliminating the need for database-specific query languages.
For example, consider a banking application that records six transactions for the same account at different intervals.
For simplicity, we use a fixed reference time of 12:00 PM for the following observations:
| time | amount | status |
|---|---|---|
| 3 days ago, 12:00 PM | €10 | APPROVED |
| 2 days ago, 12:00 PM | €11 | DECLINED |
| 30 hours ago | €12 | APPROVED |
| 20 hours ago | €13 | DECLINED |
| 10 hours ago | €14 | APPROVED |
| 2 hours ago | €15 | DECLINED |
You can insert these observations using the TimeSeriesTemplate API:
Instant now = Instant.now();
List<Long> hours = List.of(72L, 48L, 30L, 20L, 10L, 2L);
for (int i = 0; i < hours.size(); i++) {
Instant timestamp = now.minus(Duration.ofHours(hours.get(i)));
template.insert(new AccountTransaction(
timestamp,
"account-01",
BigDecimal.valueOf(10 + i),
"EUR",
i % 2 == 0 ? APPROVED : DECLINED
));
}
Each observation uses an Instant as its timestamp. For the following examples, assume queries run immediately after insertion.
1. latest() — Retrieve the Most Recent Observations
A customer can check their three latest transactions without manually sorting the entire history by requesting the newest observations directly.
List<AccountTransaction> transactions = template
.select(AccountTransaction.class)
.latest(3);
Expected results, newest first:
2 hours ago | €15 | DECLINED
10 hours ago | €14 | APPROVED
20 hours ago | €13 | DECLINED
The latest(3) operation returns up to three observations, ordered from newest to oldest. This is useful for recent transactions, monitoring dashboards, and activity feeds.
2. earliest() — Retrieve the Oldest Observations
An auditing process may need to identify the first transactions recorded for an account.
List<AccountTransaction> transactions = template
.select(AccountTransaction.class)
.earliest(3);
Expected results, oldest first:
72 hours ago | €10 | APPROVED
48 hours ago | €11 | DECLINED
30 hours ago | €12 | APPROVED
Unlike latest(), earliest() starts with the oldest observations. This is useful for auditing, historical analysis, and identifying when a sequence of events began.
3. since() — Retrieve Observations From a Specific Time
A fraud detection service may need all transactions recorded after a specific event, rather than a fixed number of observations.
Instant start = now.minus(Duration.ofHours(36));
List<AccountTransaction> transactions = template
.select(AccountTransaction.class)
.since(start)
.result();
Matching observations:
30 hours ago | €12 | APPROVED
20 hours ago | €13 | DECLINED
10 hours ago | €14 | APPROVED
2 hours ago | €15 | DECLINED
The since() operation includes observations at or after the specified timestamp. Unlike latest(), it does not impose a fixed result count.
4. until() — Retrieve Observations Before a Cutoff
A financial reconciliation process may need to examine transactions recorded before a specific cutoff and exclude more recent activity.
Instant end = now.minus(Duration.ofHours(12));
List<AccountTransaction> transactions = template
.select(AccountTransaction.class)
.until(end)
.result();
Matching observations:
72 hours ago | €10 | APPROVED
48 hours ago | €11 | DECLINED
30 hours ago | €12 | APPROVED
20 hours ago | €13 | DECLINED
The until() operation includes observations at or before the specified timestamp. It is useful for historical reporting, reconciliation, and investigating earlier application states.
5. last() — Retrieve Observations Within a Relative Period
A monitoring service may need to check transactions recorded during the last 24 hours. Unlike since(), which requires a specific timestamp, last() accepts a duration relative to the query execution time.
List<AccountTransaction> transactions = template
.select(AccountTransaction.class)
.last(Duration.ofHours(24))
.result();
Matching observations:
20 hours ago | €13 | DECLINED
10 hours ago | €14 | APPROVED
2 hours ago | €15 | DECLINED
This operation is useful for rolling windows, such as recent errors, daily financial activity, or application metrics.
Combining Temporal Operations
The real flexibility comes from combining these operations. For example, imagine retrieving the two most recent transactions within the last 24 hours:
List<AccountTransaction> transactions = template
.select(AccountTransaction.class)
.last(Duration.ofHours(24))
.latest(2);
Expected results:
2 hours ago | €15 | DECLINED
10 hours ago | €14 | APPROVED
You can also combine conventional conditions with temporal ranges. For example, to get the latest three transactions for a specific account between 36 and 12 hours ago:
List<AccountTransaction> transactions = template
.select(AccountTransaction.class)
.where("account").eq("account-01")
.since(now.minus(Duration.ofHours(36)))
.until(now.minus(Duration.ofHours(12)))
.latest(3);
Expected results:
20 hours ago | €13 | DECLINED
30 hours ago | €12 | APPROVED
Only two transactions match these conditions, even though latest(3) allows up to three results. This shows how temporal operations and traditional filtering work together without requiring database-specific queries.
These five operations offer a more expressive way to work with temporal data while preserving the Eclipse JNoSQL fluent programming model. Developers can focus on business needs instead of manually constructing timestamp conditions for each database.
The complete implementation, including executable examples using InfluxDB, QuestDB, and Apache IoTDB, is available in the Eclipse JNoSQL demos repository.
Conclusion
Time-series databases have become increasingly relevant for enterprise applications that need to analyze information over time, from financial transactions to observability and IoT. With the five new fluent API operations — latest(), earliest(), since(), until(), and last() — Eclipse JNoSQL simplifies temporal queries while preserving familiar Java persistence patterns. By supporting InfluxDB, QuestDB, and Apache IoTDB, it also provides flexibility for different architectural requirements. Explore the complete source code and discover more about enterprise Java persistence at the Eclipse JNoSQL website.
Opinions expressed by DZone contributors are their own.