Speeder
Speeder

Reputation: 140

How to deserialize avro file in react

Can anyone help me to deserialize the avro file in react? I tried with avsc npm package but I am now stuck on error.

const avro = require('avsc')
......
avro.createFileDecoder('./abc.avro').on('data', (record) => { console.log(record) })
......

but it shows an error.

TypeError: avro.createFileDecoder is not a function

How to solve this???

Upvotes: 1

Views: 951

Answers (1)

Speeder
Speeder

Reputation: 140

That error was because the createFileDecoder function requires another parameter { codecs } Anyway, I read the avro file with createBlobDecoder. This is what I did.

let metadata = null;
let rows = [];
avro
  .createBlobDecoder(file)
      .on("metadata", type => {
          metadata = type;
      })
      .on("data", val => {            
          rows.push(val);
      })
      .on("end", () => {
          console.log(metadata);
          console.log(rows);       
      });

Here, file is the default return value of an input tag in HTML.

Upvotes: 0

Related Questions