apache ignite Sql查询中基于列表字段的排序记录
假设下面有一个对象Employee,它存储在ignite缓存中apache ignite Sql查询中基于列表字段的排序记录,ignite,Ignite,假设下面有一个对象Employee,它存储在ignite缓存中 Employee{ int id; String name; List<Double> marksInorder;//marks for each subject in order //hypothetical situation, actual requirement is different } 员工{ int-id; 字符串名; 列出标记顺序;//按顺序列出每个主题的标记
Employee{
int id;
String name;
List<Double> marksInorder;//marks for each subject in order
//hypothetical situation, actual requirement is different
}
员工{
int-id;
字符串名;
列出标记顺序;//按顺序列出每个主题的标记
//假设情况下,实际需求是不同的
}
我正在使用SQLQuery从ignite缓存中获取记录,需要获取最大为marksInorder的前N个记录。但ignite不支持列表字段上的orderBy
是否有解决方法。我认为最简单的解决方法是添加
双maxmark
字段并将其配置为索引。我使用@QuerySqlFunction找到了解决方案。我的自定义SqlFunction将double列表转换为列表中的最大值,并最终按降序对其进行排序。观察输出。给你
public class Student implements Serializable {
@QuerySqlField(index = true)
private int Id;
@QuerySqlField(index = true)
private String name;
@QuerySqlField(index = true)
private List<Double> marks;
//rest of POJO code
}
public class MySqlFunctions {
@QuerySqlFunction
public static double maxOf(List<Double> marksInorder) {
Collections.sort(marksInorder, Collections.reverseOrder());
return marksInorder.get(0);
}
}
public static void main(String[] args) {
Ignite ignite = Ignition.start("examples/config/example-ignite.xml");
CacheConfiguration<Integer, Student> config = new CacheConfiguration<>("students");
config.setIndexedTypes(Integer.class, Student.class);
config.setSqlFunctionClasses(MySqlFunctions.class);
IgniteCache<Integer, Student> cache = ignite.getOrCreateCache(config);
List<Double> marks1 = new ArrayList<>();
List<Double> marks2 = new ArrayList<>();
List<Double> marks3 = new ArrayList<>();
marks1.add(12.3);
marks1.add(4.3);
marks2.add(1.3);
marks2.add(14.3);
marks3.add(13.3);
marks3.add(3.3);
Student s1 = new Student(1, "John", marks1);
Student s2 = new Student(2, "Dan", marks2);
Student s3 = new Student(3, "Ron", marks3);
cache.put(s1.getId(), s1);
cache.put(s2.getId(), s2);
cache.put(s3.getId(), s3);
String sql = "select * from Student order by maxOf(marks) desc";
SqlQuery<Integer, Student> query = new SqlQuery<>(Student.class, sql);
List<Student> result = cache.query(query).getAll().stream().map(e -> e.getValue()).collect(Collectors.toList());
System.out.println("Result : " + result);
}
Result : [Student [Id=2, name=Dan, marks=[1.3, 14.3]], Student [Id=3, name=Ron, marks=[13.3, 3.3]], Student [Id=1, name=John, marks=[12.3, 4.3]]]
公共类学生实现可序列化{
@QuerySqlField(index=true)
私有int-Id;
@QuerySqlField(index=true)
私有字符串名称;
@QuerySqlField(index=true)
私人名单标记;
//POJO代码的其余部分
}
公共类MySqlFunctions{
@QuerySqlFunction
公共静态双maxOf(列表标记顺序){
Collections.sort(marksInorder,Collections.reverseOrder());
返回marksInorder.get(0);
}
}
公共静态void main(字符串[]args){
Ignite Ignite=Ignition.start(“examples/config/example Ignite.xml”);
CacheConfiguration config=新的CacheConfiguration(“学生”);
config.setIndexedTypes(Integer.class、Student.class);
config.setSqlFunctionClasses(MySqlFunctions.class);
IgniteCache cache=ignite.getOrCreateCache(配置);
列表标记s1=新的ArrayList();
List marks2=new ArrayList();
List marks3=new ArrayList();
标记1.添加(12.3);
标记1.添加(4.3);
标记2.添加(1.3);
标记2.添加(14.3);
标记3.添加(13.3);
标记3.添加(3.3);
学生s1=新生(1,“约翰”,标记s1);
学生s2=新生(2个“丹”,标记s2);
学生s3=新生(3,“Ron”,标记s3);
cache.put(s1.getId(),s1);
cache.put(s2.getId(),s2);
cache.put(s3.getId(),s3);
String sql=“按maxOf(marks)desc从学生订单中选择*”;
SqlQuery=新的SqlQuery(Student.class,sql);
List result=cache.query(query.getAll().stream().map(e->e.getValue()).collect(Collectors.toList());
System.out.println(“结果:+Result”);
}
结果:[学生[Id=2,name=Dan,marks=[1.3,14.3]],学生[Id=3,name=Ron,marks=[13.3,3.3]],学生[Id=1,name=John,marks=[12.3,4.3]]
感谢Valentin的回复。我想我已经找到了解决办法。基本上,我们可以使用@QuerySqlFunction注释来定制ignite SQLQuery中的字段。我将在测试后不久在这里发布它