Skip to content
ajermakovics edited this page Nov 15, 2014 · 7 revisions

Welcome to the lambda2sql wiki!

Example using Hazelcast distributed map.

Here we build and run a distributed query using a lambda. The query is checked at compile time and it's quite readable as well!

    public static void main( String[] args )
    {
        Lambda2Sql.init();

        HazelcastInstance hz = Hazelcast.newHazelcastInstance();
        IMap<Integer, Employee> map = hz.getMap("employees");
        putData(map);


        Collection<Employee> vals;
        // run the query
        vals = values(map, e -> e.isActive() && e.getAge() > 30 );

        System.out.println(vals.size());
    }

    static <T> Collection<T> values(IMap<?, T> map, java.util.function.Predicate<T> p) {
        return map.values( toSqlPred(p) );
    }

    static <T> Predicate<?, T> toSqlPred(java.util.function.Predicate<T> p) {
        return new SqlPredicate( Lambda2Sql.toSql( p ) );
    }

	static void putData(IMap<Integer, Employee> map) {
		map.put(1, new Employee("alice", 30, true, 100));
        map.put(2, new Employee("bob", 40, true, 200));
	}
Clone this wiki locally