You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by li...@apache.org on 2022/08/23 02:17:45 UTC
[pulsar] branch master updated: [improve][doc] Add doc on Record as Function output type
This is an automated email from the ASF dual-hosted git repository.
liuyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git
The following commit(s) were added to refs/heads/master by this push:
new 11f6912f567 [improve][doc] Add doc on Record as Function output type
11f6912f567 is described below
commit 11f6912f567a2a81860a6ff0b73f3d2c1d5516ce
Author: Christophe Bornet <cb...@hotmail.com>
AuthorDate: Tue Aug 23 04:17:38 2022 +0200
[improve][doc] Add doc on Record as Function output type
---
site2/docs/functions-concepts.md | 3 ++-
site2/docs/functions-develop-api.md | 30 ++++++++++++++++++++++++++++++
2 files changed, 32 insertions(+), 1 deletion(-)
diff --git a/site2/docs/functions-concepts.md b/site2/docs/functions-concepts.md
index 4f40f8ec7be..8b49b62edc8 100644
--- a/site2/docs/functions-concepts.md
+++ b/site2/docs/functions-concepts.md
@@ -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
diff --git a/site2/docs/functions-develop-api.md b/site2/docs/functions-develop-api.md
index baa21f5fbe6..0a1f4a408fb 100644
--- a/site2/docs/functions-develop-api.md
+++ b/site2/docs/functions-develop-api.md
@@ -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">