ignite通过注解配置查询

本文通过一个具体的示例展示了如何使用 Apache Ignite 进行 SQL 查询。首先定义了带有 @QuerySqlField 注解的 Person 类,然后创建了一个 IgniteCache,并通过 SqlQuery 执行查询。最后,演示了如何获取并打印查询结果。

官方文档的叙述可能有些不清楚,我做了一个测试,并且可以成功运行,待会儿后面贴出小栗子.

  两步操作:

  第一步在属性值处贴上@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

...

转载于:https://www.cnblogs.com/garfieldcgf/p/5646374.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值