-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer.js
37 lines (31 loc) · 1 KB
/
producer.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
import pkg from 'kafkajs'
import _ from 'lodash'
import fs from 'fs'
import util from 'util'
import uniqueNamesGeneratorPkg from 'unique-names-generator'
const { Kafka } = pkg;
const { uniqueNamesGenerator, adjectives, colors, animals } = uniqueNamesGeneratorPkg
const writeFile = util.promisify(fs.writeFile)
const chatIds = ['chat1', 'chat2', 'chat3', 'chat4', 'chat5', 'chat6', 'chat7', 'chat8']
const accountIds = Array.from({ length: 50 }, () => uniqueNamesGenerator({
dictionaries: [adjectives, animals, colors],
length: 2
}));
(async function () {
const kafka = new Kafka({
clientId: 'my-app',
brokers: ['localhost:9092']
})
const producer = kafka.producer()
await producer.connect()
const messages = Array.from({ length: 10000 }, (__, i) => ({
key: _.sample(chatIds),
value: `${i}__${_.sample(accountIds)}`
}))
await producer.send({
topic: 'chat',
messages
})
await writeFile('messages.json', JSON.stringify(messages, null, 2))
await producer.disconnect()
})()