Error: truncated buffer/TypeError: this.buf.utf8Slice is not a function

See original GitHub issue

When I want to read an event received from Kafka with the following schema, at first I get the Error: truncated buffer exception and when I customize the JSON schema I get TypeError: this.buf.utf8Slice is not a function exception! I think something is wrong with JSON schema. I’ve provided both Avro and JSON schema for better debugging.

My Avro schema when producing event to Kafka:

{
    "name": "ComposedEvent",
    "type": "record",
    "fields": [

        { "name": "SearchResult",
            "type": [
                "null",
                {
                    "name":"SearchResultRec", "type": "record",
                    "fields":[
                        { "name": "query", "type": "string"},
                        { "name": "took", "type": "int" },
                        { "name": "timed_out", "type": "boolean" },
                        { "name": "hits", "type": "int"}
                    ]
                }
            ], "default" : null
        },

        { "name": "detectedDuplicate",       "type": "boolean" },
        { "name": "detectedCorruption",      "type": "boolean" },
        { "name": "firstInSession",          "type": "boolean" },
        { "name": "timestamp",               "type": "long" },
        { "name": "clientTimestamp",         "type": "long" },
        { "name": "remoteHost",              "type": "string" },
        { "name": "referer",                 "type": ["null", "string"], "default": null },
        { "name": "location",                "type": ["null", "string"], "default": null },
        { "name": "viewportPixelWidth",      "type": ["null", "int"],    "default": null },
        { "name": "viewportPixelHeight",     "type": ["null", "int"],    "default": null },
        { "name": "screenPixelWidth",        "type": ["null", "int"],    "default": null },
        { "name": "screenPixelHeight",       "type": ["null", "int"],    "default": null },
        { "name": "partyId",                 "type": ["null", "string"], "default": null },
        { "name": "sessionId",               "type": ["null", "string"], "default": null },
        { "name": "pageViewId",              "type": ["null", "string"], "default": null },
        { "name": "eventType",               "type": "string",           "default": "unknown" },
        { "name": "userAgentString",         "type": ["null", "string"], "default": null },
        { "name": "userAgentName",           "type": ["null", "string"], "default": null },
        { "name": "userAgentFamily",         "type": ["null", "string"], "default": null },
        { "name": "userAgentVendor",         "type": ["null", "string"], "default": null },
        { "name": "userAgentType",           "type": ["null", "string"], "default": null },
        { "name": "userAgentVersion",        "type": ["null", "string"], "default": null },
        { "name": "userAgentDeviceCategory", "type": ["null", "string"], "default": null },
        { "name": "userAgentOsFamily",       "type": ["null", "string"], "default": null },
        { "name": "userAgentOsVersion",      "type": ["null", "string"], "default": null },
        { "name": "userAgentOsVendor",       "type": ["null", "string"], "default": null }
    ]
}

My JSON schema when trying to read produced events:

{
    name: 'ComposedEvent',
    type: 'record',
    fields: [

        { name: 'SearchResult',
            type: [
                'null',
                {
                    name:'SearchResultRec', type: 'record',
                    fields:[
                        { name: 'query', type: 'string'},
                        { name: 'took', type: 'int' },
                        { name: 'timed_out', type: 'boolean' },
                        { name: 'hits', type: 'int'}
                    ]
                }
            ], 'default': null
        },

        { name: 'detectedDuplicate',       type: 'boolean' },
        { name: 'detectedCorruption',      type: 'boolean' },
        { name: 'firstInSession',          type: 'boolean' },
        { name: 'timestamp',               type: 'int' },
        { name: 'clientTimestamp',         type: 'int' },
        { name: 'remoteHost',              type: 'string' },
        { name: 'referer',                 type: ['null', 'string'], 'default': null },
        { name: 'location',                type: ['null', 'string'], 'default': null },
        { name: 'viewportPixelWidth',      type: ['null', 'int'],    'default': null },
        { name: 'viewportPixelHeight',     type: ['null', 'int'],    'default': null },
        { name: 'screenPixelWidth',        type: ['null', 'int'],    'default': null },
        { name: 'screenPixelHeight',       type: ['null', 'int'],    'default': null },
        { name: 'partyId',                 type: ['null', 'string'], 'default': null },
        { name: 'sessionId',               type: ['null', 'string'], 'default': null },
        { name: 'pageViewId',              type: ['null', 'string'], 'default': null },
        { name: 'eventType',               type: 'string',           'default': 'unknown' },
        { name: 'userAgentString',         type: ['null', 'string'], 'default': null },
        { name: 'userAgentName',           type: ['null', 'string'], 'default': null },
        { name: 'userAgentFamily',         type: ['null', 'string'], 'default': null },
        { name: 'userAgentVendor',         type: ['null', 'string'], 'default': null },
        { name: 'userAgentType',           type: ['null', 'string'], 'default': null },
        { name: 'userAgentVersion',        type: ['null', 'string'], 'default': null },
        { name: 'userAgentDeviceCategory', type: ['null', 'string'], 'default': null },
        { name: 'userAgentOsFamily',       type: ['null', 'string'], 'default': null },
        { name: 'userAgentOsVersion',      type: ['null', 'string'], 'default': null },
        { name: 'userAgentOsVendor',       type: ['null', 'string'], 'default': null }
    ]
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
BryceCicadacommented, Oct 13, 2016

I also had this problem with kafka-node. I solved it by setting the encoding on the consumer to ‘buffer’. For example:

let consumer = new kafka.Consumer(client, [], {encoding:'buffer'});

See https://www.npmjs.com/package/kafka-node#consumer

1reaction
mtthcommented, Jul 25, 2016

Thanks, I see. The problem is that kafka-node’s message.value isn’t the right argument for type.fromBuffer. For example it expects a Buffer containing exactly a single encoded value but AFAIK message.value is a string prefixed by a header (see https://github.com/mtth/avsc/issues/13).

The best way to handle this would probably be to implement some kind of schema resolution logic (retrieving the type from the header) but in your case I believe you should be able to get a quick solution working as follows:

// ...
consumer.on('message', function (message) {
  var buf = new Buffer(message.value, 'binary'); // Read string into a buffer.
  var decodedMessage = type.fromBuffer(buf.slice(5)); // Skip prefix.
  console.log(decodedMessage);
});

You might also want to take a look at https://github.com/mtth/avsc/issues/22 for more information on Kafka messages.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: this.buf.utf8Write is not a function - Stack Overflow
Solution: When avsc is used on the client side with webpack or browserify, one has to use require('avsc/etc/browser/avsc').
Read more >
Error while decoding Avro data with NodeJS - MSDN - Microsoft
TypeError: this.buf.utf8Slice is not a function. Here's my code - const avro = require('avsc'); module.exports = function (context, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found