You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现GridGain C++瘦客户端自定义类型序列化?

GridGain C++瘦客户端自定义类型序列化完整可编译示例

错误原因分析

你遇到的C2027: use of undefined type 'ignite::binary::BinaryReader'错误,是因为代码仅通过前向声明引用了BinaryReader/BinaryWriter,但未包含它们的完整定义头文件。官方文档的代码片段省略了必要的头文件引用,导致编译器无法识别类的具体方法。

完整可编译代码示例

1. 自定义结构体与序列化模板特化(user.h)

#pragma once

#include <ignite/binary/binary.h>
#include <ignite/binary/binary_reader.h>
#include <ignite/binary/binary_writer.h>
#include <string>

// 自定义结构体
struct User {
    int32_t id;
    std::string name;
    int32_t age;

    // 必须提供默认构造函数,BinaryReader需先创建空对象再填充字段
    User() : id(0), age(0) {}

    User(int32_t id, const std::string& name, int32_t age)
        : id(id), name(name), age(age) {}
};

// 特化BinaryType模板,实现序列化逻辑
namespace ignite {
namespace binary {

template<>
struct BinaryType<User> {
    // 返回全局唯一的类型名称,用于GridGain识别类型
    static const char* GetTypeName() {
        return "User";
    }

    // 返回类型ID(可选,不实现则自动基于类型名称生成哈希值)
    static int32_t GetTypeId() {
        return GetBinaryStringHashCode(GetTypeName());
    }

    // 序列化:将User对象写入BinaryWriter
    static void Write(BinaryWriter& writer, const User& obj) {
        writer.WriteInt32("id", obj.id);
        writer.WriteString("name", obj.name);
        writer.WriteInt32("age", obj.age);
    }

    // 反序列化:从BinaryReader读取数据填充User对象
    static void Read(BinaryReader& reader, User& obj) {
        obj.id = reader.ReadInt32("id");
        obj.name = reader.ReadString("name");
        obj.age = reader.ReadInt32("age");
    }
};

} // namespace binary
} // namespace ignite

2. 主程序读写逻辑(main.cpp)

#include <iostream>
#include <ignite/thin/ignite_client.h>
#include <ignite/thin/ignite_client_configuration.h>
#include "user.h"

using namespace ignite;
using namespace ignite::thin;

int main() {
    try {
        // 配置客户端连接参数
        IgniteClientConfiguration cfg;
        cfg.SetEndpoints("127.0.0.1:10800"); // 替换为你的GridGain服务器地址

        // 启动客户端并建立连接
        IgniteClient client = IgniteClient::Start(cfg);

        // 获取或创建缓存(键为int32_t,值为自定义User类型)
        cache::CacheClient<int32_t, User> cache = client.GetOrCreateCache<int32_t, User>("user_cache");

        // 写入自定义对象到缓存
        User user(1, "Alice", 30);
        cache.Put(1, user);
        std::cout << "已写入用户: ID=" << user.id << ", 姓名=" << user.name << ", 年龄=" << user.age << std::endl;

        // 从缓存读取自定义对象
        User retrievedUser = cache.Get(1);
        std::cout << "读取到用户: ID=" << retrievedUser.id << ", 姓名=" << retrievedUser.name << ", 年龄=" << retrievedUser.age << std::endl;
    }
    catch (const IgniteError& err) {
        std::cerr << "GridGain错误: " << err.GetText() << std::endl;
        return 1;
    }

    return 0;
}

编译注意事项

  1. 头文件路径:编译时需指定GridGain的include目录,例如-I/path/to/gridgain/include
  2. 链接库:
    • Windows(MSVC):链接ignite.lib和ignite_thin.lib
    • Linux:链接libignite.so和libignite_thin.so
  3. C++标准:需使用C++11或更高版本,编译参数添加-std=c++11(或对应版本)

关键要点

  • 自定义结构体必须提供默认构造函数,否则BinaryReader无法创建对象实例
  • BinaryType模板特化的GetTypeName必须全局唯一,避免与其他类型冲突
  • 序列化/反序列化的字段名称(如"id"、"name")必须完全一致,否则无法正确读取数据

内容的提问来源于stack exchange,提问作者Boris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 21:50:10