0

We're working on a project and one of the things we need done is to show how many different locations a mac address has shown up in. We're using RedisGraph to query the information and I've used a hashmap to get the data. My problem is that I'll have a mac address listed in the json response multiple times, 3 of the times with the same location and one of the times with a different location. I want to get a JSON response that shows that mac address with a location count of 2, so it only counts distinct locations. Any help is much appreciated and below is my code.

        ResultSet resultSet = api.query(GET_COUNT_OF_LOCATIONS_FOR_DEVICE);
        JSONArray finalResult = new JSONArray();

        JSONArray jsonArray = new JSONArray();

        try {
            while (resultSet.hasNext()) {

                JSONObject joined = new JSONObject();
                Map<String, String> mac = new HashMap<String, String>();
                Map<String, String> location = new HashMap<String, String>();
                Map<String, String> location_count = new HashMap<String, String>();

                Record record = resultSet.next();
                mac.put("mac_address", record.getString("m.address"));
                location.put("location", record.getString("l.lo"));
                location_count.put("location_count", record.getString("l.lo"));

                joined.put("mac_address", mac.values().stream().map(i -> i).collect(Collectors.toSet()));
                joined.put("location", location.values().stream().map(i -> i).collect(Collectors.toSet()));
                joined.put("location_count", location.values().stream().map(i -> i).collect(Collectors.toSet()).size());


                jsonArray.put(joined);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        finalResult.put(jsonArray);
        return finalResult;

    }
Guy Korland
  • 9,139
  • 14
  • 59
  • 106
rreay724
  • 173
  • 1
  • 2
  • 9
  • Could you also include `GET_COUNT_OF_LOCATIONS_FOR_DEVICE`? – Itamar Haber Apr 16 '19 at 13:58
  • Can you show the response you get from the api? – NeplatnyUdaj Apr 16 '19 at 13:59
  • Sure, here is the query: public static final String GET_COUNT_OF_LOCATIONS_FOR_DEVICE = "MATCH (l:PJLOCO)-[:PJLOC_MAC]->(m:MAC) RETURN m.address , l.lo"; – rreay724 Apr 16 '19 at 14:14
  • Here is an example of part of the JSON response: [[{"location_count":1,"mac_address":["2a:93:fd:d7:dd:6b"],"location":["Fancy_Hotel"]},{"location_count":1,"mac_address":["c2:b6:c5:11:a2:bb"],"location":["IceCream_Parlor"]},{"location_count":1,"mac_address":["24:de:c6:52:9c:72"],"location":["Fancy_Hotel"]} – rreay724 Apr 16 '19 at 14:15

0 Answers0