1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| public class SensorDataService { private Session session; private PreparedStatement insertStatement; private PreparedStatement queryStatement; public SensorDataService(Session session) { this.session = session; prepareStatements(); } private void prepareStatements() { String insertQuery = "INSERT INTO sensor_data " + "(sensor_id, year, month, day, hour, timestamp, temperature, humidity, pressure, location) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) USING TTL ?"; insertStatement = session.prepare(insertQuery); String queryRangeQuery = "SELECT * FROM sensor_data " + "WHERE sensor_id = ? AND year = ? AND month = ? AND day = ? " + "AND hour >= ? AND hour <= ? ORDER BY hour ASC, timestamp ASC"; queryStatement = session.prepare(queryRangeQuery); } public void insertSensorData(String sensorId, Date timestamp, double temperature, double humidity, double pressure, Map<String, Double> location) { Calendar cal = Calendar.getInstance(); cal.setTime(timestamp); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1; int day = cal.get(Calendar.DAY_OF_MONTH); int hour = cal.get(Calendar.HOUR_OF_DAY); int ttl = 30 * 24 * 60 * 60; BoundStatement bound = insertStatement.bind( sensorId, year, month, day, hour, timestamp, temperature, humidity, pressure, location, ttl ); session.executeAsync(bound); } public void batchInsertSensorData(List<SensorReading> readings) { BatchStatement batch = new BatchStatement(BatchStatement.Type.UNLOGGED); for (SensorReading reading : readings) { Calendar cal = Calendar.getInstance(); cal.setTime(reading.getTimestamp()); BoundStatement bound = insertStatement.bind( reading.getSensorId(), cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY), reading.getTimestamp(), reading.getTemperature(), reading.getHumidity(), reading.getPressure(), reading.getLocation(), 30 * 24 * 60 * 60 ); batch.add(bound); if (batch.size() >= 100) { session.execute(batch); batch = new BatchStatement(BatchStatement.Type.UNLOGGED); } } if (batch.size() > 0) { session.execute(batch); } } public List<SensorReading> getSensorDataRange(String sensorId, Date startDate, Date endDate) { List<SensorReading> results = new ArrayList<>(); Calendar start = Calendar.getInstance(); start.setTime(startDate); Calendar end = Calendar.getInstance(); end.setTime(endDate); while (!start.after(end)) { int year = start.get(Calendar.YEAR); int month = start.get(Calendar.MONTH) + 1; int day = start.get(Calendar.DAY_OF_MONTH); BoundStatement bound = queryStatement.bind(sensorId, year, month, day, 0, 23); ResultSet resultSet = session.execute(bound); for (Row row : resultSet) { SensorReading reading = new SensorReading(); reading.setSensorId(row.getString("sensor_id")); reading.setTimestamp(row.getTimestamp("timestamp")); reading.setTemperature(row.getDouble("temperature")); reading.setHumidity(row.getDouble("humidity")); reading.setPressure(row.getDouble("pressure")); reading.setLocation(row.getMap("location", String.class, Double.class)); results.add(reading); } start.add(Calendar.DAY_OF_MONTH, 1); } return results; } public void aggregateHourlyData(String sensorId, Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); String query = "SELECT hour, temperature, humidity FROM sensor_data " + "WHERE sensor_id = ? AND year = ? AND month = ? AND day = ?"; PreparedStatement stmt = session.prepare(query); BoundStatement bound = stmt.bind( sensorId, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH) ); ResultSet resultSet = session.execute(bound); Map<Integer, List<Double>> temperatureByHour = new HashMap<>(); Map<Integer, List<Double>> humidityByHour = new HashMap<>(); for (Row row : resultSet) { int hour = row.getInt("hour"); double temp = row.getDouble("temperature"); double hum = row.getDouble("humidity"); temperatureByHour.computeIfAbsent(hour, k -> new ArrayList<>()).add(temp); humidityByHour.computeIfAbsent(hour, k -> new ArrayList<>()).add(hum); } String insertAggQuery = "INSERT INTO sensor_data_hourly " + "(sensor_id, date, hour, avg_temperature, max_temperature, min_temperature, avg_humidity, sample_count) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; PreparedStatement aggStmt = session.prepare(insertAggQuery); for (int hour = 0; hour < 24; hour++) { List<Double> temps = temperatureByHour.get(hour); List<Double> hums = humidityByHour.get(hour); if (temps != null && !temps.isEmpty()) { double avgTemp = temps.stream().mapToDouble(Double::doubleValue).average().orElse(0.0); double maxTemp = temps.stream().mapToDouble(Double::doubleValue).max().orElse(0.0); double minTemp = temps.stream().mapToDouble(Double::doubleValue).min().orElse(0.0); double avgHum = hums.stream().mapToDouble(Double::doubleValue).average().orElse(0.0); BoundStatement aggBound = aggStmt.bind( sensorId, date, hour, avgTemp, maxTemp, minTemp, avgHum, temps.size() ); session.execute(aggBound); } } } }
|