我正在开发一个 Spring 应用程序来连接到 apache Ignite 缓存以获取记录。首先,我运行 DataNode 缓存代码(如下所述)以从数据库中获取所有数据。接下来,当我尝试运行客户端代码来查询不同应用程序中的缓存时。我收到错误消息“无法找到类型为 Person 的 SQL 表”
DataNode缓存代码:
CacheConfiguration<String, Person> personCache = new CacheConfiguration<String, Person>();
personCache.setName("person:cache");
personCache.setRebalanceMode(CacheRebalanceMode.SYNC);
personCache.setReadThrough(true);
personCache.setWriteThrough(false);
personCache.setWriteBehindEnabled(false);
personCache.setCacheMode(CacheMode.PARTITIONED);
personCache.setIndexedTypes(String.class, Person.class);
personCache.setEvictionPolicy(new LruEvictionPolicy<>(100000));
personCache.setOnheapCacheEnabled(true);
personCache.setCacheStoreFactory(FactoryBuilder.factoryOf(PersonCacheStore.class));
configuration.setCacheConfiguration(personCache);
TcpDiscoverySpi tcpDiscoverySpi = new TcpDiscoverySpi();
TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
ipFinder.setAddresses(Arrays.asList("127.0.0.1:47500"));
tcpDiscoverySpi.setIpFinder(ipFinder);
configuration.setDiscoverySpi(tcpDiscoverySpi);
IgniteCache<String, Person> personCache = ignite.getOrCreateCache("person:cache");
personCache.loadCache(null);
人:
public class Person implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@QuerySqlField
private String name;
private Date dob;
public Person() {
super();
}
public Person(String name, Date dob) {
super();
this.name = name;
this.dob = dob;
}
}
PersonCacheStore:
public class PersonCacheStore implements CacheStore<String, Person> {
public Person load(String name) throws CacheLoaderException {
// code to load data from DB.
}
public void loadCache(IgniteBiInClosure<String, Person> clo, Object... arg1) throws CacheLoaderException {
// code to load data from DB.
}
}
查询缓存的客户端代码:
IgniteConfiguration configuration = new IgniteConfiguration();
configuration.setClientMode(true);
TcpDiscoverySpi tcpDiscoverySpi = new TcpDiscoverySpi();
TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
ipFinder.setAddresses(Arrays.asList("127.0.0.1:47500"));
tcpDiscoverySpi.setIpFinder(ipFinder);
configuration.setDiscoverySpi(tcpDiscoverySpi);
Ignite ignite = Ignition.start(configuration);
IgniteCache<String, Person> cache = ignite.getOrCreateCache("person:cache");
SqlQuery<String, Person> qry2 = new SqlQuery<String, Person>(Person.class,
"select * from Person where name = ?");
qry2.setArgs("Ram");
List<Entry<String, Person>> res = cache.query(qry2).getAll();
for (Entry<String, Person> entry : res) {
}
帮我解决这个问题。
最佳答案
目前尚不清楚如何在 DataNode
上创建缓存。您创建一个 CacheConfiguration
对象,然后将其添加到 Ignite 配置中,但实际上看起来这是在启动节点后发生的。如果是这种情况,则缓存是使用默认设置创建的,因此不知道 SQL 配置。
有两个选项可以修复:
- 确保您完全创建
IgniteConfiguration
(包括CacheConfiguration
)在调用Ignition.start()
之前有了这个配置。然后使用ignite.cache("person:cache")
获取缓存;如果你这样做而不是使用getOrCreateCache
,你会得到null
而不是错误配置缓存以防万一搞砸了某些东西,这样会更容易定位问题。 - 不提供
CacheConfiguration
作为IgniteConfiguration
的一部分,并使用getOrCreateCache
创建缓存提供配置对象而不仅仅是名称。
关于java - Apache Ignite 找不到 SQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47186981/