1.influxDBConfig里面:
package com.tenoic.qq.configs;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import java.util.List;
import java.util.Map;
/**
* @auther xu
* @date 2019-10-18 10:20
* @function
* @return
*/
public class InfluxDBConfig {
private static String openurl = "http://127.0.0.1:8086";//连接地址
private static String username = "admin";//用户名
private static String password = "admin";//密码
private static String database = "XCTGL2";//数据库
private InfluxDB influxDB;
public InfluxDBConfig(String username, String password, String openurl, String database){
this.username = username;
this.password = password;
this.openurl = openurl;
this.database = database;
}
public static InfluxDBConfig setUp(){
//创建 连接
InfluxDBConfig influxDbUtil = new InfluxDBConfig(username, password, openurl, database);
influxDbUtil.influxDbBuild();
influxDbUtil.createRetentionPolicy();
// influxDB.deleteDB(database);
// influxDB.createDB(database);
return influxDbUtil;
}
/**连接时序数据库;获得InfluxDB**/
public InfluxDB influxDbBuild(){
if(influxDB == null){
influxDB = InfluxDBFactory.connect(openurl, username, password);
influxDB.createDatabase(database);
}
return influxDB;
}
/**
* 设置数据保存策略
* defalut 策略名 /database 数据库名/ 30d 数据保存时限30天/ 1 副本个数为1/ 结尾DEFAULT 表示 设为默认的策略
*/
public void createRetentionPolicy(){
String command = String.format("CREATE RETENTION POLICY \"%s\" ON \"%s\" DURATION %s REPLICATION %s DEFAULT",
"defalut", database, "30d", 1);
this.query(command);
}
/**
* 查询
* @param command 查询语句
* @return
*/
public QueryResult query(String command){
return influxDB.query(new Query(command, database));
}
/**
* 插入
* @param tags 标签
* @param fields 字段
*/
public void insert(String measurement,Map<String, String> tags, Map<String, Object> fields){
Point.Builder builder = Point.measurement(measurement);
builder.tag(tags);
builder.fields(fields);
influxDB.write(database, "", builder.build());
}
/**
* 删除
* @param command 删除语句
* @return 返回错误信息
*/
public String deleteMeasurementData(String command){
QueryResult result = influxDB.query(new Query(command, database));
return result.getError();
}
/**
* 创建数据库
* @param dbName
*/
public void createDB(String dbName){
influxDB.createDatabase(dbName);
}
/**
* 删除数据库
* @param dbName
*/
public void deleteDB(String dbName){
influxDB.deleteDatabase(dbName);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getOpenurl() {
return openurl;
}
public void setOpenurl(String openurl) {
this.openurl = openurl;
}
public void setDatabase(String database) {
this.database = database;
}
/* public void getAll(){
List strings = influxDB.describeDatabases();
//for (String string : strings) {
//String sql = "use " + string ;
//influxDB.setDatabase(string);
String sql = "show measurements" ;
QueryResult query = query(sql);
List results = query.getResults();
for (QueryResult.Result result : results) {
System.out.println(result);
}
// System.out.println(string);
//}
}*/
}
2.在pom.xml里面:
<!-- influxdb -->
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>2.7</version>
</dependency>
3.在application.properties里面:
#influxDB
spring.influx.url: http://localhost:8086
spring.influx.user: admin
spring.influx.password: admin
4.influxdbController里面:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tenoic.qq.configs.InfluxDBConfig;
import com.tenoic.qq.configs.redisConfig;
import org.influxdb.dto.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @auther xu
* @date 2019-10-31 16:14
* @function 时序数据库控制器
* @return
*/
@Controller
public class InfluxDBController {
@Resource
private redisConfig redisConfig;
private InfluxDBConfig influxDBConfig;
/*根据搜索时间段查询*/
@RequestMapping(value = "/searchtime")
@ResponseBody
public List<Map<String, Object>> search1(@RequestParam(name = "startTime", required = false) String startTime,
@RequestParam(name = "endTime", required = false) String endTime,
@RequestParam(name = "oldname", required = false) String oldname,
@RequestParam(name = "parentoldname", required = false) String parentoldname) {
influxDBConfig = InfluxDBConfig.setUp();
/*查询时序数据库*/
String sql = "SELECT " + oldname + ",time from " + parentoldname + " where time >= " + startTime + "ms and time <= " + endTime + "ms ";
QueryResult results = influxDBConfig.query(sql);
List<Map<String, Object>> maps = new ArrayList<>();
Map<String, Object> time = new HashMap<String, Object>();
Object[] times = null;
Map<String, Object> valuemaps = new HashMap<String, Object>();
Object[] valueObjects = null;
if (results.getResults() != null) {
for (QueryResult.Result result : results.getResults()) {
List<QueryResult.Series> series = result.getSeries();
if (series != null) {
for (int i = 0; i < series.size() ; i++) {
List<List<Object>> values = series.get(i).getValues();//字段子集合
List<String> colums = series.get(i).getColumns();//字段名
times = new Object[values.size()];
valueObjects = new Object[values.size()];
for (int j = 0; j < values.size(); j++) {
String words = (String) values.get(j).get(0);
String newStr = words.replaceAll("T"," ");
String newStr1 = newStr.replaceAll("Z","");
String intNumber = newStr1.substring(0,newStr1.indexOf("."));
System.out.println(intNumber);
times[j] = intNumber;
valueObjects[j] = values.get(j).get(1);
}
}
}
}
}
time.put("time" , times);
valuemaps.put("valuemaps", valueObjects);
maps.add(time);
maps.add(valuemaps);
return maps;
}