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()); }
@Override public ValueOut apply(Map.Entry<KeyIn, ValueIn> entry) { if (keyPredicate.evaluate(entry.getKey())) { ValueIn value = entry.getValue(); return chainedSupplier != null ? chainedSupplier.apply(entry) : (ValueOut) value; } return null; }
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()); }
@Override public ValueOut apply(Map.Entry<KeyIn, ValueIn> entry) { if (predicate.apply(entry)) { ValueIn value = entry.getValue(); if (value != null) { return chainedSupplier != null ? chainedSupplier.apply(entry) : (ValueOut) value; } } return null; }
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()); }
@Override public ValueOut apply(Map.Entry<KeyIn, ValueIn> entry) { if (keyPredicate.evaluate(entry.getKey())) { ValueIn value = entry.getValue(); return chainedSupplier != null ? chainedSupplier.apply(entry) : (ValueOut) value; } return null; }
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()); }
@Override public ValueOut apply(Map.Entry<KeyIn, ValueIn> entry) { if (predicate.apply(entry)) { ValueIn value = entry.getValue(); if (value != null) { return chainedSupplier != null ? chainedSupplier.apply(entry) : (ValueOut) value; } } return null; }
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 map(Key key, ValueIn value, Context<Key, ValueOut> context) { entry.setKey(key); entry.setValue(value); entry.setSerializationService(((DefaultContext) context).getSerializationService()); ValueOut valueOut = supplier.apply(entry); if (valueOut != null) { context.emit(key, valueOut); } }
@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); }
@Override public void map(Key key, Value value, Context<Integer, DistinctType> context) { int mappingKey = key(); entry.setKey(key); entry.setValue(value); entry.setSerializationService(((DefaultContext) context).getSerializationService()); DistinctType valueOut = supplier.apply(entry); if (valueOut != null) { context.emit(mappingKey, valueOut); } }
@Override public void map(Key key, ValueIn value, Context<Key, ValueOut> context) { entry.setKey(key); entry.setValue(value); entry.setSerializationService(((DefaultContext) context).getSerializationService()); entry.setExtractors(Extractors.newBuilder(((DefaultContext) context).getSerializationService()).build()); ValueOut valueOut = supplier.apply(entry); if (valueOut != null) { context.emit(key, valueOut); } }
@Override public void map(Key key, Value value, Context<Integer, DistinctType> context) { int mappingKey = key(); entry.setKey(key); entry.setValue(value); entry.setSerializationService(((DefaultContext) context).getSerializationService()); entry.setExtractors(Extractors.newBuilder(((DefaultContext) context).getSerializationService()).build()); DistinctType valueOut = supplier.apply(entry); if (valueOut != null) { context.emit(mappingKey, valueOut); } }