Skip to content

Commit

Permalink
Clarify
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa committed Dec 18, 2024
1 parent daa0e3c commit d3ff0bb
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,14 +462,16 @@ The example scripts are available as `test_<format/feature>.js` with more code a
Read [this comment](https://github.com/mostafa/xk6-kafka/issues/298#issuecomment-2165246467).
14. I want to specify the offset of a message when consuming from a topic. How can I do that?
To specify the offset of a message while consuming from a topic, use the following options based on your consumption setup:
To specify the offset of a message while consuming from a topic, use the `startOffset` option in the Reader object. This option allows you to define the starting point for message consumption. Here are the values you can use for `startOffset`:
1. **When consuming from a group:**
Use the `startOffset` option in the `Reader` object. This option allows you to define the starting point for message consumption. Here are the values you can use for `startOffset`:
- `-1`: Consume from the most recent message. This is equivalent to START_OFFSETS_LAST_OFFSET.
- `-2`: Consume from the oldest message. This is equivalent to START_OFFSETS_FIRST_OFFSET.
- Any positive number: Consume from the specific offset number provided.
- `-1`: Consume from the most recent message. This is equivalent to `START_OFFSETS_LAST_OFFSET`.
- `-2`: Consume from the oldest message. This is equivalent to `START_OFFSETS_FIRST_OFFSET`.
- Any positive number: Consume from the specific offset number provided.
The constants `START_OFFSETS_LAST_OFFSET` and `START_OFFSETS_FIRST_OFFSET` are part of the xk6-kafka module. You can import and use them in your script for better readability and maintainability. Note that the `startOffset` option is only applicable when consuming from a group.
The constants `START_OFFSETS_LAST_OFFSET` and `START_OFFSETS_FIRST_OFFSET` are part of the xk6-kafka module. You can import and use them in your script. The `startOffset` option is a string.
```javascript
import {
Expand All @@ -478,10 +480,23 @@ The example scripts are available as `test_<format/feature>.js` with more code a
} from "k6/x/kafka";
const reader = new Reader({
brokers: brokers,
groupID: groupID,
groupTopics: [topic],
startOffset: START_OFFSETS_LAST_OFFSET,
brokers: ["localhost:9092"], // Replace with your broker(s)
groupID: "example-group", // Specify your consumer group ID
groupTopics: ["example-topic"], // List of topics for the group
startOffset: START_OFFSETS_LAST_OFFSET, // Use the most recent offset
});
```
2. **When consuming from a topic:**
Use the `offset` option instead of `startOffset`. The `offset` option is a number that directly specifies the offset of the message you want to consume, unlike `startOffset`, which is a string.
```javascript
import { Reader } from "k6/x/kafka";
const reader = new Reader({
brokers: ["localhost:9092"], // Replace with your broker(s)
topic: "example-topic", // Specify the topic
offset: 10, // Consume from offset 10
});
```
Expand Down

0 comments on commit d3ff0bb

Please sign in to comment.