2017-08-14 72 views
0

尽管配置中已设置了到期时间,但数据仍未从Ignite高速缓存中逐出。我注意到当我使用SQL将数据插入缓存表时,发生此问题如果通过SQL插入,Ignite高速缓存条目不会被驱逐

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]尝试叶甫的回答后,执行以下操作:

// 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) 
+0

请在您配置的地方共享缓存配置EvictionPolicy –

+0

编辑我的问题以包含我在代码中设置的确切配置。除此之外,我还没有做过更多的配置。我假设ExpiryPolicy与EvictionPolicy相同。或者我错了? –

回答

1

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

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

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

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

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.addCacheConfiguration(cfg); 
+0

我尝试了“WITH \”template = CACHE_NAME \“”条款。我用我得到的例外更新了我的问题。 –

+0

更新了我的答案 –

相关问题