infuxDB官网:https://docs.influxdata.com/influxdb/v1.7/introduction/getting-started/
InfluxDB中文文档:https://jasper-zhang1.gitbooks.io/influxdb/content/
infuxDB v2 官网:https://v2.docs.influxdata.com/v2.0/reference/api/client-libraries/
influxdb基本SQL操作:https://blog.csdn.net/gongpulin/article/details/81103515
<dependency> <groupId>org.influxdb</groupId> <artifactId>influxdb-java</artifactId> <version>2.10</version> </dependency>
import okhttp3.OkHttpClient; import org.influxdb.InfluxDB; import org.influxdb.InfluxDBFactory; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.concurrent.TimeUnit; /** * @author chong.wang@ximalaya.com * @date 2019/11/28 上午9:59 */ public class InfluxDBTemple { private static String USERNAME; private static String PASSWORD; private static String URL; private static String DATABASE; private static InfluxDB influxDB; static { InputStream inputStream = InfluxDBTemple.class.getResourceAsStream("/influxdb.properties"); Properties properties = new Properties(); try { properties.load(inputStream); USERNAME = properties.getProperty("influxdb.username"); PASSWORD = properties.getProperty("influxdb.password"); URL = properties.getProperty("influxdb.url"); DATABASE = properties.getProperty("influxdb.database"); } catch (IOException e) { e.printStackTrace(); } } public static InfluxDB getInfluxDbConnection() { OkHttpClient.Builder client = new OkHttpClient.Builder() .connectTimeout(1, TimeUnit.MINUTES) .readTimeout(2, TimeUnit.MINUTES) .writeTimeout(2, TimeUnit.MINUTES) .retryOnConnectionFailure(true); influxDB = InfluxDBFactory.connect(URL, USERNAME, PASSWORD, client); influxDB.setDatabase(DATABASE); return influxDB; } public static String getDataBase() { return DATABASE; } } 使用示例:
InfluxDB influxDB = InfluxDBTemple.getInfluxDbConnection(); StringBuilder infuxDbSearchBuilder = getInfuxDbSearchSqlBuilder(startTime, endTime); Query query = new Query(infuxDbSearchBuilder.toString(), InfluxDBTemple.getDataBase()); QueryResult queryResult = influxDB.query(query); List<QueryResult.Result> queryResults = queryResult.getResults(); apiRankListResult = new ApiRankListResult(); List<ApiRank> apiRankList = Lists.newArrayList(); apiRankListResult.setData(apiRankList); queryResults.forEach(result -> result.getSeries().forEach( serie -> { convertFromSeries(apiRankList, serie); })); influxDB.close();
private StringBuilder getInfuxDbSearchSqlBuilder(Long startTime, Long endTime) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("select sum(five_nums) as fivexxNums,sum(qps) as qps FROM \"a_week\".api_metrics WHERE fivexx_nums > 0 AND time >=").append(startTime * 1000 * 1000).append(" AND time <=").append(endTime * 1000 * 1000).append(" GROUP BY service,app"); String searchInfluxDBSql = stringBuilder.toString(); log.info("searchInfluxDBSql:" + searchInfluxDBSql); return stringBuilder; }