|  | @@ -0,0 +1,69 @@
 | 
	
		
			
				|  |  | +package com.usky.common.influx.core;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import lombok.Data;
 | 
	
		
			
				|  |  | +import org.influxdb.InfluxDB;
 | 
	
		
			
				|  |  | +import org.influxdb.InfluxDBFactory;
 | 
	
		
			
				|  |  | +import org.influxdb.dto.Query;
 | 
	
		
			
				|  |  | +import org.influxdb.dto.QueryResult;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +@Data
 | 
	
		
			
				|  |  | +public class InfluxDbUtils {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private String userName;
 | 
	
		
			
				|  |  | +    private String password;
 | 
	
		
			
				|  |  | +    private String url;
 | 
	
		
			
				|  |  | +    public String database;
 | 
	
		
			
				|  |  | +    private String retentionPolicy;
 | 
	
		
			
				|  |  | +    // InfluxDB实例
 | 
	
		
			
				|  |  | +    private InfluxDB influxDB;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    // 数据保存策略
 | 
	
		
			
				|  |  | +    public static String policyNamePix = "logRetentionPolicy_";
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public InfluxDbUtils(String userName, String password, String url, String database,
 | 
	
		
			
				|  |  | +                         String retentionPolicy) {
 | 
	
		
			
				|  |  | +        this.userName = userName;
 | 
	
		
			
				|  |  | +        this.password = password;
 | 
	
		
			
				|  |  | +        this.url = url;
 | 
	
		
			
				|  |  | +        this.database = database;
 | 
	
		
			
				|  |  | +        this.retentionPolicy = retentionPolicy == null || "".equals(retentionPolicy) ? "autogen" : retentionPolicy;
 | 
	
		
			
				|  |  | +        this.influxDB = influxDbBuild();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 连接数据库 ,若不存在则创建
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @return influxDb实例
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private InfluxDB influxDbBuild() {
 | 
	
		
			
				|  |  | +        if (influxDB == null) {
 | 
	
		
			
				|  |  | +            influxDB = InfluxDBFactory.connect(url, userName, password);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +//            createDB(database);
 | 
	
		
			
				|  |  | +            influxDB.setDatabase(database);
 | 
	
		
			
				|  |  | +        } catch (Exception e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +//          log.error("create influx db failed, error: {}", e.getMessage());
 | 
	
		
			
				|  |  | +        } finally {
 | 
	
		
			
				|  |  | +            influxDB.setRetentionPolicy(retentionPolicy);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        influxDB.setLogLevel(InfluxDB.LogLevel.BASIC);
 | 
	
		
			
				|  |  | +        return influxDB;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 查询
 | 
	
		
			
				|  |  | +     * @param command
 | 
	
		
			
				|  |  | +     * 查询语句
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public QueryResult query(String command) {
 | 
	
		
			
				|  |  | +        return influxDB.query(new Query(command, database));
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +}
 |