【问题标题】:Apache Ignite SQL query composed primary key NETApache Ignite SQL 查询复合主键 NET
【发布时间】:2017-04-04 15:03:53
【问题描述】:

我想知道是否可以以某种方式查询我用作主键的类的某个属性。 我的想法类似于下面的代码。使用 _key.NAME 我收到错误消息说列不存在,我也尝试使用 TestOjectPK.NAME,但没有成功:

var fieldsQuery = new SqlFieldsQuery("select _val,_key from TestObject where _key.NAME= ? and VALUE= ?", "name1","value");
IQueryCursor<IList> queryCursor = cache.QueryFields(fieldsQuery);

这是我用 3 列表示某个表的类,其中 2 列是 PK。

class TestObjectPK : IBinarizable
{
    private Int32 id;
    private String name;

    [QuerySqlField]
    public string NAME
    {
        get{return this.name;}
        set{this.name = value;}
    }

    [QuerySqlField]
    public Int32 ID
    {
        get{return this.id;}
        set{this.id = value;}
    }

    public void WriteBinary(IBinaryWriter writer)
    {
        writer.WriteInt("ID", ID);
        writer.WriteString("NAME", NAME);
    }

    public void ReadBinary(IBinaryReader reader)
    {
        ID = reader.ReadInt("ID");
        NAME = reader.ReadString("NAME");
    }

    public override int GetHashCode()
    {
        return id.GetHashCode() + name.GetHashCode();
    }

    public override bool Equals(object obj)
    {
        return id == ((TestObjectPK)obj).id && name == ((TestObjectPK)obj).NAME;
    }
}


class TestObject : IBinarizable
{
    private String value2;
    [QuerySqlField]
    public String VALUE
    {
        get { return this.value2; }
        set { this.value2 = value; }
    }


    public void WriteBinary(IBinaryWriter writer)
    {
        writer.WriteString("VALUE", VALUE);
    }

    public void ReadBinary(IBinaryReader reader)
    {
        VALUE = reader.ReadString("VALUE");
    }
    public override int GetHashCode()
    {
        return VALUE.GetHashCode();
    }

    public override bool Equals(object obj)
    {
        return VALUE == ((TestObject)obj).VALUE;
    }

}

【问题讨论】:

    标签: c# .net ignite


    【解决方案1】:

    键和值对象中所有标有[QuerySqlField] 的字段都直接在SQL 表中结束。

    所以正确的查询应该是

    select _val,_key from TestObject where NAME=? and VALUE=?
    

    还要确保为 SQL 配置键和值类型:

    new CacheConfiguration("TestObject", 
            new QueryEntity(typeof(TestObjectPK), typeof(TestObject)))
    

    【讨论】:

    • Pavel 我试过你说的。但是 Ignite 仍然会抛出与 NAME column not found 相关的错误。也许是我创建缓存时出错了? ICache&lt;TestObjectPK, TestObject&gt; cache = ignite.GetOrCreateCache&lt;TestObjectPK, TestObject&gt;(new CacheConfiguration("TestObject", typeof(TestObject))); TestObjectPK testObjectPK = new TestObjectPK(); testObjectPK.NAME = "h1"; testObjectPK.ID = 1; TestObject testObject = new TestObject(); testObject.VALUE = "value1"; cache.Put(testObjectPK, testObject);谢谢
    • 确切地说,要在查询中使用键字段,您还必须指定键类型:new CacheConfiguration("TestObject", new QueryEntity(typeof(TestObjectPK), typeof(TestObject)))。我已经编辑了答案。
    • 谢谢。对于所有有相同问题的人,我创建了一个包含完整示例的 GitHub 存储库:github.com/juanpale/ExampleApacheIgniteQueryCompositeObjectNET
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多