InfluxDB
Attention:
How to use DataFrame Client
1
client = DataFrameClient('localhost', 8086, user, password, dbname)
When we want to insert a DataFrame in InfluxDB, the DataFrame must have a time format index, for example:
1 | # open test_data.json as record |
- InfluxDB writes points with the same timestamp but different measured values, otherwise InfluxDB will only insert a newest timestamp record.
Solve:
write_points use argument ‘tags’ or ‘tag_columns’(recommand)
Example:
1 | client.write_points(new_record, 'demo', tag_columns=['srcaddr'], protocol=protocol) |
Complete example of InfluxDB using DataFrameClient.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42"""Tutorial for using pandas and the InfluxDB client."""
import argparse
import pandas as pd
import json
from influxdb import DataFrameClient
def main():
"""Instantiate the connection to the InfluxDB client."""
user = 'root'
password = '0318'
dbname = 'assetdb_ts'
protocol = 'line'
client = DataFrameClient('localhost', 8086, user, password, dbname)
print("Create pandas DataFrame")
with open('data/test data/' + 'test_data.json') as record:
new_record = json.load(record)
new_record = pd.DataFrame(data=new_record,
columns=['srcaddr','srcport','First'])
new_record['First'] = pd.to_datetime(new_record['First'], format='%Y%m%d%H%M%S')
new_record.set_index('First', inplace=True)
print("Create database: " + dbname)
client.create_database(dbname)
print("Write DataFrame")
client.write_points(new_record, 'demo', tag_columns=['srcaddr'], protocol=protocol)
print("Read DataFrame")
result = client.query("select * from demo")
print("Result: {0}".format(result))
print("Delete database: " + dbname)
client.drop_database(dbname)
if __name__ == '__main__':
main()
1 | # -*- coding: utf-8 -*- |
- ‘field’ is the neccessary part of the influxDB data structure.
but ‘tag’ is not the neccessary part of the influxDB data structure, every ‘tag’ is a index, so the query of ‘tag’ is faster.
More information about ‘field’ and ‘tag’
How to add tags or tag_columns into influxDB DataFrameClient.
tags:1
2tags = { "srcaddr": new_record[["srcaddr"]], "srcport": new_record[["srcport"]]}
client.write_points(new_record, 'demo', tags = tags, protocol="json")tag_columns:
1
client.write_points(new_record, 'demo', tag_columns=['srcaddr','srcport'], protocol=protocol)