Function Interface
All functions implement a basic interface with aprocess method that handles each incoming message.
Java Functions
Java functions implement theFunction<I, O> interface:
<I, O> define input and output types. Common types include:
Stringbyte[]Integer,Long,Double- Custom POJOs with schema support
Voidfor functions with no output
Python Functions
Python functions extend theFunction class:
Go Functions
Go functions are simple functions that accept a context and input:Using the Context API
TheContext object provides access to function metadata, configuration, logging, and state management.
Accessing Function Metadata
Logging
Use the context logger for structured logging:--log-topic deployment option.
User Configuration
Access custom configuration passed during deployment:State Management
Pulsar Functions provide built-in state storage backed by BookKeeper for fault-tolerant stateful processing.Counter State
Use counters for simple numeric aggregation:Key-Value State
Store arbitrary state using key-value pairs:Async State Operations
For better performance, use async state operations:Publishing Messages
Functions can publish messages to topics beyond the configured output topic.Publishing to Custom Topics
Publishing with Properties
Metrics and Monitoring
Record custom metrics to monitor function behavior:Window Functions
Window functions process batches of messages over time or count-based windows.Implementing Window Functions
Testing Functions Locally
Unit Testing
Test your function logic independently:Local Mode Testing
Run functions locally using the Pulsar Functions localrun mode:Best Practices
Keep Functions Lightweight
Functions should process messages quickly. For heavy computation, consider using Pulsar IO connectors to external systems.
Handle Errors Gracefully
Throw exceptions for transient errors (message will be retried). Return error indicators for permanent failures.
Use Async Operations
Use async state and publish operations for better throughput when processing time is dominated by I/O.
Monitor Function Health
Emit custom metrics for business-specific monitoring and set up alerts based on processing rates.
Next Steps
Deploying Functions
Learn how to deploy functions to production
Runtime Configuration
Configure resources and execution mode