-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer.js
85 lines (60 loc) · 1.58 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const path = require('path')
const { Kafka,logLevel } = require('kafkajs')
const { SchemaRegistry, SchemaType, avdlToAVSCAsync } = require('@kafkajs/confluent-schema-registry')
const registry = new SchemaRegistry({
host: 'https://xxx.eu-west-2.aws.confluent.cloud',
auth: {
username: 'xxx',
password: 'xxx',
},
})
const kafka = new Kafka({
clientId: 'example-consumer',
brokers: ['xxx.eu-west-2.aws.confluent.cloud:9092'],
ssl: true,
sasl: {
mechanism: 'plain',
username: 'xxx',
password: 'xxx'
},
//logLevel: logLevel.DEBUG
})
const producer = kafka.producer()
const incomingTopic = 'jskafkatopic'
const run = async () => {
const schema = {
type: 'record',
namespace: 'test',
name: 'B',
fields: [{ name: 'fullName', type: 'string' }],
}
await registry.register(
{ type: SchemaType.AVRO,
schema: JSON.stringify(schema)
},
{ subject: 'jskafkatopic-value' },
)
const subject = incomingTopic + '-value'
const version = 1
const id3 = await registry.getRegistryId(subject, version)
console.log(id3)
await producer.connect()
for (var i=1;i<=10; i++) {
const payload = { "fullName": 'John Doe' + i }
console.log(payload)
const outgoingMessage = {
value: await registry.encode(id3, payload)
}
await producer.send({
topic: incomingTopic,
messages: [ outgoingMessage ]
})
console.log(i)
}
producer.disconnect()
}
run().catch(async e => {
console.error(e)
producer && await producer.disconnect()
process.exit(1)
})