首页 > 其他分享 >ignite系列之6-- 使用注解配置索引

ignite系列之6-- 使用注解配置索引

时间:2023-02-15 14:22:30浏览次数:42  
标签:ignite Person -- ccfg new 索引 SQL 注解

官方连接:见处理SQL-3.2.使用注解配置索引

https://www.ignite-service.cn/doc/java/WorkingwithSQL.html#_3-2-%E4%BD%BF%E7%94%A8%E6%B3%A8%E8%A7%A3%E9%85%8D%E7%BD%AE%E7%B4%A2%E5%BC%95

 索引和可查询字段,在代码上,可以通过@QuerySqlField注解进行配置。在下面的示例中,Ignite的SQL引擎会在idsalary字段上创建索引:

public class Person implements Serializable {
    /** Indexed field. Will be visible to the SQL engine. */
    @QuerySqlField(index = true)
    private long id;

    /** Queryable field. Will be visible to the SQL engine. */
    @QuerySqlField
    private String name;

    /** Will NOT be visible to the SQL engine. */
    private int age;

    /**
     * Indexed field sorted in descending order. Will be visible to the SQL engine.
     */
    @QuerySqlField(index = true, descending = true)
    private float salary;
}

SQL查询中,类型名会被用作表名,这时,表名为Person(使用的模式名和定义见模式章节的介绍)。
id和salary都是索引字段,id为生序排列,而salary为倒序排列。
如果不希望索引一个字段,但是希望在SQL查询中使用该列,那么该字段需要加上该注解,但是不需要index = true参数,这样的字段叫做可查询字段,在上例中,name定义为可查询字段。
age字段既不是可查询字段,也不是一个索引字段,因此在SQL查询中是无法访问的。
定义索引字段后,还需要注册索引类型。

注册索引类型示例:

定义索引和可查询字段之后,需要将它们及其所属的对象类型一起注册到SQL引擎中。

要指定应建立索引的类型,需要在CacheConfiguration.setIndexedTypes()方法中传递相应的键-值对,如下例所示:

// Preparing configuration.
CacheConfiguration<Long, Person> ccfg = new CacheConfiguration<>();

// Registering indexed type.
ccfg.setIndexedTypes(Long.class, Person.class);

测试代码示例:

EnvConfig envConfig = ConfigUtils.getEnvConfig();
        List<String> igniteAddress1 = envConfig.getIgniteAddress1();

        IgniteConfiguration cfg = new IgniteConfiguration();
        cfg.setClientMode(true);
        cfg.setPeerClassLoadingEnabled(true);
        TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
        ipFinder.setAddresses(igniteAddress1);
        cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder));

        UriDeploymentSpi uriDeploymentSpi = new UriDeploymentSpi();
        cfg.setDeploymentSpi(uriDeploymentSpi);
        Ignite ignite = Ignition.start(cfg);


        CacheConfiguration<Long, Person> ccfg = new CacheConfiguration<>();
//        ccfg.setIndexedTypes(Long.class, Person.class);
        String cacheName = Person.class.getSimpleName();
        ccfg.setName(cacheName);
        //1、修改配置后重新创建缓存
        ignite.destroyCache(cacheName);
        ccfg.setCacheMode(CacheMode.REPLICATED);
        //复制模式没有必要再设置副本
        ccfg.setBackups(0);
        ccfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);
        ccfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
        ccfg.setSqlSchema("PUBLIC");

        IgniteCache<Long, Person> cache = ignite.getOrCreateCache(ccfg);

        Person person = new Person();
        person.setId(1l);
        person.setName("xiaohua");
        person.setSalary(123.0f);
        cache.put(1l,person);

        Class.forName("org.apache.ignite.IgniteJdbcThinDriver");
        Connection conn = DriverManager.getConnection(envConfig.getJdbcUrl());
        Statement statement = conn.createStatement();
        ResultSet resultSet = statement.executeQuery("SELECT id,name from public.Person where name='xiaohua'");
        while (resultSet.next()){
            int anInt = resultSet.getInt("id");
            System.out.println(anInt);
        }

        if(null != statement){
            statement.close();
        }
        if(null != conn){
            conn.close();
        }
        ignite.close();

标签:ignite,Person,--,ccfg,new,索引,SQL,注解
From: https://www.cnblogs.com/yangh2016/p/17122626.html

相关文章

  • Python+Django(3):创建主页
    打开项目主文件夹learning_log中的文件urls.py:fromdjango.contribimportadminfromdjango.urlsimportpath,re_pathasurl,includeurlpatterns=[pat......
  • 18N20-ASEMI中低压N沟道MOS管18N20
    编辑:ll18N20-ASEMI中低压N沟道MOS管18N20型号:18N20品牌:ASEMI封装:ITO-252最大漏源电流:18A漏源击穿电压:200VRDS(ON)Max:0.18Ω引脚数量:3沟道类型:N沟道MOS管芯片尺寸:MI......
  • c++中nan,inf
    nan:notanumber非数字注意事项:对负数开方sqrt(-1.0)、对负数求对数(log(-1.0))、0.0/0.0、0.0*inf、inf/inf、inf-inf这些操作都会得到nan。(0/0会产生操作异常;0.0/0.0......
  • 自定义跨域中间件
    fromdjango.utils.deprecationimportMiddlewareMixin​​classCorsMiddleware(MiddlewareMixin):  """屏蔽CORS跨域"""​  defprocess_response(self,reques......
  • ignite系列之3--如何单机或者多机部署多集群
    如何单机或者多机部署多集群discoveryport为集群发现端口comminicationport为集群节点间通信端口,不同集群间配置不同端口,则组成不同集群配置示例:<propertyname="d......
  • 装备制造业数字化转型CRM系统解决方案(信息图)
    一、制造企业面临的机遇与挑战2021年12月28日,工业和信息化部等八部门联合对外发布《“十四五”智能制造发展规划》,明确提到“推进智能制造,要立足制造本质,紧扣智能特征,以工......
  • 将博客搬至CSDN
    尊敬的CSDN工作人员:您好!&nsbp;&nsbp;很开心看到CSDN可以搬家,我这边蠢蠢欲动地试试看吧,恭喜发财,谢谢谢谢!&nsbp;&nsbp;此致&nsbp;&nsbp;敬礼!......
  • ignite系列之4--jdbc端口xml配置
    jdbc端口xml配置<!--jdbc端口范围配置--><propertyname="clientConnectorConfiguration"><beanclass="org.apache.ignite.configuration.Clie......
  • openoffice 文件转化为pdf
    /**转换组件属性设置*/functionOpenOfficeMakePropertyValue($name,$value,$osm){$oStruct=$osm->Bridge_GetStruct("com.sun.star.beans.PropertyValu......
  • 数论笔记-同余
    目录同余带余数除法带余数除法的定义与基本性质模运算加速算法模运算封装龟速乘快速幂矩阵快速幂同余的定义与基本性质同余类与剩余系的定义与基本性质欧拉函数欧拉函数的......