Forecast Data Trends with Confluent Platform for Apache Flink
Forecasting is an important use-case for Apache Flink®. You can deploy forecasting jobs using Confluent Manager for Apache Flink (CMF), a central management component of Confluent Platform for Apache Flink that enables users to securely manage a fleet of Flink jobs across multiple environments.
How does forecasting work?
Forecasting helps predict future values in time-series data by analyzing historical trends. In real-time streaming environments, continuous forecasting enables businesses to anticipate changes, optimize operations, and detect deviations as data flows in.
With Apache Flink®, you can perform continuous forecasting and anomaly detection directly within your familiar SQL-based environment.
Flink provides the Flink SQL forecast function, which enables you to perform real-time analysis and gain actionable insights from your streaming data without needing in-depth data science expertise. The ML_FORECAST function uses ARIMA (Autoregressive Integrated Moving Average), optimized for real-time performance, to deliver accurate forecasts in a streaming context.
Confluent Platform for Apache Flink also provides AI_FORECAST, which forecasts with pretrained
time-series foundation models that run in model servers in your Kubernetes
cluster. It needs no model parameters, forecasts from the 20th value of a
series onward by default, and returns a range for each forecast step. For details, see
Forecast with AI_FORECAST.
Your data must include:
A timestamp column.
A target column representing some quantity of interest at each timestamp.
ARIMA model
ARIMA, which stands for Autoregressive Integrated Moving Average, is a powerful statistical technique used for time series analysis and forecasting. It combines three key components to model and predict future values based on past data:
Autoregressive (AR): This component uses past values of the time series to predict future values. It assumes that current observations are influenced by previous observations.
Integrated (I): This refers to the differencing of raw observations to make the time series stationary. Stationary data has consistent statistical properties over time, which is crucial for accurate forecasting.
Moving Average (MA): This component incorporates the relationship between an observation and a residual error from a moving average model applied to previous observations.
ARIMA models are used widely in various fields, including finance, economics, and weather forecasting, to analyze time-dependent data and make predictions. They are particularly effective when dealing with non-seasonal time series data that exhibits trends or patterns over time.
The model is typically represented as ARIMA(p,d,q), where:
p: The number of autoregressive terms
d: The degree of differencing
q: The order of the moving average
By adjusting these parameters, analysts can fine-tune the model to best fit their specific time series data and improve forecasting accuracy.
Be aware that forecasting accuracy can vary greatly, based on many parameters, and there is no guarantee of correctness in predictions made by using ARIMA and machine learning.
Forecasting parameters
The following parameters are supported for forecasting.
Model configuration
Use the following parameters to configure the ML_FORECAST function. Pass
them as a JSON string in the optional third argument,
ML_FORECAST(value, timestamp[, config]), for example with JSON_OBJECT.
If you omit the configuration argument, the function uses the defaults below,
except that enableStl is TRUE.
enableStl
Default: FALSE (TRUE if you omit the configuration argument)
Enable STL (Seasonal-Trend decomposition using Loess smoothing) decomposition. If set to TRUE, the time series is decomposed into trend, seasonal, and remainder components.
STL is a statistical method that separates a time series into three additive components: trend, seasonal, and remainder.
The trend component captures the long-term direction or movement in the data.
The seasonal component captures regular, predictable patterns that repeat over fixed periods.
The remainder component captures random fluctuations that aren’t explained by trend or seasonality.
This decomposition helps improve forecasting accuracy by enabling the ARIMA model to better analyze and predict the underlying patterns in your data.
evalWindowSize
Default: 5
Validation: Must be >= 1 and <= 10000
Keeps the last evalWindowSize squared errors for both RMSE and AIC
calculations. RMSE uses the rolling sum of these errors, while AIC uses the
count (n) and sum of those same errors. A smaller window makes both metrics
more responsive to recent errors; a larger window smooths out short-term
noise.
An error is recorded only when a row arrives at the timestamp the model
forecast, which is the previous row’s timestamp plus the most common interval
between rows. If your timestamps are unevenly spaced, rmse and aic can
stay NULL.
horizon
Default: 5
Number of future time periods to forecast. This value defines how many steps ahead the model predicts.
m
Default: If
enableStlis TRUE, seasonality is estimated using ACF.Validation:
m>= 0 andm<= (minTrainingSize/ 2). SettingmrequiresenableStlto be TRUE.
The seasonal period length. If enableStl is TRUE, the model uses an ACF
(Autocorrelation Function) to estimate the seasonal period length. The ACF
measures the correlation between a time series and its lagged versions at
different time intervals. This helps to identify repeating patterns and
seasonal cycles in the data by detecting how strongly current values relate to
past values at various lags.
maxTrainingSize
Default: 128
Validation:
minTrainingSize<=maxTrainingSize<= 10000
Maximum number of historical data points used for training. This value limits
the training window to recent data. The default equals the default
minTrainingSize. If you set minTrainingSize above 128, also raise
maxTrainingSize, or the configuration fails validation.
minTrainingSize
Default: 128
Validation: 1 <=
minTrainingSize<=maxTrainingSize
Minimum number of data points required for training. The model won’t train until the quantity of available data is greater than this value.
p
Default: NULL
Validation: If provided, must be >= 0
Order of the AR (Autoregressive) term in the ARIMA model, which defines the number of lagged values used in the model. This value represents the number of past values that directly influence the current prediction.
If p is not provided, the model uses auto-ARIMA to determine the best
order.
q
Default: NULL
Validation: If provided, must be >= 0
Order of the MA (Moving Average) term in the ARIMA model, which defines the number of lagged error terms in the model. This value represents the number of past prediction errors that influence the current prediction.
If q is not provided, the model uses auto-ARIMA to determine the best
order.
d
Default: NULL
Validation: If provided, must be >= 0
Order of differencing in the ARIMA model, which defines the number of times the data is differenced to achieve stationarity. A value of 0 means no differencing, 1 means first difference, and so on.
If d is not provided, the model uses auto-ARIMA to determine the best
order.
updateInterval
Default: 1
Validation: Must be > 0
Frequency of model updates. This value defines how often the model retrains with new data.
Forecast results
The ML_FORECAST function returns an array with one element for each of the
horizon forecast steps. Each element is a row with the following fields, in
the order timestamp, forecast_value, lower_bound, upper_bound,
rmse, and aic. The array is empty until the model has trained.
aic
Type: DOUBLE
The AIC (Akaike Information Criterion) at the time the forecast was generated,
calculated over the last evalWindowSize errors.
AIC is a statistical measure used to evaluate and compare the quality of different models. It balances model accuracy against complexity by penalizing models with more parameters. Lower AIC values indicate better model performance, which helps to identify the optimal model configuration that provides the best fit to the data without overfitting.
forecast_value
Type: DOUBLE
The forecast value at the timestamp.
lower_bound
Type: DOUBLE
The lower confidence interval for the forecasted value.
rmse
Type: DOUBLE
The RMSE (Root Mean Square Error) at the time the forecast was generated,
calculated over the last evalWindowSize errors.
RMSE measures the difference between the predicted and actual values in a time series by calculating the square root of the average of the squared differences between the predicted and actual values. A lower RMSE indicates better model performance, because it means the model’s predictions are closer to the actual values.
timestamp
Type: TIMESTAMP
The LocalDateTime for this forecast.
upper_bound
Type: DOUBLE
The upper confidence interval for the forecasted value.
Example
Create a test table that has a timestamp field for your time series data.
CREATE TABLE orders ( total_orderunits double, summed_ts TIMESTAMP(3) NOT NULL, WATERMARK FOR summed_ts AS summed_ts );
Insert mock time series data into the table.
INSERT INTO orders (total_orderunits, summed_ts) VALUES (102.12, TIMESTAMP '2024-11-19 10:00:00.000'), (103.45, TIMESTAMP '2024-11-19 10:01:00.000'), (101.89, TIMESTAMP '2024-11-19 10:02:00.000'), (104.23, TIMESTAMP '2024-11-19 10:03:00.000'), (102.78, TIMESTAMP '2024-11-19 10:04:00.000'), (102.12, TIMESTAMP '2024-11-19 10:05:00.000'), (103.45, TIMESTAMP '2024-11-19 10:06:00.000'), (101.89, TIMESTAMP '2024-11-19 10:07:00.000'), (104.23, TIMESTAMP '2024-11-19 10:08:00.000'), (102.78, TIMESTAMP '2024-11-19 10:09:00.000'), (102.12, TIMESTAMP '2024-11-19 10:10:00.000'), (103.45, TIMESTAMP '2024-11-19 10:11:00.000'), (101.89, TIMESTAMP '2024-11-19 10:12:00.000'), (104.23, TIMESTAMP '2024-11-19 10:13:00.000'), (102.78, TIMESTAMP '2024-11-19 10:14:00.000'), (102.12, TIMESTAMP '2024-11-19 10:15:00.000'), (103.45, TIMESTAMP '2024-11-19 10:16:00.000'), (101.89, TIMESTAMP '2024-11-19 10:17:00.000'), (104.23, TIMESTAMP '2024-11-19 10:18:00.000'), (102.78, TIMESTAMP '2024-11-19 10:19:00.000'), (102.12, TIMESTAMP '2024-11-19 10:20:00.000'), (103.45, TIMESTAMP '2024-11-19 10:21:00.000'), (101.89, TIMESTAMP '2024-11-19 10:22:00.000'), (104.23, TIMESTAMP '2024-11-19 10:23:00.000'), (102.78, TIMESTAMP '2024-11-19 10:24:00.000'), (102.12, TIMESTAMP '2024-11-19 10:25:00.000'), (103.45, TIMESTAMP '2024-11-19 10:26:00.000'), (101.89, TIMESTAMP '2024-11-19 10:27:00.000'), (104.23, TIMESTAMP '2024-11-19 10:28:00.000'), (102.78, TIMESTAMP '2024-11-19 10:29:00.000');
Run the following statement to perform a forecast on the mock data in the
orderstable.SELECT ML_FORECAST(total_orderunits, summed_ts, JSON_OBJECT('p' VALUE 1, 'q' VALUE 1, 'd' VALUE 1, 'minTrainingSize' VALUE 10, 'enableStl' VALUE false, 'horizon' VALUE 5)) OVER ( ORDER BY summed_ts RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS forecast FROM orders;
Run the following statement to perform a forecast with AutoArima on the mock data in the
orderstable.SELECT ML_FORECAST(total_orderunits, summed_ts, JSON_OBJECT('minTrainingSize' VALUE 10, 'enableStl' VALUE false)) OVER ( ORDER BY summed_ts RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS forecast FROM orders;
Forecast with AI_FORECAST
Important
AI_FORECAST is a preview feature. Interfaces might change in future
releases.
AI_FORECAST sends the recent values of a series to a time-series foundation
model, and returns the model’s forecast of the next values, each with a range.
The models are pretrained, so the function needs no parameters and no
training data. With the default settings, it forecasts from the 20th value of
a series onward.
The models run in model servers in your Kubernetes cluster. Before you can call the function, an administrator must deploy the model servers and register the function in your environment, as described in Set Up AI Functions in Confluent Manager for Apache Flink.
Syntax
AI_FORECAST(value, timestamp[, config])
OVER (ORDER BY timestamp RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
valueThe value of the series, as a
DOUBLE. The function leaves rows whose value isNULL,NaN, or infinite out of the series.timestampThe time of the value, as a
TIMESTAMP(3). The function takes the series’ step from the median interval between timestamps, and gives each forecast step the timestamp of the last value plus a multiple of that step, so the timestamps of a series should be evenly spaced.configOptional. A JSON object, as a string or from
JSON_OBJECT, with the settings in the following table. The configuration must be the same for every row of a series.
Call the function from a statement whose current catalog is the environment
catalog, _env_<environment>, and whose current database is default.
To forecast several series in one statement, add PARTITION BY to the
OVER clause.
Configuration
Key |
Default |
Description |
|---|---|---|
|
|
The model to forecast with: |
|
|
The number of steps to forecast, at least 1. The model servers refuse more than 96. |
|
|
The number of values the function waits for before it forecasts. At least 2. |
|
|
The largest number of recent values that the function sends to the
model. At least |
Keys are case-insensitive. For example, the following call forecasts 12 steps with FlowState:
AI_FORECAST(v, ts, JSON_OBJECT('model' VALUE 'flowstate', 'horizon' VALUE 12))
Results
AI_FORECAST returns a row with the following fields:
Field |
Type |
Description |
|---|---|---|
|
|
One element for each forecast step: its timestamp, the forecast value,
and the 10th, 50th, and 90th percentile of the forecast. The array is
empty until the series has |
|
|
A JSON object from the model server that describes the model that made the forecast. |
Arrays in Flink SQL start at index 1, so forecast[1] is the first step.
timestamp is a reserved word, so quote it with backticks when you read the
field, as the following example does.
Example
Create a table that generates a series with one value per second, in the environment catalog:
CREATE TABLE sensor_readings ( id BIGINT, ts AS CAST(TIMESTAMPADD(SECOND, CAST(id AS INT), TIMESTAMP '2026-01-01 00:00:00') AS TIMESTAMP(3)), WATERMARK FOR ts AS ts ) WITH ( 'connector' = 'datagen', 'fields.id.kind' = 'sequence', 'fields.id.start' = '1', 'fields.id.end' = '10000', 'rows-per-second' = '20' );
Forecast the next five values of a wave with a period of 24 seconds, and read the first step:
SELECT id, v, CARDINALITY(f.forecast) AS steps, f.forecast[1].`timestamp` AS next_ts, f.forecast[1].mean AS next_mean, f.forecast[1].q10 AS next_q10, f.forecast[1].q90 AS next_q90 FROM ( SELECT id, v, AI_FORECAST(v, ts, JSON_OBJECT('model' VALUE 'ttm')) OVER (ORDER BY ts RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS f FROM ( SELECT id, ts, CAST(10 + 5 * SIN(2 * PI() * id / 24) + 0.3 * SIN(id * 1.7) AS DOUBLE) AS v FROM sensor_readings ) );
The first 19 rows have no steps. From row 20 on, each row has five steps, and
next_tsis one second after the row’s timestamp.
Limitations
Each row sends one request to the model server and waits for the answer, so a statement processes rows only as fast as its model server answers.
timesfm-2.5is not available, because no model server for it is provided.
For the errors that the function reports, see the troubleshooting section of Set Up AI Functions in Confluent Manager for Apache Flink.