ignite通过注解配置查询
Posted 但行好事 莫问前程
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ignite通过注解配置查询相关的知识,希望对你有一定的参考价值。
官方文档的叙述可能有些不清楚,我做了一个测试,并且可以成功运行,待会儿后面贴出小栗子.
两步操作:
第一步在属性值处贴上@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"); CacheConfiguration<Integer, Person> cfg = new CacheConfiguration<Integer, Person>(); cfg.setName("Person"); cfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL); cfg.setIndexedTypes(Integer.class, Person.class); ignite.getOrCreateCache(cfg); IgniteCache<Integer, Person> 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<CacheEntryImpl> 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
...
以上是关于ignite通过注解配置查询的主要内容,如果未能解决你的问题,请参考以下文章
apache ignite系列:使用ddl和dml脚本初始化ignite并使用mybatis查询缓存
基于Ignite+Lucene+Log4j2的分布式统一日志查询最佳实践
尝试使用DBeaver同时执行多个查询时,无法与Ignite群集通信