-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsomething.js
39 lines (32 loc) · 1.27 KB
/
something.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const produceOneByOne = async () => {
await producer.connect();
try {
const data = fs.readFileSync(process.env.INPUT_FILE, 'utf8');
const lines = data.split('\n');
let messagesSent = 0;
for (const line of lines) {
try {
const jsonContent = JSON.parse(line.trim()); // Parse existing JSON string
const message = {
key: `${jsonContent.payload.sagas[0].do.pathParams.productId}:${jsonContent.payload.sagas[0].do.pathParams.sales_channel_id}`,
value: line.trim()
};
// Send each message separately
await producer.send({
topic: process.env.KAFKA_TOPIC,
messages: [message]
});
messagesSent++;
console.log(`Message ${messagesSent} sent`);
} catch (error) {
console.error('Error parsing JSON:', error);
// Handle the error (e.g., log it, skip the line, etc.)
}
}
console.log(`All ${messagesSent} messages sent`);
} catch (error) {
console.error('Error reading file:', error);
}
await producer.disconnect();
console.log('Producer disconnected');
};