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

Apache Ignite(GridGain8.9.1)与.NET:AffinityKeyMapped配合Cache.Put报错求助

问题分析

报错核心是服务端与客户端对test.PersonValue的亲和键定义不一致:

  • DDL中affinity_key=ID是为表的**键类型(PersonKey)**指定的亲和规则,服务端元数据里值类型(PersonValue)的亲和键为null。
  • 客户端代码中,PersonValue继承自PersonKey,而PersonKey的ID字段带有[AffinityKeyMapped]特性,导致客户端认为PersonValue的亲和键是ID。
  • 两端元数据冲突,触发Binary type has different affinity key fields异常。
解决方案

方案1:拆分值对象与键对象的继承关系(推荐)

亲和键仅作用于缓存的键对象,值对象无需设置亲和键。修改PersonValue类,独立定义字段:

namespace test
{
    class PersonKey
    {
        [QuerySqlField]
        public int CITY_ID;

        [AffinityKeyMapped]
        public int ID;
    }

    // 取消继承,独立定义值对象字段
    class PersonValue
    {
        [QuerySqlField]
        public int CITY_ID;

        [QuerySqlField]
        public int ID;

        [QuerySqlField]
        public string NAME, COMPANY;

        [QuerySqlField]
        public int AGE;
    }

    class IgniteTest
    {
        public static void Main()
        {
            try
            {
                IgniteClientConfiguration cfg = new IgniteClientConfiguration
                {
                    Endpoints = new[] { "127.0.0.1:10800" }
                };

                using (var client = Ignition.StartClient(cfg))
                {
                    var cache = client.GetOrCreateCache<PersonKey, PersonValue>("Person");

                    PersonKey tableKey = new PersonKey();
                    PersonValue tableValue = new PersonValue();

                    tableKey.ID = 2;
                    tableKey.CITY_ID = 2;
                    tableValue.ID = 2;
                    tableValue.CITY_ID = 2;
                    tableValue.NAME = "2";
                    tableValue.AGE = 2;
                    tableValue.COMPANY = "2";
                    cache.Put(tableKey, tableValue);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

方案2:显式指定值对象的亲和键为null

若必须保留继承关系,通过[BinaryType]特性强制PersonValue的亲和键为null,与服务端元数据对齐:

namespace test
{
    class PersonKey
    {
        [QuerySqlField]
        public int CITY_ID;

        [AffinityKeyMapped]
        public int ID;
    }

    // 显式覆盖继承的亲和键设置
    [BinaryType(AffinityKeyFieldName = null)]
    class PersonValue : PersonKey
    {
        [QuerySqlField]
        public string NAME, COMPANY;

        [QuerySqlField]
        public int AGE;
    }

    // 其余代码保持不变
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 00:47:53