Flask接口签名sign原理与实例代码浅析
711
2023-01-24
java使用influxDB数据库的详细代码
本文实例为大家分享了java使用influxDB数据库的具体代码,供大家参考,具体内容如下
1.pom.xml中导入jar包依赖
2.编写inflawkzMXHNuxDB工具类:
package com.hontye.parameter.util;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;
import org.influxdb.dto.Point.Builder;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import java.util.Map;
/**
* 时序数据库 InfluxDB 连接
* @author Dai_LW
*
*/
public class InfluxDbUtil {
private static String openurl = "http://127.0.0.1:8086";//连接地址
private static String username = "root";//用户名
private static String password = "root";//密码
private static String database = "PARAMTER_DB";//数据库
private static String measurement = "tw_parameter_tb";//表名
private InfluxDB influxDB;
public InfluxDbUtil(String username, String password, String openurl, String database){
this.username = username;
this.password = password;
this.openurl = openurl;
this.database = database;
}
public static InfluxDbUtil setUp(){
//创建 连接
InfluxDbUtil influxDbUtil = new InfluxDbUtil(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/ http://结尾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(Map
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;
}
}
3.存值
public class QuatyServiceImpl{
private InfluxDbUtil influxDB;
public void intoDb() {
influxDB = InfluxDbUtil.setUp();
Map
Map
tags.put("TAG_NAME",info.getKey());
fields.put("TAG_VALUE",code);
fields.put("TIMAMPEST", df.format(new Date()));
influxDB.insert(tags, fields);
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~