Decode Avro decimal-as-bytearray string





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm using Kafka with Avro messages. One of my fields is defined like this:



{ 
"name": "a_number",
"type": "bytes",
"logicalType": "decimal",
"precision": 4,
"scale": 4
}


Using the Avro console consumer, I see a message like this:



{"a_number": "tu0000°"}


Which I expect to equal 59.



Supposedly, the bytearray should be the twos-compliment of the number. I've tried using Python's struct module to decode it, but the values I get don't make any sense:



bs = 'tu0000°'.encode('utf8')    # b'tx00xc2xb0'
struct.unpack('>l', bs)[0] / 1e4 # 15104.4784


How can I validate the message? Can I decode the string somehow, or has the Avro console consumer corrupted it?










share|improve this question

























  • For comparison: struct.pack('>l', int(59 * 1e4)) == b'x00tx00xb0'

    – z0r
    Nov 21 '18 at 22:16











  • Are you sure that you want to encode high ASCII values as UTF-8? They will gain at least one additional byte, then, which will influence the total value. That said, at least you will get 4 bytes. Your sample string defines only 3.

    – usr2564301
    Nov 21 '18 at 22:38











  • @usr2564301 Yeah, I'm not sure - it does seem weird. The reason I chose UTF-8 is that that's what JSON uses to encode strings, and the output of the Avro console consumer is (apparently) JSON. I am a bit suss on that string; I would have expected it to write something in Base64 or so.

    – z0r
    Nov 21 '18 at 23:01


















0















I'm using Kafka with Avro messages. One of my fields is defined like this:



{ 
"name": "a_number",
"type": "bytes",
"logicalType": "decimal",
"precision": 4,
"scale": 4
}


Using the Avro console consumer, I see a message like this:



{"a_number": "tu0000°"}


Which I expect to equal 59.



Supposedly, the bytearray should be the twos-compliment of the number. I've tried using Python's struct module to decode it, but the values I get don't make any sense:



bs = 'tu0000°'.encode('utf8')    # b'tx00xc2xb0'
struct.unpack('>l', bs)[0] / 1e4 # 15104.4784


How can I validate the message? Can I decode the string somehow, or has the Avro console consumer corrupted it?










share|improve this question

























  • For comparison: struct.pack('>l', int(59 * 1e4)) == b'x00tx00xb0'

    – z0r
    Nov 21 '18 at 22:16











  • Are you sure that you want to encode high ASCII values as UTF-8? They will gain at least one additional byte, then, which will influence the total value. That said, at least you will get 4 bytes. Your sample string defines only 3.

    – usr2564301
    Nov 21 '18 at 22:38











  • @usr2564301 Yeah, I'm not sure - it does seem weird. The reason I chose UTF-8 is that that's what JSON uses to encode strings, and the output of the Avro console consumer is (apparently) JSON. I am a bit suss on that string; I would have expected it to write something in Base64 or so.

    – z0r
    Nov 21 '18 at 23:01














0












0








0








I'm using Kafka with Avro messages. One of my fields is defined like this:



{ 
"name": "a_number",
"type": "bytes",
"logicalType": "decimal",
"precision": 4,
"scale": 4
}


Using the Avro console consumer, I see a message like this:



{"a_number": "tu0000°"}


Which I expect to equal 59.



Supposedly, the bytearray should be the twos-compliment of the number. I've tried using Python's struct module to decode it, but the values I get don't make any sense:



bs = 'tu0000°'.encode('utf8')    # b'tx00xc2xb0'
struct.unpack('>l', bs)[0] / 1e4 # 15104.4784


How can I validate the message? Can I decode the string somehow, or has the Avro console consumer corrupted it?










share|improve this question
















I'm using Kafka with Avro messages. One of my fields is defined like this:



{ 
"name": "a_number",
"type": "bytes",
"logicalType": "decimal",
"precision": 4,
"scale": 4
}


Using the Avro console consumer, I see a message like this:



{"a_number": "tu0000°"}


Which I expect to equal 59.



Supposedly, the bytearray should be the twos-compliment of the number. I've tried using Python's struct module to decode it, but the values I get don't make any sense:



bs = 'tu0000°'.encode('utf8')    # b'tx00xc2xb0'
struct.unpack('>l', bs)[0] / 1e4 # 15104.4784


How can I validate the message? Can I decode the string somehow, or has the Avro console consumer corrupted it?







python python-3.x apache-kafka avro






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 22:46









cricket_007

84.2k1147119




84.2k1147119










asked Nov 21 '18 at 22:14









z0rz0r

4,91713655




4,91713655













  • For comparison: struct.pack('>l', int(59 * 1e4)) == b'x00tx00xb0'

    – z0r
    Nov 21 '18 at 22:16











  • Are you sure that you want to encode high ASCII values as UTF-8? They will gain at least one additional byte, then, which will influence the total value. That said, at least you will get 4 bytes. Your sample string defines only 3.

    – usr2564301
    Nov 21 '18 at 22:38











  • @usr2564301 Yeah, I'm not sure - it does seem weird. The reason I chose UTF-8 is that that's what JSON uses to encode strings, and the output of the Avro console consumer is (apparently) JSON. I am a bit suss on that string; I would have expected it to write something in Base64 or so.

    – z0r
    Nov 21 '18 at 23:01



















  • For comparison: struct.pack('>l', int(59 * 1e4)) == b'x00tx00xb0'

    – z0r
    Nov 21 '18 at 22:16











  • Are you sure that you want to encode high ASCII values as UTF-8? They will gain at least one additional byte, then, which will influence the total value. That said, at least you will get 4 bytes. Your sample string defines only 3.

    – usr2564301
    Nov 21 '18 at 22:38











  • @usr2564301 Yeah, I'm not sure - it does seem weird. The reason I chose UTF-8 is that that's what JSON uses to encode strings, and the output of the Avro console consumer is (apparently) JSON. I am a bit suss on that string; I would have expected it to write something in Base64 or so.

    – z0r
    Nov 21 '18 at 23:01

















