我正在开发一个 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
提供配置对象而不仅仅是名称来创建缓存。这是文档中的示例:注册索引类型。
准确解决您的问题。
Apache Ignite 版本 = 2.16.0
在关注 Apache Ignite 示例时,我遇到了类似的问题,并且花了很多时间后,发现我使用了不完整的表名。
personCache.setName("person:cache");
这将您的架构名称定义为“person:cache”
Ignite 为通过编程接口或 XML 配置之一创建的每个缓存创建一个架构。参考
personCache.setIndexedTypes(String.class, Person.class);
这将您的表名称定义为“Person”
类型名称在 SQL 查询中用作表名称。在本例中,我们的表名称将为 Person(模式名称的用法和定义在模式部分中进行了解释)。参考
所以你的查询将变成
SELECT * from "person:cache".Person where name = ?"
为了完整起见,这是我的节点代码
public class Main {
private static final String PERSON_CACHE = "Persons";
public static void main(String[] args) throws Exception {
// -----------------------------
try ( Ignite ignite = Ignition.start("config/example-sql.xml")) {
CacheConfiguration<Long,Person> personCacheCfg = new CacheConfiguration<>();
personCacheCfg.setCacheMode(CacheMode.PARTITIONED); // Default.
personCacheCfg.setIndexedTypes(Long.class, Person.class);
personCacheCfg.setName(PERSON_CACHE);
try {
IgniteCache<Long, Person> cache = ignite.createCache(personCacheCfg);
// Populate caches.
initialize(cache);
} catch ( Exception ex ) {
System.out.println("Exception in main = [" + ex.getMessage() + "]");
}
finally {
ignite.destroyCache(PERSON_CACHE);
}
print("SQL queries example finished.");
}
}
private static void initialize(IgniteCache<Long, Person> personCache) {
// Clear caches before running the example.
personCache.clear();
// People.
Person p1 = new Person("John", "Doe", 2000, "John Doe has Master Degree.");
Person p2 = new Person("Jane", "Doe", 1000, "Jane Doe has Bachelor Degree.");
Person p3 = new Person("John", "Smith", 1000, "John Smith has Bachelor Degree.");
Person p4 = new Person("Jane", "Smith", 2000, "Jane Smith has Master Degree.");
// These Person objects are not collocated with their organizations.
personCache.put(p1.id, p1);
personCache.put(p2.id, p2);
personCache.put(p3.id, p3);
personCache.put(p4.id, p4);
}
这是我的Python瘦客户端,使用SQL读取数据
def read_records(client):
query = '''
SELECT * FROM \"Persons\".PERSON;
'''
cursor = execute_query(client, query, None)
if cursor is not None:
for row in cursor:
print(*row)
def execute_query(client, query, data):
cursor = None
try:
if data is not None:
print("Query Arguments = [" + str(data) + "]")
cursor = client.sql(query_str=query, query_args=data)
else:
cursor = client.sql(query_str=query)
except Exception as ex:
print("Exception executing query = [" + str(ex) + "]")
return cursor
注意:您还可以使用 sqlline 获取缓存的 SQL 视图
.\sqlline.bat --verbose=true -u jdbc:ignite:thin://127.0.0.1/