Convert Timestamps using Rust
This lab uses data transforms with the Redpanda Schema Registry to convert keys or values with timestamps across various formats. Written in Rust, the example shows how to transform numeric epoch values in milliseconds to string-based formats and vice versa.
Prerequisites
You must have the following:
-
At least version 1.75 of Rust installed on your host machine.
-
The Wasm target for Rust installed. To install this target, run the following:
rustup target add wasm32-wasip1 -
Install
rpkon your host machine. -
Docker and Docker Compose installed on your host machine.
Run the lab
-
Clone this repository:
git clone https://github.com/redpanda-data/redpanda-labs.git -
Change into the
data-transforms/ts-converter/directory:cd redpanda-labs/data-transforms/rust/ts-converter -
Set the
REDPANDA_VERSIONenvironment variable to at least version 24.1.2. For all available versions, see the GitHub releases.For example:
export REDPANDA_VERSION=v26.1.9 -
Set the
REDPANDA_CONSOLE_VERSIONenvironment variable to the version of Redpanda Console that you want to run. For all available versions, see the GitHub releases.For example:
export REDPANDA_CONSOLE_VERSION=v3.7.3 -
Start Redpanda in Docker by running the following command:
docker compose up -d --wait -
Set up your rpk profile:
rpk profile create ts-converter --from-profile profile.yml -
Create the required input topic:
rpk topic create src sink -
Create a source schema:
echo '{"type": "long", "name": "epoch"}' | tee epoch.avsc rpk registry schema create src-value --schema epoch.avsc -
Deploy the transforms function:
rpk transform build rpk transform deploy --file ts-converter.wasm -i src -o sink --var "TIMESTAMP_TARGET_TYPE=string[%+]"This example accepts the following environment variables:
-
TIMESTAMP_TARGET_TYPE(required): The output conversion time. Must be one of:-
string[<pattern>]: where<pattern>is a valid chrono format string -
unix[<precision>]: where<precision>is eitherseconds,milliseconds,microseconds, ornanoseconds -
date: which truncates to just the date portion of the timestamp -
time: which truncates to just the 24-hour time of the timestamp
-
-
TIMESTAMP_MODE: one ofkey,value, or a fielded version likevalue[my-field] -
TIMESTAMP_STRING_FORMAT: if your input data is a string, provide the chrono format for parsing
-
-
Produce an example epoch in milliseconds using
rpk topic produce:echo "1704310686988" | rpk topic produce src --schema-id topic -
Consume the sink topic with
rpkand using new schema to see the string-based timestamp:rpk topic consume sink --use-schema-registry=value -n 1 -o -1{ "topic": "sink", "value": "\"2024-01-03T19:38:06.988+00:00\"", "timestamp": 1715890281087, "partition": 0, "offset": 0 }
You can also see this in Redpanda Console.
Clean up
To shut down and delete the containers along with all your cluster data:
docker compose down -v