2016-12-07 101 views
1

當我運行以下代碼時,拋出了針對緩存禁用Apache Ignite javax.cache.CacheException:Indexing的異常。 不知道問題出在哪裏。有人可以幫忙嗎?謝謝!Apache Ignite javax.cache.CacheException:對緩存禁用索引

package com.xyz.ignite.sqlgrid; 

import org.apache.ignite.Ignite; 
import org.apache.ignite.IgniteCache; 
import org.apache.ignite.Ignition; 
import org.apache.ignite.configuration.CacheConfiguration; 
import org.apache.ignite.configuration.IgniteConfiguration; 
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi; 
import org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder; 

import javax.cache.Cache; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.util.Arrays; 


import org.apache.ignite.cache.query.annotations.QuerySqlField; 

class Person { 
    @QuerySqlField 
    private String name; 

    @QuerySqlField 
    private int age; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getAge() { 
     return age; 
    } 

    public void setAge(int age) { 
     this.age = age; 
    } 
} 

public class IgniteSQLQueryTest { 
    public static void main(String[] args) throws Exception { 
     String configPath = "D:/apache-ignite-fabric-1.7.0-bin/config/default-config.xml"; 
     Ignite ignite = Ignition.start(configPath); 
     CacheConfiguration<Integer, Person> cfg = new CacheConfiguration<Integer, Person>(); 
     cfg.setName("person_cache"); 
     cfg.setIndexedTypes(Integer.class, Person.class); 
     Cache cache = ignite.getOrCreateCache(cfg); 
     String cacheName = cache.getName(); 

     for (int i = 1; i <= 100; i++) { 
      Person p = new Person(); 
      p.setAge(i); 
      p.setName("name-" + i); 
      cache.put(i, p); 
     } 
     for (int i = 1; i <= 100; i++) { 
      System.out.println("Cache get:" + cache.get(i)); 
     } 

     Class.forName("org.apache.ignite.IgniteJdbcDriver"); 

     Connection conn = DriverManager.getConnection(String.format("jdbc:ignite:cfg://cache=%[email protected]:///%s", cacheName, configPath)); 
     ResultSet rs = conn.createStatement().executeQuery("select name from Person"); 

     while (rs.next()) { 
      String name1 = rs.getString(1); 
      System.out.println(name1); 
     } 

     ignite.close(); 
    } 
} 

發生異常,異常消息是:

Exception in thread "main" java.sql.SQLException: Failed to query Ignite. 
    at org.apache.ignite.internal.jdbc2.JdbcStatement.executeQuery(JdbcStatement.java:115) 
    at com.xyz.ignite.sqlgrid.IgniteSQLQueryTest.main(IgniteSQLQueryTest.java:69) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:88) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55) 
    at java.lang.reflect.Method.invoke(Method.java:613) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 
Caused by: javax.cache.CacheException: Indexing is disabled for cache: person_cache. Use setIndexedTypes or setTypeMetadata methods on CacheConfiguration to enable. 
    at org.apache.ignite.internal.processors.cache.IgniteCacheProxy.validate(IgniteCacheProxy.java:732) 
    at org.apache.ignite.internal.processors.cache.IgniteCacheProxy.query(IgniteCacheProxy.java:664) 
    at org.apache.ignite.internal.jdbc2.JdbcQueryTask.call(JdbcQueryTask.java:158) 
    at org.apache.ignite.internal.jdbc2.JdbcStatement.executeQuery(JdbcStatement.java:102) 

回答

0

看來,我覺得問題出在哪裏lies.The POJO的人應該是

class Person implements Serializable { 
    @QuerySqlField(index = true) 
    private String name; 

    @QuerySqlField(index = true) 
    private int age; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getAge() { 
     return age; 
    } 

    public void setAge(int age) { 
     this.age = age; 
    } 
} 

的QuerySqlField註釋應該添加一個index = true參數

+0

該參數僅控制是否爲某個特定r字段。如果它是'假'或未設置,該字段將不會被索引,但它將是可查詢的。話雖如此,我懷疑這是例外的原因。你能否提供你的測試(最好是一個小型的GitHub項目),以便我可以複製和調查? –