xdb如何实现有序批量插入

0vvn1miw  于 2021-06-10  发布在  Redis
关注(0)|答案(0)|浏览(333)

我有一个要求,因为redis流结构 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

有什么方法可以实现顺序批插入吗?
如果你能告诉我怎么解决的话,我会很感激的?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题