./influxd -pidfile influxd.pid -config influxdb.conf
其它安装方式请参考: https://docs.influxdata.com/influxdb/v0.13/introduction/installation/
下载地址:https://dl.influxdata.com,找到对应版本的Key,拼上前面的url即可下载。
注意influxdb.conf需要按照修改某些dir和port。influxdb服务默认使用端口:
TCP port 8083 is used for InfluxDB’s Admin panel
TCP port 8086 is used for client-server communication over InfluxDB’s HTTP API
8088端口其实也被占用了,而且不可配置,参见https://github.com/influxdata/influxdb/blob/master/cmd/influxd/run/config.go第36行DefaultBindAddress。之前的influxdb版本TCP ports 8088是可配置的。
use testDB
insert weather,altitude=1000,area=北 temperature=11,humidity=-4
Http接口:
curl -i -XPOST 'http://localhost:8086/write?db=testDB'
--data-binary 'weather,altitude=1000,area=北 temperature=11,humidity=-4'
插入数据的格式似乎比较奇怪,这是因为influxDB储存数据所采用的是Line Protocol格式。在上面两个插入数据的方法中,都有一样的部分。
weather,altitude=1000,area=北 temperature=11,humidity=-4
其中:
use testDB
# 查询最新的三条数据
SELECT * FROM weather ORDER BY time DESC LIMIT 3
Http接口
curl -G 'http://localhost:8086/query?pretty=true'
--data-urlencode "db=testDB"
--data-urlencode "q=SELECT * FROM weather ORDER BY time DESC LIMIT 3"
InfluxDB是支持类SQL语句的,具体的查询语法都差不多。
private static InfluxDB getClient() {
InfluxDB client = InfluxDBFactory.connect("http://10.210.228.89:9501", "root", "123456");
Pong pong = client.ping();
if (pong != null) {
System.out.println("Pong : " + pong);
} else
return null;
client.createDatabase(dbName);
return client;
}
写数据:
private static void writeData001() {
Point point001 = Point.measurement("cpu")
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
.tag("ip", "127.0.0.1").tag("hostname", "localhost")
.addField("idle", 80L)
.addField("user", 9L)
.addField("system", 11L)
.build();
Point point002 = Point.measurement("disk")
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
.tag("ip", "127.0.0.1")
.addField("used", 80L)
.addField("free", 1L)
.build();
BatchPoints batchPoints = BatchPoints
.database(dbName)
//.tag("async", "true") //Add a tag to this set of points.
.retentionPolicy("default")
.consistency(InfluxDB.ConsistencyLevel.ALL)
.build();
batchPoints.point(point001).point(point002);
client.write(batchPoints);
}
private static void writeData002() {
// Flush every 2000 Points, at least every 100ms
client.enableBatch(2000, 100, TimeUnit.MILLISECONDS);
Point point1 = Point.measurement("cpu")
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
.addField("idle", 90L)
.addField("user", 9L)
.addField("system", 1L)
.build();
Point point2 = Point.measurement("disk")
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
.addField("used", 80L)
.addField("free", 1L)
.build();
client.write(dbName, "default", point1);
client.write(dbName, "default", point2);
}
查询: