Skip to content

Commit

Permalink
[improve][doc] Add doc on Record as Function output type
Browse files Browse the repository at this point in the history
  • Loading branch information
cbornet authored Aug 23, 2022
1 parent 085a799 commit 11f6912
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion site2/docs/functions-concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ Java, Python, and Go SDKs provide access to a **context object** that can be use
* An interface for storing and retrieving state in [state storage](functions-develop-state.md).
* A function to publish new messages onto arbitrary topics.
* A function to acknowledge the message being processed (if auto-ack is disabled).
* (Java) get Pulsar admin client.
* (Java) A function to get the Pulsar admin client.
* (Java) A function to create a Record to return with default values taken from the Context and the input Record.

:::tip

Expand Down
30 changes: 30 additions & 0 deletions site2/docs/functions-develop-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,36 @@ public class ExclamationFunction implements Function<String, String> {
For more details, see [code example](https://github.com/apache/pulsar/blob/master/pulsar-functions/java-examples/src/main/java/org/apache/pulsar/functions/api/examples/ExclamationFunction.java).
The return type of the function can be wrapped in a `Record` generic which gives you more control over the output messages, such as topics, schemas, properties, and so on.
Use the `Context::newOutputRecordBuilder` method to build this `Record` output.
```java
import java.util.HashMap;
import java.util.Map;
import org.apache.pulsar.functions.api.Context;
import org.apache.pulsar.functions.api.Function;
import org.apache.pulsar.functions.api.Record;
public class RecordFunction implements Function<String, Record<String>> {
@Override
public Record<String> process(String input, Context context) throws Exception {
String output = String.format("%s!", input);
Map<String, String> properties = new HashMap<>(context.getCurrentRecord().getProperties());
context.getCurrentRecord().getTopicName().ifPresent(topic -> properties.put("input_topic", topic));
return context.newOutputRecordBuilder(Schema.STRING)
.value(output)
.properties(properties)
.build();
}
}
```
For more details, see [code example](https://github.com/apache/pulsar/blob/master/pulsar-functions/java-examples/src/main/java/org/apache/pulsar/functions/api/examples/RecordFunction.java).
</TabItem>
<TabItem value="Python">
Expand Down

0 comments on commit 11f6912

Please sign in to comment.