influxdb入门——和mongodb一样可以动态增加字段
./influxd [--config yourconfigfile 2> /dev/null] 之所以重定向 因为默认log是stderr
再启动客户端./influx
> CREATE DATABASE mydb
> show databases name: databases name ---- _internal mydb > use mydb Using database mydb > INSERT cpu,host=serverA,region=us_west value=0.64 > SELECT "host", "region", "value" FROM "cpu" name: cpu time host region value ---- ---- ------ ----- 1493949841535348823 serverA us_west 0.64 > INSERT temperature,machine=unit42,type=assembly external=25,internal=37 > > SELECT * FROM "temperature" name: temperature time external internal machine type ---- -------- -------- ------- ---- 1493950051939288361 25 37 unit42 assembly > INSERT temperature,machine=unit42,type=assembly external=25,internal=37 > SELECT * FROM "temperature" name: temperature time external internal machine type ---- -------- -------- ------- ---- 1493950051939288361 25 37 unit42 assembly 1493951735782511244 25 37 unit42 assembly > INSERT temperature,machine=unit42,type=assembly external=25,internal=37,kaka=100 > SELECT * FROM "temperature" name: temperature time external internal kaka machine type ---- -------- -------- ---- ------- ---- 1493950051939288361 25 37 unit42 assembly 1493951735782511244 25 37 unit42 assembly 1493951747715731172 25 37 100 unit42 assembly > INSERT temperature,machine=unit42,type=assembly external=25,internal=37,kaka=100,other=1223 > SELECT * FROM "temperature" name: temperature time external internal kaka machine other type ---- -------- -------- ---- ------- ----- ---- 1493950051939288361 25 37 unit42 assembly 1493951735782511244 25 37 unit42 assembly 1493951747715731172 25 37 100 unit42 assembly 1493951759943810550 25 37 100 unit42 1223 assembly
可以看到是schema free的,可以动态的添加字段!!!
插入数据:
Time Series Name:对应数据库的表名,比如已经创建的cpu表。
Values:一条记录的取值,InfluxDB不需要先定义列,根据mongodb类似!
python代码:
>>> from influxdb import InfluxDBClient >>> json_body = [ { "measurement": "cpu_load_short", "tags": { "host": "server01", "region": "us-west" }, "time": "2009-11-10T23:00:00Z", "fields": { "value": 0.64 } } ] >>> client = InfluxDBClient('localhost', 8086, 'root', 'root', 'example') >>> client.create_database('example') >>> client.write_points(json_body) >>> result = client.query('select value from cpu_load_short;') >>> print("Result: {0}".format(result))
查看数据:
~/influxdb-1.2.2-1/usr/bin/influx Connected to http://localhost:8086 version 1.2.2 InfluxDB shell version: 1.2.2 > use example Using database example > select * from cpu_load_short name: cpu_load_short time host region value ---- ---- ------ ----- 1257894000000000000 server01 us-west 0.64
参考:https://docs.influxdata.com/influxdb/v1.2/introduction/getting_started/