在.net客户端

时间:2017-08-03 13:49:42

标签: .net hazelcast

Hazelcast支持团队成员告诉我,我需要使用IPortable实现才能在Map.Values()中使用IPredicate查询。

我可以成功地将值映射到地图中,并使用Map.Values()获取所有值。请参阅下面的示例代码。

但是,当我尝试使用SqlPredicate查询Map.Values()时,我收到错误消息:

“Hazelcast.Net.dll中发生了'Hazelcast.Core.QueryException类型的未处理异常'。附加信息:java.lang.IllegalStateException:流中的字段数[2]与ClassDefinition {factoryId = 1,classId不匹配= 1,版本= 0,fieldDefinitions = [FieldDefinitionImpl {index = 0,fieldName ='Name',type = UTF,...“

我的配置:

        var clientConfig = new ClientConfig();
        clientConfig.GetNetworkConfig().AddAddress(hazelCastIpAddress);
        clientConfig.SetGroupConfig(new GroupConfig(groupId, password));
        clientConfig.GetSerializationConfig().AddPortableFactory(MyDataSerializableFactory.FactoryId, new MyDataSerializableFactory());

我的Hazelcast存储库:

    private IHazelcastInstance _hazelcastClient;
    public IMap<object, TEntity> Map { get; }

    public HazelCastRepository(IHazelcastInstance hazelcastClient)
    {
        _hazelcastClient = hazelcastClient;
        Map = hazelcastClient.GetMap<object, TEntity>(typeof(TEntity).Name);
    }

    public void Create(TEntity entity)
    {
        Map.Set(entity.MapKey, entity); // map sets correctly with MapKey
    }

    public ICollection<TEntity> Get(string where)
    {
        var x = Map.Values(); // get all values and it works properly

        var y = new EqualPredicate("Id", 6);

        return Map.Values(y); // code breaks here with an error java.lang.IllegalStateException bla bla...
    }

IPortableFactory实现:

public class MyDataSerializableFactory : IPortableFactory
{

    public const int FactoryId = 1;

    public IPortable Create(int classId)
    {
        var type = HazelcastContext.Models[classId];

        var instance = Activator.CreateInstance(type);

        return (IPortable)instance;
    }
}

public class HazelcastContext
{

    public static Dictionary<int, Type> Models = new Dictionary<int, Type>() {
        { 1, typeof(MyTestModel)},
        { 2, typeof(MySecondTestModel) }

    };

}

我的对象类:

public class MyTestModel: IPortable
{


    public string Name { get; set; }
    public long Id { get; set; }

    public Guid MapKey
    {
        get; set;
    } = Guid.NewGuid();

    public int GetClassId()
    {
        return 1;
    }

    public int GetFactoryId()
    {
        return 1;
    }

    public void ReadPortable(IPortableReader reader)
    {
        Name = reader.ReadUTF("Name");
        Id = reader.ReadLong("Id");
    }

    public void WritePortable(IPortableWriter writer)
    {
        writer.WriteUTF("Name", Name);
        writer.WriteLong("Id", Id);

    }
}

1 个答案:

答案 0 :(得分:0)

那些来错误的错误,我有解决这个问题的方法。

我通过他们的gitter.im房间联系了hazelcast的支持团队并提出了一些建议,但这对我没有帮助。 Mastering Hazelcast IMDB中有一段说:

  

getFactoryId 应返回唯一的正数,而 getId 应在其对应的PersonDataSerializableFactory内返回唯一的正数。因此,只要 getFactoryId 不同,IdentifiedDataSerializable实现就可以返回相同的ID。

所以,问题是 factoryId 。如果您遇到这样的错误,请清除hazelcast上的数据并更改您使用的 factoryId 。然后,hazelcast会做到这一点。

我相信这是因为我做了很多关于淡褐色的测试,因为它非常“脆弱”,不知何故她感到困惑。

相关问题