pyhton 操作 时序数据库(influxdb)

安装依赖包:influxdb

from influxdb import InfluxDBClient

client = InfluxDBClient('localhost', 8086, 'root', '', '') 

**# client = InfluxDBClient(host, port, user, password, database)  # 初始化**

操作数据库

显示已存在的所有数据库
使用get_list_database函数,示例如下:

print client.get_list_database() # 显示所有数据库名称

创建新数据库

使用create_database函数,示例如下:

client.create_database(‘testdb’) # 创建数据库

删除数据库

使用drop_database函数,示例如下:

client.drop_database(‘testdb’) # 删除数据库

#-*- coding:utf-8 -*-

from influxdb import InfluxDBClient

client = InfluxDBClient('localhost', 8086, 'root', '', '') # 初始化

print client.get_list_database() # 显示所有数据库名称

client.create_database('testdb') # 创建数据库

print client.get_list_database() # 显示所有数据库名称

client.drop_database('testdb') # 删除数据库

print client.get_list_database() # 显示所有数据库名称

操作数据库

显示已存在的所有数据库
使用get_list_database函数,示例如下:

print client.get_list_database() # 显示所有数据库名称

创建新数据库

使用create_database函数,示例如下:

client.create_database(‘testdb’) # 创建数据库

删除数据库

使用drop_database函数,示例如下:

client.drop_database(‘testdb’) # 删除数据库

表操作

InfluxDBClient中要指定连接的数据库,示例如下:

client = InfluxDBClient(‘localhost’, 8086, ‘root’, ‘’, ‘testdb’) # 初始化(指定要操作的数据库)
显示指定数据库中已存在的表
可以通过influxql语句实现,示例如下:

result = client.query(‘show measurements;’) # 显示数据库中的表
print(“Result: {0}”.format(result))
创建新表并添加数据
InfluxDB没有提供单独的建表语句,可以通过并添加数据的方式建表,示例如下:

json_body = [
{
“measurement”: “students”,
“tags”: {
“stuid”: “s123”
},
“time”: “2019-06-13T22:00:00Z”,
“fields”: {
“score”: 89
}
}
]

client = InfluxDBClient(‘localhost’, 8086, ‘root’, ‘’, ‘testdb’) # 初始化(指定要操作的数据库)
client.write_points(json_body) # 写入数据,同时创建表

删除表

可以通过influxql语句实现,示例如下:
client.query(“drop measurement students”) # 删除表

#-*- coding:utf-8 -*-

from influxdb import InfluxDBClient

json_body = [
    {
        "measurement": "students",
        "tags": {
            "stuid": "s123"
        },
        "time": "2019-06-13T22:00:00Z",
        "fields": {
            "score": 89
        }
    }
]

def showDBNames(client):
        result = client.query('show measurements;') # 显示数据库中的表
        print("Result: {0}".format(result))

client = InfluxDBClient('localhost', 8086, 'root', '', 'testdb') # 初始化(指定要操作的数据库)
showDBNames(client)
client.write_points(json_body) # 写入数据,同时创建表
showDBNames(client)
client.query("drop measurement students") # 删除表
showDBNames(client)

数据操作

InfluxDBClient中要指定连接的数据库,示例如下:

client = InfluxDBClient(‘localhost’, 8086, ‘root’, ‘’, ‘testdb’) # 初始化(指定要操作的数据库)
添加
可以通过write_points实现,示例如下:

json_body = [
{
“measurement”: “students”,
“tags”: {
“stuid”: “s123”
},
“time”: “2019-06-13T22:00:00Z”,
“fields”: {
“score”: 89
}
}
]

client.write_points(json_body) # 写入数据
查询
可以通过influxql语句实现,示例如下:

result = client.query(‘select * from students;’)
print(“Result: {0}”.format(result))
更新
tags 和 timestamp相同时数据会执行覆盖操作,相当于InfluxDB的更新操作。

删除
使用influxql语句实现,delete语法,示例如下:

client.query(‘delete from students;’) # 删除数据
数据操作完整示例如下:

#-*- coding:utf-8 -*-

from influxdb import InfluxDBClient

json_body = [
    {
        "measurement": "students",
        "tags": {
            "stuid": "s123"
        },
        "time": "2019-06-13T22:00:00Z",
        "fields": {
            "score": 89
        }
    }
]

def showDatas(client):
        result = client.query('select * from students;')
        print("Result: {0}".format(result))

client = InfluxDBClient('localhost', 8086, 'root', '', 'testdb') # 初始化
client.write_points(json_body) # 写入数据
showDatas(client)  # 查询数据
client.query('delete from students;') # 删除数据
showDatas(client)  # 查询数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值