官方文档的叙述可能有些不清楚,我做了一个测试,并且可以成功运行,待会儿后面贴出小栗子.
两步操作:
第一步在属性值处贴上@QuerySqlField注解
第二部设置key和value类型
Person.java
package test.ignite.client;import org.apache.ignite.cache.query.annotations.QuerySqlField;public class Person { @QuerySqlField private Integer id; @QuerySqlField private String name; @QuerySqlField private String age; public String getName() { return name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; }}
测试类:
package test.ignite.client;import java.util.List;import org.apache.ignite.Ignite;import org.apache.ignite.IgniteCache;import org.apache.ignite.Ignition;import org.apache.ignite.cache.CacheAtomicityMode;import org.apache.ignite.cache.query.SqlQuery;import org.apache.ignite.configuration.CacheConfiguration;import org.apache.ignite.internal.processors.cache.CacheEntryImpl;public class MMM { public static void main(String[] args) { System.out.println("======================================================"); Ignite ignite = Ignition.start("ignite.xml"); CacheConfigurationcfg = new CacheConfiguration (); cfg.setName("Person"); cfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL); cfg.setIndexedTypes(Integer.class, Person.class); ignite.getOrCreateCache(cfg); IgniteCache cache = ignite.cache("Person"); Person a = new Person(); a.setId(1); a.setAge("12"); a.setName("32323"); cache.put(1, a); SqlQuery sql = new SqlQuery(Person.class, "id <> -1"); List lists = ignite.cache("Person").query(sql).getAll(); for (CacheEntryImpl cacheEntryImpl : lists) { Person aa = (Person)cacheEntryImpl.getValue(); System.out.println(aa.getAge()); } }}
输出结果:
[11:36:39] Ignite node started OK (id=ce3e8b48)[11:36:39] Topology snapshot [ver=1, servers=1, clients=0, CPUs=4, heap=1.8GB]12
...