【问题标题】:Ignite Cache entries not evicted if inserted via SQL如果通过 SQL 插入,Ignite Cache 条目不会被驱逐
【发布时间】:2017-08-14 11:04:10
【问题描述】:

看起来尽管配置中设置了过期时间,但数据并未从 Ignite 缓存中清除。我注意到这个问题发生在当我使用 SQL 将数据插入缓存表时。

 emplCache.query(new SqlFieldsQuery(
   "CREATE TABLE Employee (id LONG PRIMARY KEY, firstName VARCHAR, lastName VARCHAR, 
    salary DECIMAL, gender VARCHAR)")).getAll();
 SqlFieldsQuery insertqry = new SqlFieldsQuery("INSERT INTO Employee (id, firstName,
    lastName, salary, gender) values (?, ?, ?, ?, ?)");
 emplCache.query(insertqry.setArgs(Long.toString(count), words[1], words[2],
    words[3], words[4])).getAll();

这就是我配置过期的方式:

CacheConfiguration<?, ?> cfg = new CacheConfiguration<>(cacheName);
    cfg.setCacheMode(CacheMode.PARTITIONED);
    cfg.setName(cacheName);
    cfg.setSqlSchema("PUBLIC");
    cfg.setAtomicityMode(CacheAtomicityMode.ATOMIC);
    cfg.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(SECONDS, 1)));

但是,如果我使用 dataStreamer 插入数据,我看不到这个问题。

IgniteDataStreamer<Integer, Employee> stmr = ignite.dataStreamer(cfg.getName())
 stmr.addData(id, emp);

我错过了一些配置设置吗?

[PS] 在 Evgenii 的回答之后尝试了以下操作:

// This is a util method that returns a cache config for the given cache name and the expiry time in seconds
        CacheConfiguration<?, ?> cfg = CacheConfig.getCacheConfig("emplCache", 1);
        IgniteCache<?, ?> emplCache = ignite.getOrCreateCache(cfg);
        emplCache.query(new SqlFieldsQuery(
                    "CREATE TABLE Employee (id LONG PRIMARY KEY, firstName VARCHAR, lastName VARCHAR, salary DECIMAL, gender VARCHAR)"
                    + "WITH \"template=emplCache\" ")).getAll();

现在我得到这个缓存不存在的异常。但我试图在创建缓存后创建表:

 Exception in thread "main" javax.cache.CacheException: class org.apache.ignite.internal.processors.query.IgniteSQLException: Cache doesn't exist: emplCache
at org.apache.ignite.internal.processors.cache.IgniteCacheProxy.query(IgniteCacheProxy.java:807)
at org.apache.ignite.internal.processors.cache.IgniteCacheProxy.query(IgniteCacheProxy.java:765)
at com.demo.ignite.svc.CsvStreamer.main(CsvStreamer.java:33)
Caused by: class org.apache.ignite.internal.processors.query.IgniteSQLException: Cache doesn't exist: emplCache
at org.apache.ignite.internal.processors.query.h2.ddl.DdlStatementsProcessor.convert(DdlStatementsProcessor.java:277)
at org.apache.ignite.internal.processors.query.h2.ddl.DdlStatementsProcessor.runDdlStatement(DdlStatementsProcessor.java:221)
at org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing.queryDistributedSqlFields(IgniteH2Indexing.java:1331)
at org.apache.ignite.internal.processors.query.GridQueryProcessor$5.applyx(GridQueryProcessor.java:1815)
at org.apache.ignite.internal.processors.query.GridQueryProcessor$5.applyx(GridQueryProcessor.java:1813)
at org.apache.ignite.internal.util.lang.IgniteOutClosureX.apply(IgniteOutClosureX.java:36)
at org.apache.ignite.internal.processors.query.GridQueryProcessor.executeQuery(GridQueryProcessor.java:2293)
at org.apache.ignite.internal.processors.query.GridQueryProcessor.querySqlFields(GridQueryProcessor.java:1820)
at org.apache.ignite.internal.processors.cache.IgniteCacheProxy.query(IgniteCacheProxy.java:795)

【问题讨论】:

  • 请分享您配置 EvictionPolicy 的缓存配置
  • 编辑了我的问题以包含我在代码中设置的确切配置。除此之外,我没有做任何更多的配置。我假设 ExpiryPolicy 与 EvictionPolicy 相同。还是我错了?

标签: ignite


【解决方案1】:

Table Employee 将在另一个缓存中创建(将为它创建),这就是为什么配置的 ExpiryPolicy 不会被使用。

To configure this new cache add "WITH \"template=CACHE_NAME\""

其中 CACHE_NAME 是配置中缓存(或模板)的名称。在您的情况下,它是 cacheName;

例如,将您的创建脚本更改为:

emplCache.query(new SqlFieldsQuery(
            "CREATE TABLE Employee (id LONG PRIMARY KEY, firstName VARCHAR, lastName VARCHAR, salary DECIMAL, gender VARCHAR) " +
                "WITH \"template=employee\" ")).getAll();

这里是link to doc

如果从 java 配置缓存,则需要将此名为 emplCache 的缓存添加到 Ignite:

    ignite.addCacheConfiguration(cfg);

【讨论】:

  • 我尝试了 "WITH \"template=CACHE_NAME\"" 子句。我已经更新了我的问题,但我得到了例外。
猜你喜欢
  • 1970-01-01
  • 2019-08-03
  • 1970-01-01
  • 2011-01-17
  • 1970-01-01
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多