List<Record> aggregateRecordsAndAddDistinct(List<Record> records, Field groupBy,
    Field distinctField, Field uniqueCountAssignedTo) {
    List<Record> result = new ArrayList<>();
    records.stream().collect(Collectors.groupingBy(r -> r.get(groupBy))).values().forEach(val -> {
      Set<FieldValue> distinctValues = new HashSet<>();
      Map<Field, FieldValue> map = new HashMap<>();
      for (Record record : val) {
        for (Map.Entry<Field, FieldValue> field : record.entrySet()) {
          if (distinctField.equals(field.getKey())) {
            // We will not be adding the field in the new record whose distinct count is required
            distinctValues.add(record.get(distinctField));
          } else {
            if (field.getKey().getFieldValueType() == FieldValueType.STRING) {
              map.put(field.getKey(), field.getValue());
            } else {
              if (map.get(field.getKey()) == null) {
                map.put(field.getKey(), field.getValue());
              } else {
                map.put(field.getKey(), map.get(field.getKey()).plus(field.getValue()));
              }
            }
          }
        }
      }
      // Add unique count field
      map.put(uniqueCountAssignedTo, uniqueCountAssignedTo.newValue(distinctValues.size()));
      result.add(
        Record.ofEntries(map.entrySet().stream().map(k -> Record.entry(k.getKey(), k.getValue()))));
    });
    return result;
  }
Aggregate the records and count the unique values for the given distinctField
@param records               records to be processed
@param groupBy               Field on which group by needs to be done
@param distinctField         Field whose unique values needs to be counted
@param uniqueCountAssignedTo a target field to which the unique count is assigned to
@return aggregated records
hbase-hbtop/src/main/java/org/apache/hadoop/hbase/hbtop/mode/ClientModeStrategy.java