基于Java的InfluxDB查询工具

简介 / Introduction

用Java编写的、用于向InfluxDB查询数据的工具类,无任何第三方依赖,内置HTTP解析器、Json解析器、InfluxDB数据解析器,可以方便集成到任何Java项目中,不会造成依赖冲突。

GitHub地址:https://github.com/XingKongSync/InfluxDB

用法 / Usage

String sql = "SELECT last(ff),at FROM \"2017-05\" WHERE aa = 'C14_L1-9_Ubc' and time>now() - 1h group by time(15s)";
String ip = "192.168.0.121";
int port = 8086;
InfluxDB influxdb = new InfluxDB();
influxdb.setServerIp(ip);
influxdb.setServerPort(port);

InfluxDataAdapter adapter = null;

if (influxdb.Connect()) {
	adapter = influxdb.Query("YZ001", sql);
	influxdb.DisConnect();
}

if (adapter != null) {
	try {
		InfluxResult result = adapter.GetResults().get(0);
		InfluxColumn timeColumn = result.GetColumn("time");
		InfluxColumn ffColumn = result.GetColumn("last");

		if (timeColumn != null && ffColumn != null) {
			InfluxResult.Iterator iter = result.GetIterator();
			while (iter.hasNext()) {
				float ff = iter.GetFloat(ffColumn);
				String time = iter.GetString(timeColumn);
				System.out.println("time: " + time + ", last: " + ff);
				iter.Next();
			}
		}
	} catch (Exception e) {
		System.err.println(e.getMessage());
	}
}

发表评论

邮箱地址不会被公开。 必填项已用*标注