Fetch data from Ignite using AsCacheQueryable with Generic query perfromance efficient

Viewed 237

I am trying to add the Where function in my class CacheHandler to fetch data from Ignite cache What I am looking for making the generic query work with AsCacheQueryable and Where clause to return the data from ignite cache

//Invoking

var data = CacheHandler.Where2<LocalDb>(x => x.Value.localId== localId).FirstOrDefault();

//Function

    public ICache<string, T> GetCache<T>()
    {            
        var cacheName = CacheNames.GetCacheNameFromType<T>();
        
        if (cacheName == "")
        {
            throw new Exception("Invalid Cache name");
        }

        return _ignite.GetCache<string, T>(cacheName);
    }

    public List<T> Where2<T>(Func<ICacheEntry<string, T>, bool> query)
    {
        // 'System.Collections.Generic.List<Apache.Ignite.Core.Cache.ICacheEntry<string, T>>'
        var queryResult = GetCache<T>().AsCacheQueryable().Where(x => query(x));

        if (queryResult == null)
        {
            return new List<T>();
        }

        return queryResult.Select(x => x.Value).ToList();
    }

Ignore the table Name in below error with above code context. But Similar error for all tables when trying to fetch data. All my fields in model have got [QuerySqlField]

I am getting following error:

    ""Hwb_Entity_Cache"".ENTITY AS _T0 WHERE _T0._KEY,[*] _T0._VAL "; SQL statement:
select _T0._VAL from "Hwb_Entity_Cache".ENTITY as _T0 where _T0._KEY, _T0._VAL [42000-197] (6e3f4055)
Apache.Ignite.Core.Common.IgniteException: Failed to parse query. Syntax error in SQL statement "SELECT _T0._VAL FROM ""Hwb_Entity_Cache"".ENTITY AS _T0 WHERE _T0._KEY,[*] _T0._VAL "; SQL statement:
select _T0._VAL from "Hwb_Entity_Cache".ENTITY as _T0 where _T0._KEY, _T0._VAL [42000-197]
 ---> Apache.Ignite.Core.Common.JavaException: class org.apache.ignite.IgniteCheckedException: Failed to parse query. Syntax error in SQL statement "SELECT _T0._VAL FROM ""Hwb_Entity_Cache"".ENTITY AS _T0 WHERE _T0._KEY,[*] _T0._VAL "; SQL statement:
select _T0._VAL from "Hwb_Entity_Cache".ENTITY as _T0 where _T0._KEY, _T0._VAL [42000-197]
    at org.apache.ignite.internal.processors.platform.utils.PlatformUtils.unwrapQueryException(PlatformUtils.java:520)
    at org.apache.ignite.internal.processors.platform.cache.PlatformCache.runFieldsQuery(PlatformCache.java:1321)
    at org.apache.ignite.internal.processors.platform.cache.PlatformCache.processInStreamOutObject(PlatformCache.java:940)
    at org.apache.ignite.internal.processors.platform.PlatformTargetProxyImpl.inStreamOutObject(PlatformTargetProxyImpl.java:79)
Caused by: javax.cache.CacheException: Failed to parse query. Syntax error in SQL statement "SELECT _T0._VAL FROM ""Hwb_Entity_Cache"".ENTITY AS _T0 WHERE _T0._KEY,[*] _T0._VAL "; SQL statement:
select _T0._VAL from "Hwb_Entity_Cache".ENTITY as _T0 where _T0._KEY, _T0._VAL [42000-197]
    at org.apache.ignite.internal.processors.cache.IgniteCacheProxyImpl.query(IgniteCacheProxyImpl.java:817)
    at org.apache.ignite.internal.processors.cache.GatewayProtectedCacheProxy.query(GatewayProtectedCacheProxy.java:412)
    at org.apache.ignite.internal.processors.platform.cache.PlatformCache.runFieldsQuery(PlatformCache.java:1315)
    ... 2 more
Caused by: class org.apache.ignite.internal.processors.query.IgniteSQLException: Failed to parse query. Syntax error in SQL statement "SELECT _T0._VAL FROM ""Hwb_Entity_Cache"".ENTITY AS _T0 WHERE _T0._KEY,[*] _T0._VAL "; SQL statement:
select _T0._VAL from "Hwb_Entity_Cache".ENTITY as _T0 where _T0._KEY, _T0._VAL [42000-197]
    at org.apache.ignite.internal.processors.query.h2.QueryParser.parseH2(QueryParser.java:584)
    at org.apache.ignite.internal.processors.query.h2.QueryParser.parse0(QueryParser.java:210)
    at org.apache.ignite.internal.processors.query.h2.QueryParser.parse(QueryParser.java:131)
    at org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing.querySqlFields(IgniteH2Indexing.java:1103)
    at org.apache.ignite.internal.processors.query.GridQueryProcessor$3.applyx(GridQueryProcessor.java:2406)
    at org.apache.ignite.internal.processors.query.GridQueryProcessor$3.applyx(GridQueryProcessor.java:2402)
    at org.apache.ignite.internal.util.lang.IgniteOutClosureX.apply(IgniteOutClosureX.java:36)
    at org.apache.ignite.internal.processors.query.GridQueryProcessor.executeQuery(GridQueryProcessor.java:2919)
    at org.apache.ignite.internal.processors.query.GridQueryProcessor.lambda$querySqlFields$1(GridQueryProcessor.java:2422)
    at org.apache.ignite.internal.processors.query.GridQueryProcessor.executeQuerySafe(GridQueryProcessor.java:2460)
    at org.apache.ignite.internal.processors.query.GridQueryProcessor.querySqlFields(GridQueryProcessor.java:2396)
    at org.apache.ignite.internal.processors.query.GridQueryProcessor.querySqlFields(GridQueryProcessor.java:2323)
    at org.apache.ignite.internal.processors.cache.IgniteCacheProxyImpl.query(IgniteCacheProxyImpl.java:802)
    ... 4 more

Could someone suggest to correct way of passing and reading query here?

1 Answers

AsCacheQueryable is a part of Ignite.NET LINQ provider - it converts LINQ expression trees to the Ignite SQL dialect. The problem with code above is Func<ICacheEntry<string, T>, bool> query - it should be Expression<Func<ICacheEntry<string, T>, bool>> query. This works for me:

public List<T> Where2<T>(Expression<Func<ICacheEntry<string, T>, bool>> query)
{
    var queryResult = GetCache<T>().AsCacheQueryable().Where(query);

    return queryResult.Select(x => x.Value).ToList();
}

Related