Search in sources :

Example 1 with PropertyExtractor

use of com.hazelcast.mapreduce.aggregation.PropertyExtractor in project microservices by pwillhan.

the class AggregationCountryCityPopulation method main.

public static void main(String[] args) throws Exception {
    Config conf = new Config();
    HazelcastInstance hz = Hazelcast.newHazelcastInstance(conf);
    IMap<CityKey, City> cities = hz.getMap("cities");
    if (cities.isEmpty()) {
        cities.put(new CityKey("London", "GB"), new City("London", "GB", 8416535, 1572));
        cities.put(new CityKey("Southampton", "GB"), new City("Southampton", "GB", 242100, 51));
        cities.put(new CityKey("Chicago", "US"), new City("Chicago", "US", 2718782, 606));
        cities.put(new CityKey("Washington DC", "US"), new City("Washington DC", "US", 658893, 177));
        cities.put(new CityKey("Seattle", "US"), new City("Seattle", "US", 652405, 370));
    }
    Supplier<CityKey, City, Integer> gbCityPopulation6 = Supplier.fromPredicate(new Predicate<CityKey, City>() {

        @Override
        public boolean apply(Map.Entry<CityKey, City> entry) {
            return "GB".equals(entry.getValue().getCountry());
        }
    }, Supplier.<CityKey, City, Integer>all(new PropertyExtractor<City, Integer>() {

        @Override
        public Integer extract(City city) {
            return city.getPopulation();
        }
    }));
    int result6 = cities.aggregate(gbCityPopulation6, Aggregations.<CityKey, City>integerSum());
    System.err.println(result6);
    Supplier<CityKey, City, Integer> gbCityPopulation8 = Supplier.fromPredicate(entry -> "GB".equals(entry.getValue().getCountry()), Supplier.all(city -> city.getPopulation()));
    int result8 = cities.aggregate(gbCityPopulation8, Aggregations.integerSum());
    System.err.println(result8);
}
Also used : CityKey(data.CityKey) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) City(data.City) Supplier(com.hazelcast.mapreduce.aggregation.Supplier) IMap(com.hazelcast.core.IMap) Hazelcast(com.hazelcast.core.Hazelcast) PropertyExtractor(com.hazelcast.mapreduce.aggregation.PropertyExtractor) Aggregations(com.hazelcast.mapreduce.aggregation.Aggregations) Map(java.util.Map) Predicate(com.hazelcast.query.Predicate) Config(com.hazelcast.config.Config) City(data.City) HazelcastInstance(com.hazelcast.core.HazelcastInstance) PropertyExtractor(com.hazelcast.mapreduce.aggregation.PropertyExtractor) CityKey(data.CityKey) IMap(com.hazelcast.core.IMap) Map(java.util.Map)

Aggregations

Config (com.hazelcast.config.Config)1 Hazelcast (com.hazelcast.core.Hazelcast)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 IMap (com.hazelcast.core.IMap)1 Aggregations (com.hazelcast.mapreduce.aggregation.Aggregations)1 PropertyExtractor (com.hazelcast.mapreduce.aggregation.PropertyExtractor)1 Supplier (com.hazelcast.mapreduce.aggregation.Supplier)1 Predicate (com.hazelcast.query.Predicate)1 City (data.City)1 CityKey (data.CityKey)1 Map (java.util.Map)1