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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
@Service public class GPSDataStorageService { @Autowired private GPSDataProcessingProperties properties; @Autowired private MongoTemplate mongoTemplate; @Autowired private RedisTemplate<String, Object> redisTemplate; @Autowired private GPSDataMonitorService gpsDataMonitorService; private static final Logger logger = LoggerFactory.getLogger(GPSDataStorageService.class);
public GPSDataStorageResult storeToMongoDB(GPSData gpsData) { logger.info("存储GPS数据到MongoDB,车辆ID: {}", gpsData.getVehicleId()); GPSDataStorageResult result = new GPSDataStorageResult(); result.setVehicleId(gpsData.getVehicleId()); result.setStorageType("MONGODB"); result.setStartTime(System.currentTimeMillis()); try { createIndexIfNotExists(); GPSData savedData = mongoTemplate.save(gpsData, properties.getMongoCollectionName()); result.setSuccess(true); result.setStoredData(savedData); result.setEndTime(System.currentTimeMillis()); gpsDataMonitorService.recordGPSDataStorage(gpsData.getVehicleId(), "MONGODB", true); logger.info("GPS数据存储到MongoDB成功,车辆ID: {}, 耗时: {}ms", gpsData.getVehicleId(), result.getDuration()); return result; } catch (Exception e) { logger.error("GPS数据存储到MongoDB失败,车辆ID: {}", gpsData.getVehicleId(), e); result.setSuccess(false); result.setError("GPS数据存储到MongoDB失败: " + e.getMessage()); result.setEndTime(System.currentTimeMillis()); gpsDataMonitorService.recordGPSDataStorage(gpsData.getVehicleId(), "MONGODB", false); return result; } }
public GPSDataBatchStorageResult batchStoreToMongoDB(List<GPSData> gpsDataList) { logger.info("批量存储GPS数据到MongoDB,数量: {}", gpsDataList.size()); GPSDataBatchStorageResult result = new GPSDataBatchStorageResult(); result.setTotalCount(gpsDataList.size()); result.setStorageType("MONGODB"); result.setStartTime(System.currentTimeMillis()); try { createIndexIfNotExists(); List<List<GPSData>> batches = partitionList(gpsDataList, properties.getMongoBatchInsertSize()); for (List<GPSData> batch : batches) { Collection<GPSData> savedData = mongoTemplate.insert(batch, properties.getMongoCollectionName()); result.addSuccessCount(savedData.size()); } result.setEndTime(System.currentTimeMillis()); gpsDataMonitorService.recordGPSDataBatchStorage(gpsDataList.size(), result.getSuccessCount()); logger.info("批量GPS数据存储到MongoDB完成,总数: {}, 成功: {}, 失败: {}, 耗时: {}ms", result.getTotalCount(), result.getSuccessCount(), result.getFailureCount(), result.getDuration()); return result; } catch (Exception e) { logger.error("批量GPS数据存储到MongoDB失败", e); result.setSuccess(false); result.setError("批量GPS数据存储到MongoDB失败: " + e.getMessage()); result.setEndTime(System.currentTimeMillis()); return result; } }
public GPSDataStorageResult storeToRedisCache(GPSData gpsData) { logger.info("存储GPS数据到Redis缓存,车辆ID: {}", gpsData.getVehicleId()); GPSDataStorageResult result = new GPSDataStorageResult(); result.setVehicleId(gpsData.getVehicleId()); result.setStorageType("REDIS"); result.setStartTime(System.currentTimeMillis()); try { String key = "gps:vehicle:" + gpsData.getVehicleId(); redisTemplate.opsForValue().set(key, gpsData, Duration.ofSeconds(properties.getRedisCacheExpireSeconds())); result.setSuccess(true); result.setEndTime(System.currentTimeMillis()); gpsDataMonitorService.recordGPSDataStorage(gpsData.getVehicleId(), "REDIS", true); logger.info("GPS数据存储到Redis缓存成功,车辆ID: {}, 耗时: {}ms", gpsData.getVehicleId(), result.getDuration()); return result; } catch (Exception e) { logger.error("GPS数据存储到Redis缓存失败,车辆ID: {}", gpsData.getVehicleId(), e); result.setSuccess(false); result.setError("GPS数据存储到Redis缓存失败: " + e.getMessage()); result.setEndTime(System.currentTimeMillis()); gpsDataMonitorService.recordGPSDataStorage(gpsData.getVehicleId(), "REDIS", false); return result; } }
public GPSData getFromRedisCache(String vehicleId) { try { String key = "gps:vehicle:" + vehicleId; return (GPSData) redisTemplate.opsForValue().get(key); } catch (Exception e) { logger.error("从Redis缓存获取GPS数据失败,车辆ID: {}", vehicleId, e); return null; } }
public List<GPSData> queryFromMongoDB(String vehicleId, Long startTime, Long endTime) { try { Query query = new Query(); query.addCriteria(Criteria.where("vehicle_id").is(vehicleId)); if (startTime != null && endTime != null) { query.addCriteria(Criteria.where("gps_timestamp").gte(startTime).lte(endTime)); } query.with(Sort.by(Sort.Direction.ASC, "gps_timestamp")); return mongoTemplate.find(query, GPSData.class, properties.getMongoCollectionName()); } catch (Exception e) { logger.error("从MongoDB查询GPS数据失败,车辆ID: {}", vehicleId, e); return new ArrayList<>(); } }
private void createIndexIfNotExists() { try { Index vehicleIdIndex = new Index().on("vehicle_id", Sort.Direction.ASC); mongoTemplate.indexOps(properties.getMongoCollectionName()).ensureIndex(vehicleIdIndex); Index gpsTimestampIndex = new Index().on("gps_timestamp", Sort.Direction.ASC); mongoTemplate.indexOps(properties.getMongoCollectionName()).ensureIndex(gpsTimestampIndex); Index geoIndex = new Index().on("longitude", Sort.Direction.ASC).on("latitude", Sort.Direction.ASC); mongoTemplate.indexOps(properties.getMongoCollectionName()).ensureIndex(geoIndex); Index compoundIndex = new Index() .on("vehicle_id", Sort.Direction.ASC) .on("gps_timestamp", Sort.Direction.ASC); mongoTemplate.indexOps(properties.getMongoCollectionName()).ensureIndex(compoundIndex); } catch (Exception e) { logger.error("创建MongoDB索引失败", e); } }
private <T> List<List<T>> partitionList(List<T> list, int size) { List<List<T>> partitions = new ArrayList<>(); for (int i = 0; i < list.size(); i += size) { partitions.add(list.subList(i, Math.min(i + size, list.size()))); } return partitions; } }
|