Ibis for streaming
Ibis has support for streaming operations, which can be executed on Flink, Spark Structured Streaming, and RisingWave.
Setup
We demonstrate the streaming operations with a real-time fraud detection example. If you have Kafka set up in your infrastructure, you can connect to your existing Kafka topics as well.
You can find our code setup here. Feel free to clone the repository if you want to follow along.
Window aggregation
Computes aggregations over windows.
The output schema consists of window_start
, window_end
, the group by column if applicable (optional), and the aggregation results.
Tumble and hop windows are supported. Tumbling windows have a fixed size and do not overlap. Hopping windows (aka sliding windows) are configured by both window size and window slide. The additional window slide parameter controls how frequently a sliding window is started.
For more, see Flink’s documentation on Windowing TVFs and Spark’s documentation on time windows.
= con.table("payment") # table corresponding to the `payment` topic
t
# tumble window
= (
expr =t.createTime)
t.window_by(time_col=ibis.interval(seconds=30))
.tumble(size=["provinceId"], avgPayAmount=_.payAmount.mean())
.agg(by
)
# hop window
= (
expr =t.createTime)
t.window_by(time_col=ibis.interval(seconds=30), slide=ibis.interval(seconds=15))
.hop(size=["provinceId"], avgPayAmount=_.payAmount.mean())
.agg(by )
Over aggregation
Computes aggregate values for every input row, over either a row range or a time range.
Spark Structured Streaming does not support aggregation using the OVER
syntax. You need to use window aggregation to aggregate over time windows.
= (
expr
t.select(=t.provinceId,
province_id=t.payAmount.sum().over(
pay_amountrange=(-ibis.interval(seconds=10), 0),
=t.provinceId,
group_by=t.createTime,
order_by
),
) )
Stream-table join
Joining a stream with a static table.
= (
provinces "Beijing",
"Shanghai",
"Hangzhou",
"Shenzhen",
"Jiangxi",
"Chongqing",
"Xizang",
)= pd.DataFrame(
province_id_to_name_df enumerate(provinces), columns=["provinceId", "province"]
)= t.join(province_id_to_name_df, ["provinceId"]) expr
Stream-stream join
Joining two streams.
= con.table("order") # table corresponding to the `order` topic
order = t.join(
expr == order.orderId, t.createTime == order.createTime]
order, [t.orderId )