|
| 1 | +// require('kafkajs') is replaced with require('confluent-kafka-js').KafkaJS. |
| 2 | +// Since this example is within the package itself, we use '../..', but code |
| 3 | +// will typically use 'confluent-kafka-js'. |
| 4 | +const { Kafka } = require('../..').KafkaJS; |
| 5 | + |
| 6 | +// Note: The kafkajs/confluent-schema-registry will need to be installed separately to run this example, |
| 7 | +// as it isn't a dependency of confluent-kafka-js. |
| 8 | +const { SchemaRegistry, SchemaType } = require('@kafkajs/confluent-schema-registry'); |
| 9 | + |
| 10 | +const registry = new SchemaRegistry({ host: '<fill>' }) |
| 11 | +const kafka = new Kafka({ |
| 12 | + brokers: ['<fill>'], |
| 13 | + clientId: 'example-consumer', |
| 14 | +}) |
| 15 | +let consumer = kafka.consumer({ groupId: 'test-group' , fromBeginning: true, } ); |
| 16 | +let producer = kafka.producer(); |
| 17 | + |
| 18 | +const schemaA = { |
| 19 | + type: 'record', |
| 20 | + namespace: 'test', |
| 21 | + name: 'A', |
| 22 | + fields: [ |
| 23 | + { name: 'id', type: 'int' }, |
| 24 | + { name: 'b', type: 'test.B' }, |
| 25 | + ], |
| 26 | +}; |
| 27 | + |
| 28 | +const schemaB = { |
| 29 | + type: 'record', |
| 30 | + namespace: 'test', |
| 31 | + name: 'B', |
| 32 | + fields: [{ name: 'id', type: 'int' }], |
| 33 | +}; |
| 34 | + |
| 35 | +const topicName = 'test-topic'; |
| 36 | + |
| 37 | +const run = async () => { |
| 38 | + // Register schemaB. |
| 39 | + await registry.register( |
| 40 | + { |
| 41 | + type: SchemaType.AVRO, |
| 42 | + schema: JSON.stringify(schemaB), |
| 43 | + }, |
| 44 | + { subject: 'Avro:B' }, |
| 45 | + ); |
| 46 | + const response = await registry.api.Subject.latestVersion({ subject: 'Avro:B' }); |
| 47 | + const { version } = JSON.parse(response.responseData); |
| 48 | + |
| 49 | + // Register schemaA, which references schemaB. |
| 50 | + const { id } = await registry.register( |
| 51 | + { |
| 52 | + type: SchemaType.AVRO, |
| 53 | + schema: JSON.stringify(schemaA), |
| 54 | + references: [ |
| 55 | + { |
| 56 | + name: 'test.B', |
| 57 | + subject: 'Avro:B', |
| 58 | + version, |
| 59 | + }, |
| 60 | + ], |
| 61 | + }, |
| 62 | + { subject: 'Avro:A' }, |
| 63 | + ) |
| 64 | + |
| 65 | + // Produce a message with schemaA. |
| 66 | + await producer.connect() |
| 67 | + const outgoingMessage = { |
| 68 | + key: 'key', |
| 69 | + value: await registry.encode(id, { id: 1, b: { id: 2 } }) |
| 70 | + } |
| 71 | + await producer.send({ |
| 72 | + topic: topicName, |
| 73 | + messages: [outgoingMessage] |
| 74 | + }); |
| 75 | + console.log("Producer sent its message.") |
| 76 | + await producer.disconnect(); |
| 77 | + producer = null; |
| 78 | + |
| 79 | + await consumer.connect() |
| 80 | + await consumer.subscribe({ topic: topicName }) |
| 81 | + |
| 82 | + let messageRcvd = false; |
| 83 | + await consumer.run({ |
| 84 | + eachMessage: async ({ message }) => { |
| 85 | + const decodedMessage = { |
| 86 | + ...message, |
| 87 | + value: await registry.decode(message.value) |
| 88 | + }; |
| 89 | + console.log("Consumer recieved message.\nBefore decoding: " + JSON.stringify(message) + "\nAfter decoding: " + JSON.stringify(decodedMessage)); |
| 90 | + messageRcvd = true; |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + // Wait around until we get a message, and then disconnect. |
| 95 | + while (!messageRcvd) { |
| 96 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 97 | + } |
| 98 | + |
| 99 | + await consumer.disconnect(); |
| 100 | + consumer = null; |
| 101 | +} |
| 102 | + |
| 103 | +run().catch (async e => { |
| 104 | + console.error(e); |
| 105 | + consumer && await consumer.disconnect(); |
| 106 | + producer && await producer.disconnect(); |
| 107 | + process.exit(1); |
| 108 | +}) |
0 commit comments