当我运行以下代码时,会抛出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=%s@file:///%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 :(得分:0)
看起来我找到了问题所在。人员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
参数