1
我想反序列化從ZeroMQ收到的谷歌protobuf消息,並試圖轉換爲JSON格式,使用下面的一段代碼。但在最終輸出中,定義爲字節的字段是不可讀的。如何反序列化從Google protobuf以Java接收的字節?
(例如,
"source_id": "\u0000PV\uff98t\uff9e"
)。
由於它是機器生成的數據,我們沒有從源發送的實際值。
InputStream is = new ByteArrayInputStream(message.getBytes());
Schema.nb_event data = Schema.nb_event.parseFrom(is);
String jsonFormat = JsonFormat.printToString(data);
輸出
{ "seq": 6479250, "timestamp": 1488461706,"op": "OP_UPDATE","topic_seq": 595736,"source_id": "\u0000PV\uff98t\uff9e","location": {"sta_eth_mac": {"addr": "xxxxxxx"},"sta_location_x": 879.11456,"sta_location_y": 945.0676,"error_level": 1220,"associated": true,"campus_id": "\uff9f\uff94\uffc7\uffa3\uffa2\b6\uffe3\uff92U\uff9f\uffdcN\'MT","building_id": "\uffee\u0016??X}5\u001a\uffaa\uffc4^\uffa0n\uffa4\ufffb\'","floor_id": "\uffd9/\"uF\uffdd3\uffdd\uff96\u0015\uff83~\u0005\uff8a(\uffd0","hashed_sta_eth_mac": "\u0013h\u0017\uffd0\uffef\uffc8\u001f\u0005V\u0010w?xxxxxx","loc_algorithm": "ALGORITHM_LOW_DENSITY","unit": "FEET"}}
==
{ "seq": 6479250,
"timestamp": 1488461706,
"op": "OP_UPDATE",
"topic_seq": 595736,
"source_id": "\u0000PV\uff98t\uff9e",
"location": { "sta_eth_mac": { "addr": "\uffc0\uffcc\ufff8P\uffee." },
"sta_location_x": 879.11456,
"sta_location_y": 945.0676,
"error_level": 1220,
"associated": true,
"campus_id": "\uff9f\uff94\uffc7\uffa3\uffa2\b6\uffe3\uff92U\uff9f\uffdcN\'MT",
"building_id": "\uffee\u0016??X}5\u001a\uffaa\uffc4^\uffa0n\uffa4\ufffb\'",
"floor_id": "\uffd9/\"uF\uffdd3\uffdd\uff96\u0015\uff83~\u0005\uff8a(\uffd0",
"hashed_sta_eth_mac": "\u0013h\u0017\uffd0\uffef\uffc8\u001f\u0005V\u0010w?\uff88\uffa8\uffee\u000fm.\u0015\uffe9",
"loc_algorithm": "ALGORITHM_LOW_DENSITY",
"unit": "FEET"
}
}
所有不可讀字段定義在.proto
文件字節。
獲取這些值是否需要額外的步驟?
optional bytes building_id = 10;
optional bytes floor_id = 11;
optional bytes hashed_sta_eth_mac = 12;
嗯,是不透明的二進制數據。你爲什麼期望能夠將它看作文本?基本上在Java中,他們將是一個'byte []'。無可否認,我希望JSON格式化程序可以將它們轉換爲base64格式,而不是你所顯示的格式,但它仍然不會是可讀的文本。 –
事實上,正常的'JsonFormat'確實應該將這些字節序列化爲Base64:https://github.com/google/protobuf/blob/34a1b6e6b8c0d477504d09df4df4b86770e47872/java/util/src/main/java/com/google/protobuf/util /JsonFormat.java#L992是你正在使用的'JsonFormat'類,還是這是別的? (另一方面,'mesage'是一個'String'還是其他的東西?如果是這樣,調用'getBytes()'就是個壞主意)。基本上,[mcve]會讓你更容易幫助你。 –
需求是反序列化數據並在我們的數據湖中以JSON格式寫成純文本格式。是否有工作來獲得實際價值? –