时序数据库-4-[IoTDB]的python3操作

从采集到存储:时序数据库到底怎么处理时间?
iotdb官方文档手册

1 容器安装iotdb

可以使用docker volume create命令创建 docker 卷。此命令将在/var/lib/docker/volumes目录中创建一个卷。

docker search iotdb
docker pull apache/iotdb
(1)创建数据文件和日志的 docker 挂载目录
docker volume create mydata
docker volume create mylogs

(2)运行docker容器
docker run --name iotdb  -p 6667:6667 -v mydata:/iotdb/data -v mylogs:/iotdb/logs -d apache/iotdb:latest /iotdb/bin/start-server.sh

(3)可以使用docker ps来检查是否运行成功
(4)获取container的ID
docker container ls
(5)进入容器
docker exec -it iotdb /bin/bash
(6)登陆IotDB
/iotdb/sbin/start-cli.sh -h localhost -p 6667 -u root -pw root

2 python操作iotdb

要求Python >=3.7
pip3 install apache-iotdb

2.1 session初始化

from iotdb.Session import Session
ip = "192.168.43.212"
port_= "6667"
username_ = "root"
password_ = "root"

session = Session(ip,port_,username_,password_)
session.open(enable_rpc_compression=False)
zone = session.get_time_zone()
session.close()

2.2 存储组管理

设置存储组
session.set_storage_group(group_name)
删除一个或多个存储组
session.delete_storage_group(group_name)
session.delete_storage_groups(group_name_lst)

命令行操作

IoTDB> SET STORAGE GROUP TO root.in

2.3 时间序列管理

创建一个或多个时间序列
session.create_time_series(ts_path, data_type, encoding, compressor,
    props=None, tags=None, attributes=None, alias=None)
      
session.create_multi_time_series(
    ts_path_lst, data_type_lst, encoding_lst, compressor_lst,
    props_lst=None, tags_lst=None, attributes_lst=None, alias_lst=None
)
创建对齐的时间序列
session.create_aligned_time_series(
    device_id, measurements_lst, data_type_lst, encoding_lst, compressor_lst
)
删除一个或多个时间序列
session.delete_time_series(paths_list)
核查指定的时间序列是否存在
session.check_time_series_exists(path)

命令行

IoTDB> CREATE TIMESERIES root.in.wf01.wt01.status WITH DATATYPE=BOOLEAN, ENCODING=PLAIN
IoTDB> CREATE TIMESERIES root.in.wf01.wt01.temperature WITH DATATYPE=FLOAT, ENCODING=RLE
IoTDB> show timeseries

参数说明

create single time series
:param ts_path: String, complete time series path (starts from root)
:param data_type: TSDataType, data type for this time series
:param encoding: TSEncoding, encoding for this time series
:param compressor: Compressor, compressing type for this time series
:param props: Dictionary, properties for time series
:param tags: Dictionary, tag map for time series
:param attributes: Dictionary, attribute map for time series
:param alias: String, measurement alias for time series

3 应用举例

3.1 创建session连接

from iotdb.Session import Session
# creating session connection.
ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)
session.close()

3.2 创建与删除存储组

from iotdb.Session import Session
# creating session connection.
ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)

# set and delete storage groups
session.set_storage_group("root.sg_test_01")
session.set_storage_group("root.sg_test_02")
session.set_storage_group("root.sg_test_03")
session.set_storage_group("root.sg_test_04")
session.delete_storage_group("root.sg_test_02")
session.delete_storage_groups(["root.sg_test_03", "root.sg_test_04"])

session.close()

命令行操作

IoTDB> show storage group

3.3 创建时间序列

3.3.1 每次创建一个

from iotdb.Session import Session
from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor

# creating session connection.
ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)

# set storage groups
session.set_storage_group("root.sg_test_01")

# setting time series.
session.create_time_series(
    "root.sg_test_01.d_01.s_01", TSDataType.BOOLEAN, TSEncoding.PLAIN, Compressor.SNAPPY)
