influxdb入门——和mongodb一样可以动态增加字段
阅读原文时间:2023年07月11日阅读:2

./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
---- ---- ------ -----
serverA us_west 0.64

INSERT temperature,machine=unit42,type=assembly external=,internal=

SELECT * FROM "temperature"
name: temperature
time external internal machine type
---- -------- -------- ------- ----
unit42 assembly

INSERT temperature,machine=unit42,type=assembly external=,internal=
SELECT * FROM "temperature"
name: temperature
time external internal machine type
---- -------- -------- ------- ----
unit42 assembly
unit42 assembly

INSERT temperature,machine=unit42,type=assembly external=,internal=,kaka=
SELECT * FROM "temperature"

name: temperature
time external internal kaka machine type
---- -------- -------- ---- ------- ----
unit42 assembly
unit42 assembly
unit42 assembly

INSERT temperature,machine=unit42,type=assembly external=,internal=,kaka=,other=
SELECT * FROM "temperature"

name: temperature
time external internal kaka machine other type
---- -------- -------- ---- ------- ----- ----
unit42 assembly
unit42 assembly
unit42 assembly
unit42 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/

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章