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);
}
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);
}
}
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();
}
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);
}
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);
}
Aggregations