本文主要是介绍INFLUXDB-influxDB在Java中的简单操作,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在SENTINEL-sentinel实时监控持久化到InfluxDB中中已经有了用Java实现基本的操作,今天我们再来补充一下。主要是熟悉InfluxDB的语法和操作。
@Component
public class InfluxDBMetricExeRepository implements MetricsExeRepository {private static Logger logger = LoggerFactory.getLogger(InfluxDBMetricExeRepository.class);@Autowiredpublic InfluxDB influxDB;/*** @param app 应用的名称* @param searchKey 查询关键字的名称* @param pageIndex 查询第几页* @param pageSize 查询第几条数据* @return*/@Overridepublic List<String> listResourcesOfApp(String app, String searchKey, Integer pageIndex, Integer pageSize, long minTimeMs) {List<String> returnList = new ArrayList<>();if (StringUtil.isBlank(app)) {return returnList;}StringBuilder command = new StringBuilder();command.append("select * from ( ");command.append("select distinct(resourceName) from sentinelInfo where app= ");command.append("'");command.append(app);command.append("'");command.append(" and");command.append(" gmtCreate > ");command.append(minTimeMs);if (!StringUtil.isBlank(searchKey)) {command.append(" and resource =~/");command.append(searchKey);command.append("/");}command.append(" )");command.append(" limit ");command.append(pageSize);command.append(" offset ");command.append((pageIndex - 1) * pageSize);logger.info("firstPage---"+command.toString());Query query = new Query(command.toString());QueryResult queryResult = influxDB.query(query);if (queryResult.getResults()!=null&&queryResult.getResults().get(0)!=null&&queryResult.getResults().get(0).getSeries()!=null){List<List<Object>> result = queryResult.getResults().get(0).getSeries().get(0).getValues();for (List<Object> objects : result) {if (objects != null && objects.size() > 1) {objects.get(1);returnList.add(String.valueOf(objects.get(1)));}}}return returnList;}/*** @param app 应用的名称* @param searchKey 查询关键字的名称* @param minTimeMs* @return*/@Overridepublic Integer countListResourcesOfApp(String app, String searchKey, long minTimeMs) {StringBuilder command = new StringBuilder();command.append("select count(distinct(resourceName)) from sentinelInfo where app= ");command.append("'");command.append(app);command.append("'");command.append(" and ");command.append(" gmtCreate > ");command.append(minTimeMs);if (!StringUtil.isBlank(searchKey)) {command.append(" and resource =~/");command.append(searchKey);command.append("/");}logger.info("count---"+command.toString());Query query = new Query(command.toString());QueryResult queryResult = influxDB.query(query);if (queryResult.getResults()!=null&&queryResult.getResults().get(0)!=null&&queryResult.getResults().get(0).getSeries()!=null){List<List<Object>> result = queryResult.getResults().get(0).getSeries().get(0).getValues();for (List<Object> objects : result) {if (objects != null && objects.size() > 1) {return new Double(String.valueOf(objects.get(1))).intValue();}}}return 1;}/*** 查询一段时间内的数据* influxDB不支持in的语法,所以直接用的or* @param app 应用的名称* @param resources 资源的名称* @param startTime 查询的开始时间* @param endTime 查询的结束时间* @return*/@Overridepublic List<MetricEntity> queryInfoBetween(String app, List<String> resources, long startTime, long endTime) {List<MetricEntity> results = new ArrayList<>();if (StringUtil.isBlank(app)) {return results;}StringBuilder command = new StringBuilder();command.append("select * from sentinelInfo where app= ");command.append("'");command.append(app);command.append("'");command.append("and ");command.append("( ");int size=0;for (String resource:resources){command.append(" resource=");command.append("'");command.append(resource);command.append("'");size=size+1;if (size<resources.size()){command.append(" or ");}}command.append(") ");command.append(" AND gmtCreate>"+startTime+" AND gmtCreate<"+endTime);Query query = new Query(command.toString());logger.info("entity---"+command.toString());QueryResult queryResult = influxDB.query(query);InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();List<InfluxDBMetricEntity> influxResults = resultMapper.toPOJO(queryResult, InfluxDBMetricEntity.class);try {influxResults.forEach(entity->{MetricEntity metric = new MetricEntity();BeanUtils.copyProperties(entity,metric);results.add(metric);});} catch (Exception e) {e.printStackTrace();}return results;}
}
这篇关于INFLUXDB-influxDB在Java中的简单操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!