@Datapublic class SearchParams<T> { // 查询时间范围开始时间 private String start; // 时间戳字段排序规则,true:降序 private Boolean sortRule = true; // 查询时间范围结束时间 private String end; // 时间间隔 private String every; // 筛选条件 private List<String> filterList; // map构造的目标类对象 private Class<? extends T> mapClazz;}功能类@Repository@Slf4jpublic class InfluxRepository { @Autowired private WriteApi writeApi; @Autowired private QueryApi queryApi; @Autowired private InfluxdbConfigProp influxdbConfigProp; /** * 向influx写入数据 * * @param data 写入数据实体 */ public <T> void writeData(T data) { writeApi.writeMeasurement( influxdbConfigProp.getBucket(), influxdbConfigProp.getOrg(), WritePrecision.MS, data); } /** * 查询数据 * * @param params 查询参数 */ public <T> List<FluxTable> findMonitoringData(SearchParams<T> params) { StringBuffer queryBuffer = new StringBuffer(); // BUCKET queryBuffer.append("from(bucket: \""); queryBuffer.append(influxdbConfigProp.getBucket()); // 时间范围条件 queryBuffer.append("\") \n|> range(start: "); queryBuffer.append(params.getStart()); queryBuffer.append(", stop: "); queryBuffer.append(params.getEnd()); queryBuffer.append(")\n"); List<String> filterList = params.getFilterList(); if (!CollectionUtils.isEmpty(filterList)) { queryBuffer.append(" |> filter(fn: (r) => "); // 拼接查询条件 for (int i = 0; i < filterList.size(); i++) { String[] filters = filterList.get(i).split(">"); queryBuffer.append("r[\""); queryBuffer.append(filters[0]); queryBuffer.append("\"]"); queryBuffer.append(filters[1]); if (i < filterList.size() - 1) queryBuffer.append(" and "); } queryBuffer.append(")\n"); } // aggregateWindow函数 queryBuffer.append(" |> aggregateWindow(every: "); queryBuffer.append(params.getEvery()); queryBuffer.append(",fn: first, createEmpty: true)\n"); // 为查询结果添加排序 queryBuffer.append(" |> sort(columns: [\"_time\"], desc: "); queryBuffer.append(params.getSortRule().booleanValue()); queryBuffer.append(")\n"); // map函数语句拼接 Class<? extends T> mapClazz = params.getMapClazz(); if (!ObjectUtils.isEmpty(mapClazz)) { queryBuffer.append(" |> map("); queryBuffer.append(" fn:(r) => { \n"); queryBuffer.append(" return {\n"); Field[] fields = mapClazz.getDeclaredFields(); // 目标实体字段和influx查询结果字段的映射 Map<String, String> fieldMap = new HashMap<>(); for (Field field : fields) { InfluxColumn influxColumn = field.getAnnotation(InfluxColumn.class); if (influxColumn != null) { fieldMap.put(field.getName(), influxColumn.value()); } } // 若有需要映射的字段则构建语句 if (!CollectionUtils.isEmpty(fieldMap)) { for (String key : fieldMap.keySet()) { queryBuffer.append(key); queryBuffer.append(": r[\""); queryBuffer.append(fieldMap.get(key)); queryBuffer.append("\"],\n"); } queryBuffer.append("}})\n"); } } String influxQl = queryBuffer.toString(); log.info("查询语句, {}", influxQl); List<FluxTable> queryData = queryApi.query(influxQl, influxdbConfigProp.getOrg()); return queryData; }}
推荐阅读
- Deepin系统navicat15安装
- 手把手教你玩转 Gitea|使用 Docker 安装 Gitea
- Docker搭建kafka及监控
- VMware安装Win11+WSA子系统和使用教程
- 大华海康NVR录像JAVA下载及WEB播放
- Linux make编译
- 为什么下载不了小红书 小红书为什么下载不了
- 三星GT-N7102救砖步骤
- 如何安装iis软件 如何安装IIS?
- 怎样安装iis组件 怎样安装IIS