session.create_time_series(
    "root.sg_test_01.d_01.s_02", TSDataType.INT32, TSEncoding.PLAIN, Compressor.SNAPPY)
session.create_time_series(
    "root.sg_test_01.d_01.s_03", TSDataType.INT64, TSEncoding.PLAIN, Compressor.SNAPPY)

session.create_time_series(
    "root.sg_test_01.d_02.s_01",
    TSDataType.BOOLEAN,
    TSEncoding.PLAIN,
    Compressor.SNAPPY,
    None,
    {"tag1": "v1"},
    {"description": "v1"},
    "temperature",
)
session.close()

3.3.2 每次创建多个

from iotdb.Session import Session
from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor

# creating session connection.
ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)

# set storage groups
session.set_storage_group("root.sg_test_01")

# setting multiple time series once.
ts_path_lst_ = [
    "root.sg_test_01.d_01.s_04",
    "root.sg_test_01.d_01.s_05",
    "root.sg_test_01.d_01.s_06",
    "root.sg_test_01.d_01.s_07",
    "root.sg_test_01.d_01.s_08",
    "root.sg_test_01.d_01.s_09",
]
data_type_lst_ = [
    TSDataType.FLOAT,
    TSDataType.DOUBLE,
    TSDataType.TEXT,
    TSDataType.FLOAT,
    TSDataType.DOUBLE,
    TSDataType.TEXT,
]
encoding_lst_ = [TSEncoding.PLAIN for _ in range(len(data_type_lst_))]
compressor_lst_ = [Compressor.SNAPPY for _ in range(len(data_type_lst_))]
session.create_multi_time_series(
    ts_path_lst_, data_type_lst_, encoding_lst_, compressor_lst_
)
session.close()

3.3.3 每次创建多个带标签

from iotdb.Session import Session
from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor

# creating session connection.
ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)

# set storage groups
session.set_storage_group("root.sg_test_01")

# setting multiple time series once.
ts_path_lst_ = [
    "root.sg_test_01.d_02.s_04",
    "root.sg_test_01.d_02.s_05",
    "root.sg_test_01.d_02.s_06",
    "root.sg_test_01.d_02.s_07",
    "root.sg_test_01.d_02.s_08",
    "root.sg_test_01.d_02.s_09",
]
data_type_lst_ = [
    TSDataType.FLOAT,
    TSDataType.DOUBLE,
    TSDataType.TEXT,
    TSDataType.FLOAT,
    TSDataType.DOUBLE,
    TSDataType.TEXT,
]
encoding_lst_ = [TSEncoding.PLAIN for _ in range(len(data_type_lst_))]
compressor_lst_ = [Compressor.SNAPPY for _ in range(len(data_type_lst_))]
tags_lst_ = [{"tag2": "v2"} for _ in range(len(data_type_lst_))]
attributes_lst_ = [{"description": "v2"} for _ in range(len(data_type_lst_))]
session.create_multi_time_series(
    ts_path_lst_,
    data_type_lst_,
    encoding_lst_,
    compressor_lst_,
    None,
    tags_lst_,
    attributes_lst_,
    None,
)
session.close()

3.3.4 删除时间序列

from iotdb.Session import Session
from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor

# creating session connection.
ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)

# delete time series
session.delete_time_series(
    [
        "root.sg_test_01.d_01.s_07",
        "root.sg_test_01.d_01.s_08",
        "root.sg_test_01.d_01.s_09",
    ]
)

session.close()

3.3.5 检查时间序列

from iotdb.Session import Session
from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor

# creating session connection.
ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)
# checking time series
print(
    "s_07 expecting False, checking result: ",
    session.check_time_series_exists("root.sg_test_01.d_01.s_07"),
)
print(
    "s_03 expecting True, checking result: ",
    session.check_time_series_exists("root.sg_test_01.d_01.s_03"),
)
print(
    "d_02.s_01 expecting True, checking result: ",
    session.check_time_series_exists("root.sg_test_01.d_02.s_01"),
)
print(
    "d_02.s_06 expecting True, checking result: ",
    session.check_time_series_exists("root.sg_test_01.d_02.s_06"),
)
session.close()

