Make Uppercase (map)
Create a SmartModule that converts records to uppercase.
Prerequisitesโ
- SDMK CLI is part of the Fluvio installation. Install Fluvio and start a local a cluster to build and test the SmartModule.
Generate a Smartmodule Projectโ
Use SMDK generator to create a new SmartModule project of type map
.
$ smdk generate uppercase
๐คท Please set a group name: acme
โ ๐คท Which type of SmartModule would you like? ยท map
โ ๐คท Will your SmartModule use init parameters? ยท false
โ ๐คท Will your SmartModule be public? ยท false
[1/7] Done: .gitignore
[2/7] Done: Cargo.toml
[3/7] Done: README.md
[4/7] Done: SmartModule.toml
[5/7] Done: rust-toolchain.toml
[6/7] Done: src/lib.rss
[7/7] Done: src
Add Custom logicโ
Let's checkout the project tree:
$ cd uppercase; tree
.
โโโ Cargo.toml
โโโ README.md
โโโ SmartModule.toml
โโโ rust-toolchain.toml
โโโ src
โโโ lib.rs
Replace the existing code in src/lib.rs
with the following:
use fluvio_smartmodule::{smartmodule, Result, SmartModuleRecord, RecordData};
#[smartmodule(map)]
pub fn map(record: &SmartModuleRecord) -> Result<(Option<RecordData>, RecordData)> {
let key = record.key.clone();
let string = std::str::from_utf8(record.value.as_ref())?;
let upper = string.to_string().to_uppercase();
Ok((key, upper.into()))
}
Build and Testโ
It's time to build the SmartModule:
$ smdk build
Test the SmartModule:
$ smdk test --text "Hello in uppercase"
HELLO IN UPPERCASE
Load Smartmodule to your Clusterโ
You must upload the SmartModule to your cluster to use it.
$ smdk load
Creating SmartModule: uppercase
Ensure it has been uploaded by running the following command:
$ fluvio sm list
SMARTMODULE SIZE
acme/uppercase@0.1.0 40.9 KB
๐ Congratulations! Your smartmodule is ready to use.