0

我有一个要求,因为 Redis-Stream 结构包含specified length of data我希望的一个transfer to influxDB in bulk,但是在传输过程中,批量插入的数据不会与原始数据的顺序相同

数据生成代码示例如下所示

try:
    rs = redis.Redis(host="localhost", port=6379, db=0)
except Exception as e:
    print(e)
count = 0
while(True):
    Ts = int(round(time.time() * 1000))
    count = count + 1
    Count = count

    data = {
        "Ts": Ts,
        "Count":Count
    }
    rs.xadd("simulation", data, maxlen=1000, approximate=False)
    time.sleep(0.1)

数据读取和保存代码示例如下所示

def parse(datas):
    ts,data = datas
    w_json = {
    "measurement": 'test1',
    "tags": {
        'Ts': data[b'Ts'],
        },
    "fields": {
        "Count":data[b'Count'].decode('utf-8'),
        'Ts': data[b'Ts']
        }
    }
    return w_json


try:
    rs = redis.Redis(host="localhost", port=6379, db=0)
except Exception as e:
    print(e)
client = InfluxDBClient(host="localhost", port=8086,database='test')

results = rs.xrange("simulation",count=1000)

influxdb_data = list(map(parse,results))
client.write_points(influxdb_data,batch_size=500)
print('insert success')

当我查看数据时,您会看到 Count 数据顺序未按该顺序输出

> select Count,Ts from test1 limit 30;
name: sensor1
time                Count Ts
----                ----- --
1594003971238615143 1     1594003959534
1594003971238615143 10    1594003960446
1594003971238615143 100   1594003969537
1594003971238615143 101   1594003969638
1594003971238615143 102   1594003969739
1594003971238615143 103   1594003969840
1594003971238615143 104   1594003969941
1594003971238615143 105   1594003970042
1594003971238615143 106   1594003970143
1594003971238615143 107   1594003970244
1594003971238615143 108   1594003970345
1594003971238615143 109   1594003970446
1594003971238615143 11    1594003960547
1594003971238615143 110   1594003970547
1594003971238615143 111   1594003970648
1594003971238615143 112   1594003970749
1594003971238615143 12    1594003960648
1594003971238615143 13    1594003960749
1594003971238615143 14    1594003960850
1594003971238615143 15    1594003960951
1594003971238615143 16    1594003961053
1594003971238615143 17    1594003961154
1594003971238615143 18    1594003961255
1594003971238615143 19    1594003961356
1594003971238615143 2     1594003959638
1594003971238615143 20    1594003961457
1594003971238615143 21    1594003961558
1594003971238615143 22    1594003961659
1594003971238615143 23    1594003961760
1594003971238615143 24    1594003961861

有没有办法实现顺序批量插入?

如果您能告诉我如何解决它,我将不胜感激?

4

0 回答 0