3.4 插入数据

3.4.1 插入一条记录

from iotdb.Session import Session
from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor

# creating session connection.
ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)

# insert one record into the database.
measurements_ = ["s_01", "s_02", "s_03", "s_04", "s_05", "s_06"]
values_ = [False, 10, 11, 1.1, 10011.1, "test_record"]
data_types_ = [
    TSDataType.BOOLEAN,
    TSDataType.INT32,
    TSDataType.INT64,
    TSDataType.FLOAT,
    TSDataType.DOUBLE,
    TSDataType.TEXT,
]
session.insert_record("root.sg_test_01.d_01", 1, measurements_, data_types_, values_)
session.close()

命令行

IoTDB> select * from root.sg_test_01.d_01

3.4.2 插入多条记录

# insert multiple records into database
from iotdb.Session import Session
from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor

# creating session connection.
ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)


measurements_list_ = [
    ["s_01", "s_02", "s_03", "s_04", "s_05", "s_06"],
    ["s_01", "s_02", "s_03", "s_04", "s_05", "s_06"],
]
values_list_ = [
    [False, 22, 33, 4.4, 55.1, "test_records01"],
    [True, 77, 88, 1.25, 8.125, "test_records02"],
]
data_types_ = [
    TSDataType.BOOLEAN,
    TSDataType.INT32,
    TSDataType.INT64,
    TSDataType.FLOAT,
    TSDataType.DOUBLE,
    TSDataType.TEXT,
]
data_type_list_ = [data_types_, data_types_]
device_ids_ = ["root.sg_test_01.d_01", "root.sg_test_01.d_01"]
session.insert_records(
    device_ids_, [2, 3], measurements_list_, data_type_list_, values_list_
)
session.close()

3.4.3 插入一个tablet

# insert multiple records into database
from iotdb.Session import Session
from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor

# creating session connection.
from iotdb.utils.Tablet import Tablet

ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)


measurements_ = ["s_01", "s_02", "s_03", "s_04", "s_05", "s_06"]
data_types_ = [
    TSDataType.BOOLEAN,
    TSDataType.INT32,
    TSDataType.INT64,
    TSDataType.FLOAT,
    TSDataType.DOUBLE,
    TSDataType.TEXT,
]
# insert one tablet into the database.
values_ = [
    [False, 10, 11, 1.1, 10011.1, "test01"],
    [True, 100, 11111, 1.25, 101.0, "test02"],
    [False, 100, 1, 188.1, 688.25, "test03"],
    [True, 0, 0, 0, 6.25, "test04"],
]  # Non-ASCII text will cause error since bytes can only hold 0-128 nums.
timestamps_ = [4, 5, 6, 7]
tablet_ = Tablet(
    "root.sg_test_01.d_01", measurements_, data_types_, values_, timestamps_
)
session.insert_tablet(tablet_)
session.close()

3.4.4 插入一个numpy tablet

# insert multiple records into database
from iotdb.Session import Session
from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor
import numpy as np
# creating session connection.
from iotdb.utils.NumpyTablet import NumpyTablet
from iotdb.utils.Tablet import Tablet

ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)


measurements_ = ["s_01", "s_02", "s_03", "s_04", "s_05", "s_06"]
data_types_ = [
    TSDataType.BOOLEAN,
    TSDataType.INT32,
    TSDataType.INT64,
    TSDataType.FLOAT,
    TSDataType.DOUBLE,
    TSDataType.TEXT,
]
# insert one numpy tablet into the database.
np_values_ = [
    np.array([False, True, False, True], TSDataType.BOOLEAN.np_dtype()),
    np.array([10, 100, 100, 0], TSDataType.INT32.np_dtype()),
    np.array([11, 11111, 1, 0], TSDataType.INT64.np_dtype()),
    np.array([1.1, 1.25, 188.1, 0], TSDataType.FLOAT.np_dtype()),
    np.array([10011.1, 101.0, 689.25, 6.25], TSDataType.DOUBLE.np_dtype()),
    np.array(["test01", "test02", "test03", "test04"], TSDataType.TEXT.np_dtype()),
]
np_timestamps_ = np.array([1, 2, 3, 4], TSDataType.INT64.np_dtype())
np_tablet_ = NumpyTablet(
    "root.sg_test_01.d_02", measurements_, data_types_, np_values_, np_timestamps_
)
session.insert_tablet(np_tablet_)
session.close()

3.4.5 插入一个unsorted numpy tablet

# insert multiple records into database
from iotdb.Session import Session
from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor
import numpy as np
# creating session connection.
from iotdb.utils.NumpyTablet import NumpyTablet
from iotdb.utils.Tablet import Tablet

ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)

measurements_ = ["s_01", "s_02", "s_03", "s_04", "s_05", "s_06"]
data_types_ = [
    TSDataType.BOOLEAN,
    TSDataType.INT32,
    TSDataType.INT64,
    TSDataType.FLOAT,
    TSDataType.DOUBLE,
    TSDataType.TEXT,
]
# insert one unsorted numpy tablet into the database.
np_values_unsorted = [
    np.array([False, False, False, True, True], np.dtype(">?")),
    np.array([0, 10, 100, 1000, 10000], np.dtype(">i4")),
    np.array([1, 11, 111, 1111, 11111], np.dtype(">i8")),
    np.array([1.1, 1.25, 188.1, 0, 8.999], np.dtype(">f4")),
    np.array([10011.1, 101.0, 688.25, 6.25, 8, 776], np.dtype(">f8")),
    np.array(["test09", "test08", "test07", "test06", "test05"]),
]
np_timestamps_unsorted = np.array([9, 8, 7, 6, 5], np.dtype(">i8"))
np_tablet_unsorted = NumpyTablet(
    "root.sg_test_01.d_02",
    measurements_,
    data_types_,
    np_values_unsorted,
    np_timestamps_unsorted,
)
session.insert_tablet(np_tablet_unsorted)
print(np_tablet_unsorted.get_timestamps())
for value in np_tablet_unsorted.get_values():
    print(value)

3.4.6 插入多个tablet

# insert multiple records into database
from iotdb.Session import Session
from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor
import numpy as np
# creating session connection.
from iotdb.utils.NumpyTablet import NumpyTablet
from iotdb.utils.Tablet import Tablet

ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)

measurements_ = ["s_01", "s_02", "s_03", "s_04", "s_05", "s_06"]
data_types_ = [
    TSDataType.BOOLEAN,
    TSDataType.INT32,
    TSDataType.INT64,
    TSDataType.FLOAT,
    TSDataType.DOUBLE,
    TSDataType.TEXT,
]
values_ = [
    [False, 10, 11, 1.1, 10011.1, "test01"],
    [True, 100, 11111, 1.25, 101.0, "test02"],
    [False, 100, 1, 188.1, 688.25, "test03"],
    [True, 0, 0, 0, 6.25, "test04"],
]  # Non-ASCII text will cause error since bytes can only hold 0-128 nums.

# insert multiple tablets into database
tablet_01 = Tablet(
    "root.sg_test_01.d_01", measurements_, data_types_, values_, [8, 9, 10, 11]
)
tablet_02 = Tablet(
    "root.sg_test_01.d_01", measurements_, data_types_, values_, [12, 13, 14, 15]
)
session.insert_tablets([tablet_01, tablet_02])
session.close()

3.4.7 插入带empty的tablet

空值会自动补。

# insert multiple records into database
from iotdb.Session import Session
from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor
import numpy as np
# creating session connection.
from iotdb.utils.NumpyTablet import NumpyTablet
from iotdb.utils.Tablet import Tablet

ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)

