redpanda_transform_sdk/lib.rs
1// Copyright 2023 Redpanda Data, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Redpanda Data Transforms Rust library.
16//!
17//! Data transforms let you run common data streaming tasks, like filtering,
18//! scrubbing, and transcoding, within Redpanda. For example, you may have consumers
19//! that require you to redact credit card numbers or convert JSON to Avro.
20//!
21//! Data transforms use a WebAssembly (Wasm) engine inside a Redpanda broker.
22//! Thus, transforms must compile to WebAssembly via `--target=wasm32-wasi`.
23//! A Wasm function acts on a single record in an input topic.
24//! You can develop and manage data transforms with `rpk transform` commands.
25//!
26//! This crate provides a framework for writing transforms.
27//!
28//! [`on_record_written`]: Transforms individual records after they have been written to an input topic.
29//! Written records are output to the destination topic.
30
31use std::fmt::Debug;
32
33pub use redpanda_transform_sdk_types::*;
34
35/// Register a callback to be fired when a record is written to the input topic.
36///
37/// This callback is triggered after the record has been written and fsynced to disk and the
38/// producer has been acknowledged.
39///
40/// This method blocks and runs forever, it should be called from `main`. Any setup that is
41/// needed prior to transforming any records can be done before calling this method.
42///
43/// # Examples
44///
45/// ```no_run
46/// extern crate redpanda_transform_sdk as redpanda;
47///
48/// use redpanda::*;
49/// use anyhow::Result;
50///
51/// fn main() {
52/// on_record_written(my_transform);
53/// }
54///
55/// // A transform that duplicates the record on the input topic to the output topic.
56/// fn my_transform(event: WriteEvent, writer: &mut RecordWriter) -> Result<()> {
57/// writer.write(&Record::new(
58/// event.record.key().map(|k| k.to_owned()),
59/// event.record.value().map(|v| v.to_owned()),
60/// ))?;
61/// Ok(())
62/// }
63/// ```
64pub fn on_record_written<E, F>(cb: F) -> !
65where
66 E: Debug,
67 F: Fn(WriteEvent, &mut RecordWriter) -> Result<(), E>,
68{
69 redpanda_transform_sdk_sys::process(cb)
70}