-
Notifications
You must be signed in to change notification settings - Fork 387
Update insert-functions.md to correct onBatch example #7944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thumbsupep
wants to merge
1
commit into
segmentio:develop
Choose a base branch
from
thumbsupep:patch-1
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -267,7 +267,7 @@ To prevent your insert function from processing data, toggle Enable Function off | |
|
||
## Batching the destination insert function | ||
|
||
Batch handlers are an extension of insert functions. When you define an `onBatch` handler alongside the handler functions for single events (for example, `onTrack` or `onIdentity`), you're telling Segment that the insert function can accept and handle batches of events. | ||
Batch handlers are an extension of insert functions. When you define an `onBatch` handler alongside the handler functions for single events (for example, `onTrack` or `onIdentity`), you're telling Segment that the insert function can accept and handle batches of events. | ||
|
||
> info "" | ||
> Batching is available for destination and destination insert functions only. | ||
|
@@ -289,6 +289,8 @@ Segment collects the events over a short period of time and combines them into a | |
|
||
To create a batch handler, define an `onBatch` function within your destination insert function. You can also use the "Default Batch" template found in the Functions editor to get started quickly. | ||
|
||
**However**, Segment's function invoker service relies on positional consistency between the input and output arrays when using the `onBatch` handler. When a function returns a transformed batch, Segment pairs each output event with its corresponding input using array index positions—not event IDs or timestamps. Consumers must preserve the original order in their `onBatch` implementation. | ||
|
||
```js | ||
async function onBatch(events, settings){ | ||
// handle the batch of events | ||
|
@@ -306,6 +308,7 @@ The handler function receives an array of events. The events can be of any suppo | |
{ | ||
"type": "identify", | ||
"userId": "019mr8mf4r", | ||
"messageId": "ajs-next-1757955997356-c029ce21-9034-45a6-ac62-b8b23b655df5", | ||
"traits": { | ||
"email": "[email protected]", | ||
"name": "Jake Peterson", | ||
|
@@ -315,6 +318,7 @@ The handler function receives an array of events. The events can be of any suppo | |
{ | ||
"type": "track", | ||
"userId": "019mr8mf4r", | ||
"messageId": "ajs-next-1757955997356-bde2ad8e-2af9-45f5-837b-993671f6b1b0", | ||
"event": "Song Played", | ||
"properties": { | ||
"name": "Fallin for You", | ||
|
@@ -324,6 +328,7 @@ The handler function receives an array of events. The events can be of any suppo | |
{ | ||
"type": "track", | ||
"userId": "971mj8mk7p", | ||
"messageId": "ajs-next-1757955997356-9e2ba07c-0085-4a5b-8928-e4450a417f74", | ||
"event": "Song Played", | ||
"properties": { | ||
"name": "Get Right", | ||
|
@@ -339,6 +344,9 @@ Segment batches together any event _of any type_ that it sees over a short perio | |
|
||
```js | ||
async function onBatch(events, settings) { | ||
// store original order | ||
const originalOrder = events.map(event => event.messageId); | ||
|
||
// group events by type | ||
const eventsByType = {} | ||
for (const event of events) { | ||
|
@@ -361,7 +369,10 @@ async function onBatch(events, settings) { | |
try { | ||
const results = await Promise.all(promises); | ||
const batchResult = [].concat(...results); // Combine arrays into a single array | ||
return batchResult; | ||
|
||
// restore original order | ||
const resultMap = new Map(batchResult.map(e => [e.messageId, e])); | ||
return originalOrder.map(id => resultMap.get(id)); | ||
} catch (error) { | ||
throw new RetryError(error.message); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.