IoTDB + Grafana 实现数据可视化(2)
前言
在实现历史数据可视化之后,见[IoTDB + Grafana 实现数据可视化(1)],自动生成数据,达到实时监控的目的提示:以下是本篇文章正文内容,下面案例可供参考
一、自动生成数据
使用Random 方法,以之前实际数据的平均值为基础,自动生成数据
在这里插入代码片
private static DataEntity AutoGetDAta(){
DataEntity dataEntity = new DataEntity();
Random random = new Random();
dataEntity.setId(random.nextInt(1024));
//dataEntity.setDatetime(datetime);
dataEntity.setInitialSulfurContent(random.nextDouble()+120);
dataEntity.setInitialOctaneNumber(random.nextDouble()+88);
dataEntity.setSulfurContentOfTheProduct(random.nextDouble()+88);
dataEntity.setProductOctaneNumber(random.nextDouble()+3);
dataEntity.setLossOfOctaneNumber(random.nextDouble()+1);
dataEntity.setV1(random.nextDouble()+160);
dataEntity.setV2(random.nextDouble());
dataEntity.setV3(random.nextDouble()+2);
dataEntity.setV4(random.nextDouble());
dataEntity.setV5(random.nextDouble()+60);
dataEntity.setV6(random.nextDouble()+410);
dataEntity.setV7(random.nextDouble()+70);
dataEntity.setV8(random.nextDouble());
dataEntity.setV9(random.nextDouble()+28);
dataEntity.setV10(random.nextDouble()+83);
dataEntity.setV11(random.nextDouble()+ random.nextInt(300)+600);
dataEntity.setV12(random.nextDouble()+230);
dataEntity.setV13(random.nextDouble()+200);
dataEntity.setV14(random.nextInt()+520222);
dataEntity.setV16(random.nextDouble()+50);
return dataEntity;
}
二、设置间隔时间,并上传数据
int count = 0;
while(count<1000) {
try {
Thread.sleep(5 * 1000); //设置暂停的时间 5 秒
count ++ ;
Date date = new Date();
Timestamp nowdate = new Timestamp(date.getTime());
// String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp);
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(nowdate);
DataEntity dataEntity1 = AutomaticallyGenerateData.AutoGetDAta();
/*
假设只有一个存储组 1 Storage Group
有三台设备 Device1 Device2 Device3
Device1 包含了 initialSulfurContent,InitialOctaneNumber 初始值
Device2 SulfurContentOfTheProduct,ProductOctaneNumber,LossOfOctaneNumber 结果值
Device3 v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16 几个设备的值
*/
//d1
//Drvice1.initialSulfurContent
statement.addBatch("insert into root.Drvice1(timestamp,initialSulfurContent) values("+time+","+dataEntity1.getInitialSulfurContent()+");");
// Drvice1.InitialOctaneNumber
statement.addBatch("insert into root.Drvice1(timestamp,InitialOctaneNumber) values("+time+","+dataEntity1.getInitialOctaneNumber()+");");
//d2
// LossOfOctaneNumber
statement.addBatch("insert into root.Drvice2(timestamp,LossOfOctaneNumber) values("+time+","+dataEntity1.getLossOfOctaneNumber()+");");
// ProductOctaneNumber
statement.addBatch("insert into root.Drvice2(timestamp,ProductOctaneNumber) values("+time+","+dataEntity1.getProductOctaneNumber()+");");
// SulfurContentOfTheProduct
statement.addBatch("insert into root.Drvice2(timestamp,SulfurContentOfTheProduct) values("+time+","+dataEntity1.getSulfurContentOfTheProduct()+");");
//d3
statement.addBatch("insert into root.Drvice3(timestamp,V1) values("+time+","+dataEntity1.getV1()+");");
statement.addBatch("insert into root.Drvice3(timestamp,V2) values("+time+","+dataEntity1.getV2()+");");
statement.addBatch("insert into root.Drvice3(timestamp,V3) values("+time+","+dataEntity1.getV3()+");");
statement.addBatch("insert into root.Drvice3(timestamp,V4) values("+time+","+dataEntity1.getV4()+");");
statement.addBatch("insert into root.Drvice3(timestamp,V5) values("+time+","+dataEntity1.getV5()+");");
statement.addBatch("insert into root.Drvice3(timestamp,V6) values("+time+","+dataEntity1.getV6()+");");
statement.addBatch("insert into root.Drvice3(timestamp,V7) values("+time+","+dataEntity1.getV7()+");");
statement.addBatch("insert into root.Drvice3(timestamp,V8) values("+time+","+dataEntity1.getV8()+");");
statement.addBatch("insert into root.Drvice3(timestamp,V9) values("+time+","+dataEntity1.getV9()+");");
statement.addBatch("insert into root.Drvice3(timestamp,V10) values("+time+","+dataEntity1.getV10()+");");
statement.addBatch("insert into root.Drvice3(timestamp,V11) values("+time+","+dataEntity1.getV11()+");");
statement.addBatch("insert into root.Drvice3(timestamp,V12) values("+time+","+dataEntity1.getV12()+");");
statement.addBatch("insert into root.Drvice3(timestamp,V13) values("+time+","+dataEntity1.getV13()+");");
statement.addBatch("insert into root.Drvice3(timestamp,V14) values("+time+","+dataEntity1.getV14()+");");
statement.addBatch("insert into root.Drvice3(timestamp,V16) values("+time+","+dataEntity1.getV16()+");");
statement.executeBatch();
// statement.executeQuery("select * from root.Drvice3");
outputResult(statement.getResultSet());
statement.clearBatch();
} catch (InterruptedException e) {
e.printStackTrace();
}
三、在Grafana设置刷新时间
启动自动刷新