一个jvm层级的仿DataFrame工具
准备工作
<dependency>
<groupId>io.github.burukeyou</groupId>
<artifactId>jdframe</artifactId>
<version>0.0.2</version>
</dependency>
一、案例1.小试牛刀
新建学生实体
package com.example.demo.dao;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private int id;
private String name;
private String school;
private String level;
private Integer age;
private BigDecimal score;
private Integer rank;
public Student(String level, BigDecimal score) {
this.level = level;
this.score = score;
}
public Student(int id, String name, String school, String level, Integer age, BigDecimal score) {
this.id = id;
this.name = name;
this.school = school;
this.level = level;
this.age = age;
this.score = score;
}
}
统计每个学校的里学生年龄不为空并且年龄在9到16岁间的合计分数,并且获取合计分前2名的学校
static List<Student> studentList = new ArrayList<>();
static {
studentList.add(new Student(1,"a","一中","一年级",11, new BigDecimal(1)));
studentList.add(new Student(2,"a","一中","一年级",11, new BigDecimal(1)));
studentList.add(new Student(3,"b","一中","三年级",12, new BigDecimal(2)));
studentList.add(new Student(4,"c","二中","一年级",13, new BigDecimal(3)));
studentList.add(new Student(5,"d","二中","一年级",14, new BigDecimal(4)));
studentList.add(new Student(6,"e","三中","二年级",14, new BigDecimal(5)));
studentList.add(new Student(7,"e","三中","二年级",15, new BigDecimal(5)));
}
public static void main(String[] args) {
// 等价于SQL:
// select school,sum(score)
// from students
// where age is not null and age >=9 and age <= 16
// group by school
// order by sum(score) desc
// limit 2
SDFrame<FI2<String, BigDecimal>> sdf2 = SDFrame.read(studentList)
.whereNotNull(Student::getAge)
.whereBetween(Student::getAge,9,16)
.groupBySum(Student::getSchool, Student::getScore)
.sortDesc(FI2::getC2)
.cutFirst(2);
sdf2.show();
}
控制台输出信息
c1 c2
三中 10
二中 7