3

I am using http://kafka.js.org/ to produce messages to Kafka, one of the schemas I am working on is a decimal byte type:

{"type": "bytes", "logicalType": "decimal", "precision": 11, "scale": 2}

I am looking for some guidance on how I can convert a decimal in Node to a byte value to match the above schema.

Thanks in advance.

danik
  • 191
  • 3
  • 13

2 Answers2

1

You can try https://www.npmjs.com/package/@ovotech/avro-decimal

This package helped us resolve the same problem

razon
  • 3,882
  • 2
  • 33
  • 46
1

We also solved the problem using https://www.npmjs.com/package/@ovotech/avro-decimal

Here are the relevant parts of the code:

const {AvroDecimal} = require('@ovotech/avro-decimal');
const {Decimal} = require('decimal.js');

const registry = new SchemaRegistry({host: 'http://localhost:8201'}, {forSchemaOptions: {logicalTypes: {decimal: AvroDecimal}}})

I asume you use @kafkajs/confluent-schema-registry. As you can see the SchemaRegistry constructor has an optional parameter which lets you add custom serializers and deserializers (AvroDecimal will be our custom serializer/deserializer for decimals).

Then when you build your message, using the Decimal constructor as follow

const myObject = {
  stringField: 'whatever String' 
  decimalField: new Decimal('1.0'),
}

Then just send your message as usual

const id = await registry.getLatestSchemaId(REGISTRY_SUBJECT);
const record = await registry.encode(id, myObject);
await producer.connect();
await producer.send({
  topic: MY_TOPIC
  messages: [{
    value: record
  }]
}
);
Heyjojo
  • 465
  • 1
  • 4
  • 11