PDA

View Full Version : A first look at linq4j



jhyde
04-24-2012, 04:50 AM
This is a sneak peek of an exciting new data management technology. linq4j (short for "Language-Integrated Query for Java") is inspired by Microsoft's LINQ technology (http://msdn.microsoft.com/en-us/library/bb308959.aspx), previously only available on the .NET platform, and adapted for Java. (It also builds upon ideas I had in my earlier Saffron project (http://saffron.sourceforge.net/overview.html).)



I launched the linq4j project (https://github.com/julianhyde/linq4j/commit/ab31757589ed2423abe6a9454edafb5e77e69ffd) less than a week ago, but already you can do select, filter, join and groupBy operations on in-memory and SQL data.

In this demo, I write and execute sample code against the working system, and explain the differences between the key interfaces Iterable (http://docs.oracle.com/javase/7/docs/api/java/lang/Iterable.html), Enumerable (http://www.hydromatic.net/linq4j/apidocs/net/hydromatic/linq4j/Enumerable.html), and Queryable (http://www.hydromatic.net/linq4j/apidocs/net/hydromatic/linq4j/Queryable.html).

For those of you who want to get a closer look at the real code, here's one of the queries shown in the demo:

DatabaseProvider provider =
new DatabaseProvider(Helper.MYSQL_DATA_SOURCE);
provider.emps
.where(
new Predicate1() {
public boolean apply(Employee v1) {
return v1.manager;
}
})
.join(
provider.depts,
new Function1() {
public Integer apply(Employee a0) {
return a0.deptno;
}
},
new Function1() {
public Integer apply(Department a0) {
return a0.deptno;
}
},
new Function2() {
public String apply(Employee v1,
Department v2) {
return v1.name + " works in " + v2.name;
}
}
)
.foreach(
new Function1() {
public Void apply(String a0) {
System.out.println(a0);
return null;
}
}
); and here is its (not yet implemented) sugared syntax:
List strings =
from emp in provider.emps,
join dept in provider.depts on emp.deptno == dept.deptno
where emp.manager
orderBy emp.name
select emp.name + " works in " + dept.name;
For more information, visit the linq4j project's home page (https://github.com/julianhyde/linq4j).https://blogger.googleusercontent.com/tracker/5672165237896126100-6779529697171172010?l=julianhyde.blogspot.com


More... (http://julianhyde.blogspot.com/2012/04/first-look-at-linq4j.html)