Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;Hazelcast with Protobuf序列化:字符串不是UTF-8格式的_C++_Protocol Buffers_Hazelcast - Fatal编程技术网

C++ C++;Hazelcast with Protobuf序列化:字符串不是UTF-8格式的

C++ C++;Hazelcast with Protobuf序列化:字符串不是UTF-8格式的,c++,protocol-buffers,hazelcast,C++,Protocol Buffers,Hazelcast,我希望能够使用HazelCast发送序列化的Protobuf数据。我理解为什么它会给我错误,但有办法解决吗 以下是我正在使用的示例代码: main.cpp #include <iostream> #include <vector> #include <hazelcast/client/HazelcastClient.h> #include "testProto.pb.h" using namespace std; int main(){

我希望能够使用HazelCast发送序列化的Protobuf数据。我理解为什么它会给我错误,但有办法解决吗

以下是我正在使用的示例代码:

main.cpp

#include <iostream>
#include <vector>
#include <hazelcast/client/HazelcastClient.h>
#include "testProto.pb.h"

using namespace std;

int main(){

    // create object
    tutorial::Input protoInput;
    protoInput.set_innum(500);

    // buffer to store serialized string
    string stringBuffer;
    protoInputer.SerializeToString(&stringBuffer);
    
 
    // set up hazelcast client
    hazelcast::client::ClientConfig(config);
    hazelcast::client::HazelcastClient hz(config);
    hazelcast::client::IMap<string,string> map = hz.getMap<string,string>("myMap");
    
    //error is from trying to write it
    map.put("Input", stringBuffer);

    return 0;
}
来自protobuf:

SerializeToString(字符串*输出)常量

序列化消息并将字节存储在给定字符串中。请注意,字节是二进制的,而不是文本;我们只将string类用作方便的容器

因此,在IMap中将它们存储为字符串是不安全的,因为在非UTF-8格式的情况下很容易失败。我建议您将protobuf存储为字节向量,其中
hazelcast::byte
unsigned char

hz.getMap<string, vector<hazelcast::byte>>("proto_map");
hz.getMap(“proto_map”);

此解决方案可行,但由于protobuf作为数组序列化,我必须不断将数组复制到向量,反之亦然,才能进行反序列化。我想知道是否有更干净的方法…可能是通过Hazelcast的自定义序列化,我不确定如何直接存储二进制文件。可以通过使用实现Hazelcast序列化方法之一的包装器类来避免复制。在反序列化的写/读方法中,您可以手动(通过writeInt、readInt等)反序列化protobuf字段。或者,您可以对反序列化二进制protobuf字符串的每个字节使用writeByte/readByte。
hz.getMap<string, vector<hazelcast::byte>>("proto_map");