The WASM runtime is using WasmEdge, theoretically, you can use any languages which
can be compiled to a WASM module to write your functions, below is an example using Rust:
cargo.toml
Copy
Ask AI
[package]name = "excla"version = "0.1.0"edition = "2021"[lib]crate-type = ["cdylib"]# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html[dependencies]apache-avro = { version = "0.15.0", features = ["bzip", "xz", "snappy", "zstandard"] }serde = "^1.0"serde_json = "^1.0"wasmedge-bindgen = "0.4.1"wasmedge-bindgen-macro = "0.4.1"
src/lib.rs
Copy
Ask AI
#[allow(unused_imports)]use wasmedge_bindgen::*;use wasmedge_bindgen_macro::*;use serde::{Deserialize, Serialize};#[derive(Debug, Deserialize, Serialize)]pub struct Student { pub name: Option<String>, pub age: Option<i32>, pub grade: Option<i32>}// use `process_json` as the class name when creating functions#[wasmedge_bindgen]pub fn process_json(s: Vec<u8>) -> Vec<u8> { let stu = serde_json::from_slice::<Student>(&s[..]).unwrap(); let stu = Student { grade: stu.grade.map(|grade| grade + 1), ..stu }; let res = serde_json::to_vec(&stu).unwrap(); return res}
After creating a cluster, set up your environment and develop&package your function, you can use the snctl, pulsarctl, pulsar-admin command, the REST API, or terraform to deploy a Pulsar function to your cluster.
You can create a WASM Pulsar function by using a local .wasm file or an uploaded Pulsar functions package(recommend).
Since Pulsar doesn’t support WASM runtime, we need to use --py to specify the function file and specify the --custom-runtime-options '{"genericKind": "wasm"}' to make it work.
Since Pulsar doesn’t support WASM runtime, we need to use --py to specify the function file and specify the --custom-runtime-options '{"genericKind": "wasm"}' to make it work.
Since Pulsar doesn’t support WASM runtime, we need to use --py to specify the function file and specify the --custom-runtime-options '{"genericKind": "wasm"}' to make it work.
Since Pulsar doesn’t support WASM runtime, we need to use --py to specify the function file and specify the --custom-runtime-options '{"genericKind": "wasm"}' to make it work.
Since Pulsar doesn’t support WASM runtime, we need to use --py to specify the function file and specify the --custom-runtime-options '{"genericKind": "wasm"}' to make it work.
Init the terraform provider in the same dir of your .tf file if you haven’t done it:
Copy
Ask AI
terraform init
You should see something like this:
Copy
Ask AI
Initializing the backend...Initializing provider plugins...- Finding streamnative/pulsar versions matching "0.2.0"...- Installing streamnative/pulsar v0.2.0...- Installed streamnative/pulsar v0.2.0 (self-signed, key ID 3105E1011F3C3671)Partner and community providers are signed by their developers.If you'd like to know more about provider signing, you can read about it here:https://www.terraform.io/docs/cli/plugins/signing.htmlTerraform has created a lock file .terraform.lock.hcl to record the providerselections it made above. Include this file in your version control repositoryso that Terraform can guarantee to make the same selections by default whenyou run "terraform init" in the future.Terraform has been successfully initialized!You may now begin working with Terraform. Try running "terraform plan" to seeany changes that are required for your infrastructure. All Terraform commandsshould now work.If you ever set or change modules or backend configuration for Terraform,rerun this command to reinitialize your working directory. If you forget, othercommands will detect it and remind you to do so if necessary.
Create the function:
Copy
Ask AI
terraform apply
You should see something like:
Copy
Ask AI
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + createTerraform will perform the following actions: # pulsar_function.function-1 will be created + resource "pulsar_function" "function-1" { + auto_ack = true + classname = "exclamation.ExclamationFunction" + cleanup_subscription = true + cpu = 0.5 + custom_runtime_options = jsonencode( { + genericKind = "wasm", + env = { + HELLO = "WORLD" } } ) + dead_letter_topic = "public/default/dlt" + disk_mb = 128 + forward_source_message_property = true + id = (known after apply) + inputs = [ + "persistent://public/default/test-wasm-input", ] + py = "function://public/default/wasm-exclamation@v0.1" + log_topic = "public/default/lt" + max_message_retries = 100 + name = "function1" + namespace = "default" + output = "persistent://public/default/test-wasm-output" + parallelism = 1 + processing_guarantees = "ATLEAST_ONCE" + ram_mb = 128 + retain_key_ordering = true + secrets = jsonencode( { + SECRET1 = { + key = "hello" + path = "sectest" } } ) + skip_to_latest = true + subscription_name = "test-sub" + subscription_position = "Latest" + tenant = "public" + timeout_ms = 6666 }Plan: 1 to add, 0 to change, 0 to destroy.Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value:
Since Pulsar doesn’t support WASM runtime, we need to use --py to specify the function file and specify the --custom-runtime-options '{"genericKind": "wasm"}' to make it work.
The function is assumed to be already uploaded at this point. If you have not
uploaded the function, change the url parameter to be your local filepath.
This will look something like the following.
Copy
Ask AI
-F 'url=file://$YOUR_LCOAL_FUNCTION_FILE'
You should see something like this:
Copy
Ask AI
Created successfully
Replace the placeholder variables with the actual values that you can get when setting up client tools.
WEB_SERVICE_URL: the HTTPS service URL of your Pulsar cluster.
TOKEN: a valid token to interact with your Pulsar cluster.
FUNCTION_NAME: the name of your function.
FUNCTION_VERSION: the version of your function you want to deploy e.g. 1.12.