2017-08-14 71 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

更新了我的答案 –

相關問題