官方连接:见处理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引擎会在id
和salary
字段上创建索引:
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