@@ -20,24 +20,25 @@ The sender also supports TLS and authentication.
20
20
21
21
conf = ' http::addr=localhost:9000;'
22
22
with Sender.from_conf(conf) as sender:
23
- # One row at a time
23
+ # Adding by rows
24
24
sender.row(
25
- ' weather_sensor ' ,
26
- symbols = {' id ' : ' toronto1 ' },
27
- columns = {' temperature ' : 23.5 , ' humidity ' : 0.49 },
25
+ ' trades ' ,
26
+ symbols = {' symbol ' : ' ETH-USD ' , ' side ' : ' sell ' },
27
+ columns = {' price ' : 2615.54 , ' amount ' : 0.00044 },
28
28
at = TimestampNanos.now())
29
+ # It is highly recommended to auto-flush or to flush in batches,
30
+ # rather than for every row
31
+ sender.flush()
29
32
30
- # Whole dataframes at once - MUCH FASTER
33
+ # Whole dataframes at once
31
34
df = pd.DataFrame({
32
- ' id' : [' dubai2' , ' memphis7' ],
33
- ' temperature' : [41.2 , 33.3 ],
34
- ' humidity' : [0.34 , 0.55 ],
35
- ' timestamp' : [
36
- pd.Timestamp(' 2021-01-01 12:00:00' ),
37
- pd.Timestamp(' 2021-01-01 12:00:01' )
38
- ]
39
- })
40
- sensor.dataframe(' weather_sensor' , df, at = ' timestamp' )
35
+ ' symbol' : pd.Categorical([' ETH-USD' , ' BTC-USD' ]),
36
+ ' side' : pd.Categorical([' sell' , ' sell' ]),
37
+ ' price' : [2615.54 , 39269.98 ],
38
+ ' amount' : [0.00044 , 0.001 ],
39
+ ' timestamp' : pd.to_datetime([' 2021-01-01' , ' 2021-01-02' ])})
40
+
41
+ sensor.dataframe(' trades' , df, at = ' timestamp' )
41
42
42
43
The ``Sender `` object holds an internal buffer which will be flushed and sent
43
44
at when the ``with `` block ends.
@@ -138,13 +139,12 @@ Here is an example of sending a row with a symbol and a string:
138
139
conf = ' http::addr=localhost:9000;'
139
140
with Sender.from_conf(conf) as sender:
140
141
sender.row(
141
- ' news ' ,
142
+ ' trades ' ,
142
143
symbols = {
143
- ' category ' : ' sport ' },
144
+ ' symbol ' : ' ETH-USD ' , ' side ' : ' sell ' },
144
145
columns = {
145
- ' headline' : ' The big game' ,
146
- ' url' : ' https://dailynews.com/sport/the-big-game' ,
147
- ' views' : 1000 },
146
+ ' price' : 2615.54 ,
147
+ ' amount' : 0.00044 }
148
148
at = datetime.datetime(2021 , 1 , 1 , 12 , 0 , 0 ))
149
149
150
150
Populating Timestamps
@@ -184,9 +184,9 @@ received by the server.
184
184
conf = ' http::addr=localhost:9000;'
185
185
with Sender.from_conf(conf) as sender:
186
186
sender.row(
187
- ' weather_sensor ' ,
188
- symbols = {' id ' : ' toronto1 ' },
189
- columns = {' temperature ' : 23.5 , ' humidity ' : 0.49 },
187
+ ' trades ' ,
188
+ symbols = {' symbol ' : ' ETH-USD ' , ' side ' : ' sell ' },
189
+ columns = {' price ' : 2615.54 , ' amount ' : 0.00044 },
190
190
at = ServerTimestamp) # Legacy feature, not recommended.
191
191
192
192
.. warning ::
@@ -217,15 +217,15 @@ send any pending data immediately.
217
217
conf = ' http::addr=localhost:9000;'
218
218
with Sender.from_conf(conf) as sender:
219
219
sender.row(
220
- ' weather_sensor ' ,
221
- symbols = {' id ' : ' toronto1 ' },
222
- columns = {' temperature ' : 23.5 , ' humidity ' : 0.49 },
220
+ ' trades ' ,
221
+ symbols = {' symbol ' : ' ETH-USD ' , ' side ' : ' sell ' },
222
+ columns = {' price ' : 2615.54 , ' amount ' : 0.00044 },
223
223
at = TimestampNanos.now())
224
224
sender.flush()
225
225
sender.row(
226
- ' weather_sensor ' ,
227
- symbols = {' id ' : ' dubai2 ' },
228
- columns = {' temperature ' : 41.2 , ' humidity ' : 0.34 },
226
+ ' trades ' ,
227
+ symbols = {' symbol ' : ' BTC-USD ' , ' side ' : ' sell ' },
228
+ columns = {' price ' : 39269.98 , ' amount ' : 0.001 },
229
229
at = TimestampNanos.now())
230
230
sender.flush()
231
231
@@ -276,7 +276,7 @@ When using the HTTP protocol, the server will send back an error message if
276
276
the data is invalid or if there is a problem with the server. This will be
277
277
raised as an :class: `IngressError <questdb.ingress.IngressError> ` exception.
278
278
279
- The HTTP layer will also attempt retries, configurable via the
279
+ The HTTP layer will also attempt retries, configurable via the
280
280
:ref: `retry_timeout <sender_conf_request >` parameter.`
281
281
282
282
When using the TCP protocol errors are *not * sent back from the server and
@@ -299,12 +299,14 @@ rows as a single transaction.
299
299
with Sender.from_conf(conf) as sender:
300
300
with sender.transaction(' weather_sensor' ) as txn:
301
301
txn.row(
302
- symbols = {' id' : ' toronto1' },
303
- columns = {' temperature' : 23.5 , ' humidity' : 0.49 },
302
+ ' trades' ,
303
+ symbols = {' symbol' : ' ETH-USD' , ' side' : ' sell' },
304
+ columns = {' price' : 2615.54 , ' amount' : 0.00044 },
304
305
at = TimestampNanos.now())
305
306
txn.row(
306
- symbols = {' id' : ' dubai2' },
307
- columns = {' temperature' : 41.2 , ' humidity' : 0.34 },
307
+ ' trades' ,
308
+ symbols = {' symbol' : ' BTC-USD' , ' side' : ' sell' },
309
+ columns = {' price' : 39269.98 , ' amount' : 0.001 },
308
310
at = TimestampNanos.now())
309
311
310
312
If auto-flushing is enabled, any pending data will be flushed before the
@@ -385,14 +387,14 @@ buffers.
385
387
386
388
buf = Buffer()
387
389
buf.row(
388
- ' weather_sensor ' ,
389
- symbols = {' id ' : ' toronto1 ' },
390
- columns = {' temperature ' : 23.5 , ' humidity ' : 0.49 },
390
+ ' trades ' ,
391
+ symbols = {' symbol ' : ' ETH-USD ' , ' side ' : ' sell ' },
392
+ columns = {' price ' : 2615.54 , ' amount ' : 0.00044 },
391
393
at = TimestampNanos.now())
392
394
buf.row(
393
- ' weather_sensor ' ,
394
- symbols = {' id ' : ' dubai2 ' },
395
- columns = {' temperature ' : 41.2 , ' humidity ' : 0.34 },
395
+ ' trades ' ,
396
+ symbols = {' symbol ' : ' BTC-USD ' , ' side ' : ' sell ' },
397
+ columns = {' price ' : 39269.98 , ' amount ' : 0.001 },
396
398
at = TimestampNanos.now())
397
399
398
400
conf = ' http::addr=localhost:9000;'
@@ -415,9 +417,9 @@ databases via the ``.flush(buf, clear=False)`` option.
415
417
416
418
buf = Buffer()
417
419
buf.row(
418
- ' weather_sensor ' ,
419
- symbols = {' id ' : ' toronto1 ' },
420
- columns = {' temperature ' : 23.5 , ' humidity ' : 0.49 },
420
+ ' trades ' ,
421
+ symbols = {' symbol ' : ' ETH-USD ' , ' side ' : ' sell ' },
422
+ columns = {' price ' : 2615.54 , ' amount ' : 0.00044 },
421
423
at = TimestampNanos.now())
422
424
423
425
conf1 = ' http::addr=db1.host.com:9000;'
@@ -480,27 +482,25 @@ sender objects in parallel.
480
482
with Sender.from_conf(conf_string) as sender:
481
483
sender.dataframe(
482
484
df,
483
- table_name = ' weather_sensor ' ,
484
- symbols = [' id ' ],
485
+ table_name = ' trades ' ,
486
+ symbols = [' symbol ' , ' side ' ],
485
487
at = ' timestamp' )
486
488
487
489
dfs = [
488
- pd.DataFrame({
489
- ' id' : [' sensor1' , ' sensor2' ],
490
- ' temperature' : [22.5 , 24.7 ],
491
- ' humidity' : [0.45 , 0.47 ],
492
- ' timestamp' : [
493
- pd.Timestamp(' 2017-01-01T12:00:00' ),
494
- pd.Timestamp(' 2017-01-01T12:00:01' )
495
- ]}),
496
- pd.DataFrame({
497
- ' id' : [' sensor3' , ' sensor4' ],
498
- ' temperature' : [23.1 , 25.3 ],
499
- ' humidity' : [0.48 , 0.50 ],
500
- ' timestamp' : [
501
- pd.Timestamp(' 2017-01-01T12:00:02' ),
502
- pd.Timestamp(' 2017-01-01T12:00:03' )
503
- ]})
490
+ pd.DataFrame({
491
+ ' symbol' : pd.Categorical([' ETH-USD' , ' BTC-USD' ]),
492
+ ' side' : pd.Categorical([' sell' , ' sell' ]),
493
+ ' price' : [2615.54 , 39269.98 ],
494
+ ' amount' : [0.00044 , 0.001 ],
495
+ ' timestamp' : pd.to_datetime([' 2021-01-01' , ' 2021-01-02' ])}
496
+ ),
497
+ pd.DataFrame({
498
+ ' symbol' : pd.Categorical([' BTC-USD' , ' BTC-USD' ]),
499
+ ' side' : pd.Categorical([' buy' , ' sell' ]),
500
+ ' price' : [39268.76 , 39270.02 ],
501
+ ' amount' : [0.003 , 0.010 ],
502
+ ' timestamp' : pd.to_datetime([' 2021-01-03' , ' 2021-01-03' ])}
503
+ ),
504
504
]
505
505
506
506
with ThreadPoolExecutor() as executor:
0 commit comments