For comparison: struct.pack('>l', int(59 * 1e4)) == b'x00tx00xb0'

– z0r
Nov 21 '18 at 22:16





For comparison: struct.pack('>l', int(59 * 1e4)) == b'x00tx00xb0'

– z0r
Nov 21 '18 at 22:16













Are you sure that you want to encode high ASCII values as UTF-8? They will gain at least one additional byte, then, which will influence the total value. That said, at least you will get 4 bytes. Your sample string defines only 3.

– usr2564301
Nov 21 '18 at 22:38





Are you sure that you want to encode high ASCII values as UTF-8? They will gain at least one additional byte, then, which will influence the total value. That said, at least you will get 4 bytes. Your sample string defines only 3.

– usr2564301
Nov 21 '18 at 22:38













@usr2564301 Yeah, I'm not sure - it does seem weird. The reason I chose UTF-8 is that that's what JSON uses to encode strings, and the output of the Avro console consumer is (apparently) JSON. I am a bit suss on that string; I would have expected it to write something in Base64 or so.

– z0r
Nov 21 '18 at 23:01





@usr2564301 Yeah, I'm not sure - it does seem weird. The reason I chose UTF-8 is that that's what JSON uses to encode strings, and the output of the Avro console consumer is (apparently) JSON. I am a bit suss on that string; I would have expected it to write something in Base64 or so.

– z0r
Nov 21 '18 at 23:01












1 Answer
1






active

oldest

votes


















1














You seem to be going about this the Hard Way. The approach suggested by How to extract schema for avro file in python is to use:



reader = avro.datafile.DataFileReader(open('filename.avro',"rb"),avro.io.DatumReader())
schema = reader.meta


Single stepping in a debugger to see how the reader decodes your messages should get you closer to assembling a "raw" hand engineered decode.






share|improve this answer
























  • Yep fair point. I don't have a .avro file to read, but maybe I should just write a little Python script using an AvroConsumer instead of the (presumably) Java-based console consumer to test it.

    – z0r
    Nov 22 '18 at 2:43














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421197%2fdecode-avro-decimal-as-bytearray-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You seem to be going about this the Hard Way. The approach suggested by How to extract schema for avro file in python is to use:



reader = avro.datafile.DataFileReader(open('filename.avro',"rb"),avro.io.DatumReader())
schema = reader.meta


Single stepping in a debugger to see how the reader decodes your messages should get you closer to assembling a "raw" hand engineered decode.






share|improve this answer
























  • Yep fair point. I don't have a .avro file to read, but maybe I should just write a little Python script using an AvroConsumer instead of the (presumably) Java-based console consumer to test it.

    – z0r
    Nov 22 '18 at 2:43


















1














You seem to be going about this the Hard Way. The approach suggested by How to extract schema for avro file in python is to use:



reader = avro.datafile.DataFileReader(open('filename.avro',"rb"),avro.io.DatumReader())
schema = reader.meta


Single stepping in a debugger to see how the reader decodes your messages should get you closer to assembling a "raw" hand engineered decode.






share|improve this answer
























  • Yep fair point. I don't have a .avro file to read, but maybe I should just write a little Python script using an AvroConsumer instead of the (presumably) Java-based console consumer to test it.

    – z0r
    Nov 22 '18 at 2:43
















1












1








1







You seem to be going about this the Hard Way. The approach suggested by How to extract schema for avro file in python is to use:



reader = avro.datafile.DataFileReader(open('filename.avro',"rb"),avro.io.DatumReader())
schema = reader.meta


Single stepping in a debugger to see how the reader decodes your messages should get you closer to assembling a "raw" hand engineered decode.






share|improve this answer













You seem to be going about this the Hard Way. The approach suggested by How to extract schema for avro file in python is to use:



reader = avro.datafile.DataFileReader(open('filename.avro',"rb"),avro.io.DatumReader())
schema = reader.meta


Single stepping in a debugger to see how the reader decodes your messages should get you closer to assembling a "raw" hand engineered decode.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 2:07









J_HJ_H

4,4441922




4,4441922













  • Yep fair point. I don't have a .avro file to read, but maybe I should just write a little Python script using an AvroConsumer instead of the (presumably) Java-based console consumer to test it.

    – z0r
    Nov 22 '18 at 2:43





















  • Yep fair point. I don't have a .avro file to read, but maybe I should just write a little Python script using an AvroConsumer instead of the (presumably) Java-based console consumer to test it.

    – z0r
    Nov 22 '18 at 2:43



















Yep fair point. I don't have a .avro file to read, but maybe I should just write a little Python script using an AvroConsumer instead of the (presumably) Java-based console consumer to test it.

– z0r
Nov 22 '18 at 2:43







Yep fair point. I don't have a .avro file to read, but maybe I should just write a little Python script using an AvroConsumer instead of the (presumably) Java-based console consumer to test it.

– z0r
Nov 22 '18 at 2:43






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421197%2fdecode-avro-decimal-as-bytearray-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)