西安做网站seo,dedecms中餐网站模板,wordpress最新文章,seo优化 搜 盈seo公司Lin4j简介
Linq4j是Apache Calcite项目中的一个模块#xff0c;它提供了类似于LINQ#xff08;Language-Integrated Query#xff09;的功能#xff0c;用于在Java中进行数据查询和操作。Linq4j可以将逻辑查询转换为物理查询#xff0c;支持对集合进行筛选、映射、分组等…Lin4j简介
Linq4j是Apache Calcite项目中的一个模块它提供了类似于LINQLanguage-Integrated Query的功能用于在Java中进行数据查询和操作。Linq4j可以将逻辑查询转换为物理查询支持对集合进行筛选、映射、分组等操作。 例如通过Linq4j可以将类似于以下的逻辑查询语句转换为最终的在java集合上的查询操作
EnumerableEmployee employees ...;
EnumerableString names employees.where(emp - emp.getSalary() 50000).select(emp - emp.getName());依赖
dependencygroupIdorg.apache.calcite/groupIdartifactIdcalcite-linq4j/artifactIdversion1.36.0/version
/dependencyLinq4j核心类与接口
Linq4jorg.apache.calcite.linq4j.Linq4j一个实用工具类可将 java.util.Collection、java.lang.Iterable、java.util.List等集合对象转换为 org.apache.calcite.linq4j.EnumerableEnumerable接口的职责是定义了对集合进行查询和操作的方法包括筛选、映射、分组等功能。接口继承了下列三个接口支持集合的迭代、投影、过滤等操作 RawEnumerable (org.apache.calcite.linq4j)返回org.apache.calcite.linq4j.Enumerator可对Enumerator实现进行迭代Iterable (java.lang)返回java.lang.Iterable可对Iterable的实现迭代ExtendedEnumerable (org.apache.calcite.linq4j)定义了在集合对象上的操作 例如 select 投影、where 过滤、groupBy 分组 、hashJoin 连接 因此通过Linq4j转换方法将输入集合转换为Enumerable这样就可在原始数据上进行操作。
使用案例
使用过滤、投影、分组
投影没有进行映射变换按原值输出过滤筛选出大于3的数最后进行分组分组后返回的是一个 Grouping 对象使用sum函数对分组求和最终输出求和后的结果 整个操作使用分组后返回的是Grouping对象因此后续操作基于此对象进行 ListInteger idList Lists.newArrayList(1,2,3,4,5,6);EnumerableInteger itEnumerable Linq4j.asEnumerable(idList);EnumerableGroupingBoolean, Integer result itEnumerable.select((a) - a).where((a) - a 3).groupBy(a - a%2 0);for (GroupingBoolean, Integer item: result) {int sum item.sum(new IntegerFunction1Integer() {Overridepublic int apply(Integer v0) {return v0;}});System.out.println(item.getKey() : sum);}通过一个案例可类推其它方法的使用。
Linq4j在Calcite中的应用
在使用Calcite实现适配多来源数据查询时需要实现自定义的table通常需要借助Linq4j提供的方法将集合转换为Enumerable实例转换过程中依赖asEnumerable方法创建Enumerable实例实例接着通过select进行对象类型转换。
public class TableForList extends AbstractTable implements ScannableTable{private PersonList personList;public TableForList(PersonList personList) {this.personList personList;}Overridepublic EnumerableObject[] scan(DataContext root) {return Linq4j.asEnumerable(personList.getPersonList()).select(emp - new Object[]{emp.getId(), emp.getName(), emp.getAge()});}