Search in sources :

Example 1 with CityKey

use of data.CityKey in project microservices by pwillhan.

the class AverageCityPopulationCallableExample method main.

public static void main(String[] args) throws Exception {
    Config conf = new Config();
    MapConfig citiesConf = conf.getMapConfig("cities");
    citiesConf.addMapIndexConfig(new MapIndexConfig("country", false));
    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", 7322403, 2001));
        cities.put(new CityKey("Southampton", "GB"), new City("Southampton", "GB", 226698, 2006));
        cities.put(new CityKey("Plymouth", "GB"), new City("Plymouth", "GB", 244037, 2004));
        cities.put(new CityKey("York", "GB"), new City("York", "GB", 195070, 2010));
        cities.put(new CityKey("Paris", "FR"), new City("Paris", "FR", 2268265, 2013));
    }
    ExecutorService exec = hz.getExecutorService("exec");
    Future<Integer> avgTask = exec.submit(new AverageCityPopulationCallable("GB"));
    Integer avgPop = avgTask.get();
    System.err.println("Average GB city population: " + avgPop);
}
Also used : AverageCityPopulationCallable(tasks.AverageCityPopulationCallable) MapIndexConfig(com.hazelcast.config.MapIndexConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapConfig(com.hazelcast.config.MapConfig) Config(com.hazelcast.config.Config) MapIndexConfig(com.hazelcast.config.MapIndexConfig) ExecutorService(java.util.concurrent.ExecutorService) MapConfig(com.hazelcast.config.MapConfig) City(data.City) CityKey(data.CityKey)

Example 2 with CityKey

use of data.CityKey in project microservices by pwillhan.

the class CityGrowthExample method main.

public static void main(String[] args) throws Exception {
    Config conf = new Config();
    HazelcastInstance hz = Hazelcast.newHazelcastInstance(conf);
    MapConfig citiesConf = conf.getMapConfig("cities");
    citiesConf.addMapIndexConfig(new MapIndexConfig("country", false));
    IMap<CityKey, City> cities = hz.getMap("cities");
    if (cities.isEmpty()) {
        cities.put(new CityKey("London", "GB"), new City("London", "GB", 7322403, 2001));
        cities.put(new CityKey("Southampton", "GB"), new City("Southampton", "GB", 226698, 2006));
        cities.put(new CityKey("Plymouth", "GB"), new City("Plymouth", "GB", 244037, 2004));
        cities.put(new CityKey("York", "GB"), new City("York", "GB", 195070, 2010));
        cities.put(new CityKey("Paris", "FR"), new City("Paris", "FR", 2268265, 2013));
    }
    cities.executeOnEntries(new CityGrowthEntryProcessor(0.01), Predicates.equal("country", "GB"));
    for (City city : cities.values()) {
        System.err.println(city);
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapIndexConfig(com.hazelcast.config.MapIndexConfig) MapConfig(com.hazelcast.config.MapConfig) Config(com.hazelcast.config.Config) MapIndexConfig(com.hazelcast.config.MapIndexConfig) MapConfig(com.hazelcast.config.MapConfig) City(data.City) CityKey(data.CityKey) CityGrowthEntryProcessor(tasks.CityGrowthEntryProcessor)

Example 3 with CityKey

use of data.CityKey in project microservices by pwillhan.

the class AverageCityPopulationCallable method call.

@Override
public Integer call() throws Exception {
    System.err.println("Running task on: " + hz.getCluster().getLocalMember().toString());
    IMap<CityKey, City> cities = hz.getMap("cities");
    Predicate countryCityPredicate = Predicates.equal("country", country);
    Collection<City> countryCities = cities.values(countryCityPredicate);
    int totalPopulation = 0;
    for (City countryCity : countryCities) {
        totalPopulation += countryCity.getPopulation();
    }
    return totalPopulation / countryCities.size();
}
Also used : City(data.City) CityKey(data.CityKey) Predicate(com.hazelcast.query.Predicate)

Example 4 with CityKey

use of data.CityKey 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)

Example 5 with CityKey

use of data.CityKey in project microservices by pwillhan.

the class MapReduceCountryCityDensity 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));
    }
    JobTracker jobTracker = hz.getJobTracker("default");
    KeyValueSource<CityKey, City> sourceData = KeyValueSource.fromMap(cities);
    Job<CityKey, City> job = jobTracker.newJob(sourceData);
    JobCompletableFuture<Map<String, Integer>> future = job.mapper(new CountryCityDensityMapper()).reducer(new IntegerAvgReducerFactory()).submit();
    Map<String, Integer> result = future.get();
    System.err.println(result);
}
Also used : Config(com.hazelcast.config.Config) JobTracker(com.hazelcast.mapreduce.JobTracker) IntegerAvgReducerFactory(mapreduce.reducer.IntegerAvgReducerFactory) City(data.City) HazelcastInstance(com.hazelcast.core.HazelcastInstance) CityKey(data.CityKey) IMap(com.hazelcast.core.IMap) Map(java.util.Map) CountryCityDensityMapper(mapreduce.mapper.CountryCityDensityMapper)

Aggregations

City (data.City)6 CityKey (data.CityKey)6 Config (com.hazelcast.config.Config)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)5 IMap (com.hazelcast.core.IMap)3 Map (java.util.Map)3 MapConfig (com.hazelcast.config.MapConfig)2 MapIndexConfig (com.hazelcast.config.MapIndexConfig)2 Predicate (com.hazelcast.query.Predicate)2 Hazelcast (com.hazelcast.core.Hazelcast)1 JobTracker (com.hazelcast.mapreduce.JobTracker)1 Aggregations (com.hazelcast.mapreduce.aggregation.Aggregations)1 PropertyExtractor (com.hazelcast.mapreduce.aggregation.PropertyExtractor)1 Supplier (com.hazelcast.mapreduce.aggregation.Supplier)1 ExecutorService (java.util.concurrent.ExecutorService)1 IntegerSumCombinerFactory (mapreduce.combiner.IntegerSumCombinerFactory)1 CountryCityDensityMapper (mapreduce.mapper.CountryCityDensityMapper)1 CountryCityPopulationMapper (mapreduce.mapper.CountryCityPopulationMapper)1 IntegerAvgReducerFactory (mapreduce.reducer.IntegerAvgReducerFactory)1 IntegerSumReducerFactory (mapreduce.reducer.IntegerSumReducerFactory)1