private static void countEmployees(IMap<String, Employee> employees) { // select all employees Supplier<String, Employee, Object> supplier = Supplier.all(); // choose the aggregation to perform Aggregation<String, Object, Long> aggregation = Aggregations.count(); // execute the aggregation and print the result long countEmployee = employees.aggregate(supplier, aggregation); System.out.println("Number of employees: " + countEmployee); System.out.println("\n"); // in Java 8: //long countEmployees = employees.aggregate(Supplier.all(), Aggregations.count()); }
private static void salarySum(IMap<String, Employee> employees) { // create the PropertyExtractor to extract salary value from the employee PropertyExtractor<Employee, Integer> propertyExtractor = new SalaryPropertyExtractor(); // select all employees Supplier<String, Employee, Integer> supplier = Supplier.all(propertyExtractor); // choose the aggregation to perform Aggregation<String, Integer, Integer> aggregation = Aggregations.integerSum(); // execute the aggregation and print the result int sumSalary = employees.aggregate(supplier, aggregation); System.out.println("Sum of all salaries: " + sumSalary); System.out.println("\n"); // in Java 8: //int sumSalary = employees.aggregate(Supplier.all((value) -> value.getSalaryPerMonth()), Aggregations.integerSum()); }
private static void simpleSalaryAverage(IMap<String, Employee> employees) { System.out.println("Calculating salary average"); // create the PropertyExtractor to extract salary value from the employee PropertyExtractor<Employee, Integer> propertyExtractor = new SalaryPropertyExtractor(); // select all employees Supplier<String, Employee, Integer> supplier = Supplier.all(propertyExtractor); // choose the aggregation to perform Aggregation<String, Integer, Integer> aggregation = Aggregations.integerAvg(); // execute the aggregation and print the result int avgSalary = employees.aggregate(supplier, aggregation); System.out.println("Overall average salary: " + avgSalary); System.out.println("\n"); // in Java 8: //int avgSalary = employees.aggregate(Supplier.all((value) -> value.getSalaryPerMonth()), Aggregations.integerAvg()); }
private static void distinctEmployeeFirstNames(IMap<String, Employee> employees) { // create PropertyExtractor to extract firstName value from the employee PropertyExtractor<Employee, String> propertyExtractor = new FirstNamePropertyExtractor(); // select all employees Supplier<String, Employee, String> supplier = Supplier.all(propertyExtractor); // choose the aggregation to perform Aggregation<String, String, Set<String>> aggregation = Aggregations.distinctValues(); // execute the aggregation and print the result Set<String> allFirstNames = employees.aggregate(supplier, aggregation); System.out.println("All first names: " + allFirstNames); System.out.println("\n"); // in Java 8: //Set<String> allFirstNames = employees.aggregate(Supplier.all((value) -> value.getFirstName()), // Aggregations.distinctValues()); }
@Override public void execute(HazelcastInstance hazelcastInstance) throws Exception { IMap<String, SalaryYear> map = hazelcastInstance.getMap("salaries"); Supplier<String, SalaryYear, Integer> supplier = Supplier.all(new SalaryPropertyExtractor()); Aggregation<String, Integer, Integer> aggregation = Aggregations.integerSum(); int sum = map.aggregate(supplier, aggregation); System.out.println("Salary sum: " + sum); }
private static void companyBasedSalaryAverage(IMap<String, Employee> employees) { System.out.println("Calculating average monthly salary for Hazelcast"); // create the Predicate to select only Hazelcast employees Predicate<String, Employee> companyPredicate = new CompanyPredicate("Hazelcast"); // create the PropertyExtractor to extract salary value from the employee PropertyExtractor<Employee, Integer> propertyExtractor = new SalaryPropertyExtractor(); // create the supplier to handle extracted salaries from selected employees Supplier<String, Employee, Integer> salaryExtractor = Supplier.all(propertyExtractor); // merge Predicate and PropertyExtractor (through the previous Supplier) together Supplier<String, Employee, Integer> supplier = Supplier.fromPredicate(companyPredicate, salaryExtractor); // choose the aggregation to perform Aggregation<String, Integer, Integer> aggregation = Aggregations.integerAvg(); // execute the aggregation and print the result int avgSalary = employees.aggregate(supplier, aggregation); System.out.println("Hazelcast average salary: " + avgSalary); System.out.println("\n"); // in Java 8: //int avgSalary = employees.aggregate( // Supplier.fromPredicate( // (entry) -> "Hazelcast".equals(mapEntry.getValue().getCompanyName(), // Supplier.all((value) -> value.getSalaryPerMonth()) // ), Aggregations.integerAvg()); }