measurements_ = ["s_01", "s_02", "s_03", "s_04", "s_05", "s_06"]
data_types_ = [
    TSDataType.BOOLEAN,
    TSDataType.INT32,
    TSDataType.INT64,
    TSDataType.FLOAT,
    TSDataType.DOUBLE,
    TSDataType.TEXT,
]
# insert one tablet with empty cells into the database.
values_ = [
    [None, 10, 11, 1.1, 10011.1, "test01"],
    [True, None, 11111, 1.25, 101.0, "test02"],
    [False, 100, 1, None, 688.25, "test03"],
    [True, 0, 0, 0, 6.25, None],
]  # Non-ASCII text will cause error since bytes can only hold 0-128 nums.
timestamps_ = [16, 17, 18, 19]
tablet_ = Tablet(
    "root.sg_test_01.d_01", measurements_, data_types_, values_, timestamps_
)
session.insert_tablet(tablet_)
session.close()

3.4.8 插入一个设备的数据

会覆盖以前的数据

# insert multiple records into database
from iotdb.Session import Session
from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor
import numpy as np
# creating session connection.
from iotdb.utils.NumpyTablet import NumpyTablet
from iotdb.utils.Tablet import Tablet

ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)

# insert records of one device
time_list = [1, 2, 3]
measurements_list = [
    ["s_01", "s_02", "s_03"],
    ["s_01", "s_02", "s_03"],
    ["s_01", "s_02", "s_03"],
]
data_types_list = [
    [TSDataType.BOOLEAN, TSDataType.INT32, TSDataType.INT64],
    [TSDataType.BOOLEAN, TSDataType.INT32, TSDataType.INT64],
    [TSDataType.BOOLEAN, TSDataType.INT32, TSDataType.INT64],
]
values_list = [[False, 22, 33], [True, 1, 23], [False, 15, 26]]

session.insert_records_of_one_device(
    "root.sg_test_01.d_01", time_list, measurements_list, data_types_list, values_list
)
session.close()

3.5 执行sql

3.5.1 执行非查询语句sql

from iotdb.Session import Session

ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)

# execute non-query sql statement
session.execute_non_query_statement(
    "insert into root.sg_test_01.d_01(timestamp, s_02) values(16, 188)"
)
session.close()

3.5.2 执行查询语句sql

from iotdb.Session import Session

ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)

# execute sql query statement
#sql_str = "select * from root.sg_test_01.d_01"
sql_str = "select s_01, s_02, s_03, s_04, s_05, s_06 from root.sg_test_01.d_02"
with session.execute_query_statement(sql_str) as session_data_set:
    session_data_set.set_fetch_size(1024)
    while session_data_set.has_next():
        print(session_data_set.next())
session.close()

3.5.3 查询后转换为df

from iotdb.Session import Session
import pandas as pd
ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024, zone_id="UTC+8")
print(session.get_time_zone())
session.open(False)

# execute sql query statement
sql_str = "select * from root.sg_test_01.d_01"

with session.execute_query_statement(sql_str) as session_data_set:
    df = session_data_set.todf()
session.close()

4 整体测试

4.1 写入记录并查询

写入后时间戳会减去8小时,读取后正常。
在这里插入图片描述

from iotdb.Session import Session
# creating session connection.
from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor
import time
from datetime import datetime
# 字符串时间戳转换为整数时间戳
def tostamp_int(timestr):
    datetime_obj = datetime.strptime(timestr, "%Y-%m-%d %H:%M:%S.%f")
    obj_stamp = int(time.mktime(datetime_obj.timetuple()) * 1000.0 + datetime_obj.microsecond / 1000.0)
    return obj_stamp


ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024)
print(session.get_time_zone())
session.open(False)

devid = "667601E99"
pointid = "01"
kpiid = "环境温度"
timestr1 = "2022-08-31 13:18:55.786"
timestr2 = "2022-08-31 13:18:56.505"
ts_path = "root.main.{}.{}.{}".format(devid,pointid,kpiid)


# 创建存储组
session.set_storage_group("root.main")
# 创建时间序列
session.create_time_series(ts_path, TSDataType.FLOAT, TSEncoding.PLAIN, Compressor.SNAPPY)

# 插入多条记录
measurements_list_ = [
    ["环境温度"],
    ["环境温度"],
]
values_list_ = [
    [35.6],
    [23.8],
]

