仅从 Apache Ignite 缓存中读取密钥

Read keys only from Apache Ignite cache

提问人:Justin Mathew 提问时间:6/16/2022 最后编辑:Pavel TupitsynJustin Mathew 更新时间:6/24/2022 访问量:238

问:

我想找到一种最佳方法,从保存在 Ignite 缓存中的对象列表中读取键。以下是源代码中的用例。当前代码适用于小尺寸的对象列表,但对于大型数据集失败或花费大量时间。我认为失败是由于以下代码将整个数据集加载到厚客户端内存中,然后找到键。对于我的要求,我只想从对象列表中的每个对象中读取键(子键)。

 BinaryFormatter bf = new BinaryFormatter();
            var cache = ignite.GetOrCreateCache<int, IBinaryObject>("my-cache");

            IBinary binary = cache.Ignite.GetBinary();

            // Populate persons.
            cache[1] = binary.GetBuilder("PersonType")
                .SetField("Name", "James Wilson")
                .SetField("CompanyId", -1)
                .Build();

            cache[2] = binary.GetBuilder("PersonType")
                .SetField("Name", "Daniel Adams")
                .SetField("CompanyId", -1)
                .Build();

            
           Console.WriteLine("**************************************/n/n");
           Console.WriteLine("Count : " + ignite.GetCache<int, IBinaryObject>("my-cache").GetSize());

           // The below line fails or taking lots of time for a very big dataset becuase I feel ignite load the entire data into thick client memory to get the keys.
           // is there anyway just to read the keys instead of loading the full dataset
   [use case :] 
   cache.Query(new Apache.Ignite.Core.Cache.Query.ScanQuery<string, IBinaryObject>()).ToList().ForEach(item => Console.WriteLine(item.));
            Console.ReadLine();

我也尝试了以下代码,但仍然需要很多时间,所以我不确定这是最好的方法

ICollection<string> _keys = new List<string>();
        var cache = ignite.GetCache<string, IBinaryObject>(name).WithKeepBinary<string, IBinaryObject>();

        using (var cursor = cache.Query(new Apache.Ignite.Core.Cache.Query.ScanQuery<string, IBinaryObject>()))
        {
            foreach (var entry in cursor)
            {
                _keys.Add(entry.Key);
            }
        }
点燃 gridgain

评论


答:

0赞 Pavel Tupitsyn 6/17/2022 #1

仅查询部分缓存条目的最有效方法是 SQL。

仅密钥检索示例:

var ignite = Ignition.Start();
var queryEntity = new QueryEntity
{
    KeyType = typeof(int),
    ValueTypeName = "Person",
    TableName = "People"
};

var configuration = new CacheConfiguration("my-cache", queryEntity);
var cache = ignite.GetOrCreateCache<int, object>(configuration).WithKeepBinary<int, IBinaryObject>();

cache[1] = ignite.GetBinary().GetBuilder("Person").SetField("Name", "A").SetField("Id", 1).Build();;

// SQL
var cursor = cache.Query(new SqlFieldsQuery("select _key from \"my-cache\".People"));

foreach (var row in cursor)
{
    var key = (int)row[0];
    Console.WriteLine(key);
}

// LINQ to SQL
var keys = cache.AsCacheQueryable().Select(e => e.Key);

foreach (var key in keys)
{
    Console.WriteLine(key);
}

评论

0赞 Justin Mathew 6/18/2022
非常感谢帕维尔的回答。我需要更多时间来应用解决方案,因为我在数据使用(胖客户端)团队中,并且需要了解发布团队的类型和密钥。我会早点回来的。
0赞 Justin Mathew 6/21/2022
亲爱的帕维尔,我们将 ignite 缓存中的所有对象都存储为二进制文件。您能否确认上述解决方案也适用于二进制对象?提前致谢。
0赞 Pavel Tupitsyn 6/21/2022
@JustinMathew是的,它也适用于二进制对象。诀窍在于,我们不定义任何自定义 SQL 列,只定义默认的 和 将可用。_key_val
0赞 Justin Mathew 6/21/2022
对于上面的代码示例,我尝试了以下带有二进制对象的代码,但下面的两行都返回相同的错误“无法解析查询。未找到表“my”;SQL语句:select _key from my-cache [42102-199]”。我在创建对象时没有使用 QueryEntity - 这是一个问题吗?var binaryCache = 点燃。GetCache<string, IBinaryObject>(“我的缓存”)。WithKeepBinary<字符串, IBinaryObject>();var cursor1 = binaryCache.Query(new SqlFieldsQuery(“从 my-cache 中选择_key”));var cursor2 = binaryCache.Query(new SqlFieldsQuery(“select _key from PersonType”));
0赞 Pavel Tupitsyn 6/22/2022
创建缓存时应使用此选项来启用 SQL 功能。QueryEntity