data_types_ = [TSDataType.FLOAT]
data_type_list_ = [data_types_, data_types_]
device_ids_ = ["root.main.667601E99.01", "root.main.667601E99.01"]

session.insert_records(
    device_ids_, [tostamp_int(timestr1), tostamp_int(timestr2)], measurements_list_, data_type_list_, values_list_
)

print(tostamp_int(timestr1),tostamp_int(timestr2))
# execute sql query statement
sql_str = "select * from root.main"

with session.execute_query_statement(sql_str) as session_data_set:
    df = session_data_set.todf()
print(df)
session.close()

4.2 sql写入并查询

from iotdb.Session import Session
# creating session connection.
from iotdb.utils.IoTDBConstants import TSDataType, TSEncoding, Compressor
import time
from datetime import datetime
# 字符串时间戳转换为整数时间戳
def tostamp_int(timestr):
    datetime_obj = datetime.strptime(timestr, "%Y-%m-%d %H:%M:%S.%f")
    obj_stamp = int(time.mktime(datetime_obj.timetuple()) * 1000.0 + datetime_obj.microsecond / 1000.0)
    return obj_stamp


ip = "192.168.124.26"
port_ = "6667"
username_ = "root"
password_ = "root"
session = Session(ip, port_, username_, password_, fetch_size=1024)
print(session.get_time_zone())
session.open(False)

devid = "667601E99"
pointid = "01"
kpiid = "环境温度"
timestr1 = "2022-08-31 14:18:55.786"
ts_path = "root.main.{}.{}.{}".format(devid,pointid,kpiid)


# 创建存储组
session.set_storage_group("root.main")
# 创建时间序列
session.create_time_series(ts_path, TSDataType.FLOAT, TSEncoding.PLAIN, Compressor.SNAPPY)

# execute no sql query statement
sql_str = "insert into root.main.667601E99.01(timestamp,{}) values({},{})".format(kpiid,tostamp_int(timestr1),67.8)
print(sql_str)
session.execute_non_query_statement(sql_str)

# execute sql query statement
sql_str = "select * from root.main"
with session.execute_query_statement(sql_str) as session_data_set:
    df = session_data_set.todf()
print(df)
session.close()
Python IOTDB是一个用于连接和操作IOTDB数据库Python库。您可以使用该库来执行各种操作,包括数据查询、数据写入和数据导出。 要使用Python IOTDB,首先需要安装该库。您可以使用pip来安装,具体命令是pip install apache-iotdb。确保您的Python版本大于等于3.7。 安装完成后,您可以使用以下代码来建立与IOTDB数据库的连接: ```python from iotdb.Session import Session ip = "127.0.0.1" port_ = "6667" username_ = "root" password_ = "root" session = Session(ip, port_, username_, password_) session.open(enable_rpc_compression=False) ``` 使用上述代码,您可以建立一个会话并连接到指定的IOTDB数据库。然后,您可以执行各种操作,例如执行SQL查询和导出数据。 如果要执行SQL查询,您可以使用execute_query_statement方法。例如,以下代码执行了两个SQL查询,并将查询结果转换为DataFrame进行处理: ```python sql_str1 = "show timeseries" sql_str2 = "SELECT * FROM root.ln.wf01.wt01" with session.execute_query_statement(sql_str1) as session_data_set: df1 = session_data_set.todf() with session.execute_query_statement(sql_str2) as session_data_set: df2 = session_data_set.todf() print(df1) print(df2) ``` 执行完毕后,您可以关闭会话并断开与IOTDB数据库的连接: ```python session.close() ``` 如果您想导出数据到CSV文件,可以使用IOTDB自带的工具。您可以创建一个脚本文件,指定要导出的SQL语句,并使用export-csv.sh脚本来执行导出操作。导出的文件将保存在指定的目录中。 请注意,以上只是Python IOTDB的一些基本用法示例。根据实际需求,您可以使用更多的方法和参数来完成更复杂的操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [时序数据库-3-[IoTDB]的安装与使用](https://blog.csdn.net/qq_20466211/article/details/124224226)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮皮冰燃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值