|
1 | | -from questdb.ingress import Sender |
| 1 | +from questdb.ingress import Sender, IngressError |
| 2 | +import sys |
| 3 | +import datetime |
2 | 4 |
|
3 | 5 |
|
4 | 6 | def example(host: str = 'localhost', port: int = 9009): |
5 | | - # See: https://questdb.io/docs/reference/api/ilp/authenticate |
6 | | - auth = ( |
7 | | - "testUser1", # kid |
8 | | - "5UjEMuA0Pj5pjK8a-fa24dyIf-Es5mYny3oE_Wmus48", # d |
9 | | - "fLKYEaoEb9lrn3nkwLDA-M_xnuFOdSt9y0Z7_vWSHLU", # x |
10 | | - "Dt5tbS1dEDMSYfym3fgMv0B99szno-dFc1rYF9t0aac") # y |
11 | | - with Sender(host, port, auth=auth, tls=True) as sender: |
12 | | - sender.row( |
13 | | - 'line_sender_example', |
14 | | - symbols={'id': 'OMEGA'}, |
15 | | - columns={'price': 111222233333, 'qty': 3.5}) |
16 | | - sender.row( |
17 | | - 'line_sender_example', |
18 | | - symbols={'id': 'ZHETA'}, |
19 | | - columns={'price': 111222233330, 'qty': 2.5}) |
20 | | - sender.flush() |
| 7 | + try: |
| 8 | + # See: https://questdb.io/docs/reference/api/ilp/authenticate |
| 9 | + auth = ( |
| 10 | + "testUser1", # kid |
| 11 | + "5UjEMuA0Pj5pjK8a-fa24dyIf-Es5mYny3oE_Wmus48", # d |
| 12 | + "fLKYEaoEb9lrn3nkwLDA-M_xnuFOdSt9y0Z7_vWSHLU", # x |
| 13 | + "Dt5tbS1dEDMSYfym3fgMv0B99szno-dFc1rYF9t0aac") # y |
| 14 | + with Sender(host, port, auth=auth, tls=True) as sender: |
| 15 | + # Record with provided designated timestamp (using the 'at' param) |
| 16 | + # Notice the designated timestamp is expected in Nanoseconds, |
| 17 | + # but timestamps in other columns are expected in Microseconds. |
| 18 | + # The API provides convenient functions |
| 19 | + sender.row( |
| 20 | + 'trades', |
| 21 | + symbols={ |
| 22 | + 'pair': 'USDGBP', |
| 23 | + 'type': 'buy'}, |
| 24 | + columns={ |
| 25 | + 'traded_price': 0.83, |
| 26 | + 'limit_price': 0.84, |
| 27 | + 'qty': 100, |
| 28 | + 'traded_ts': datetime.datetime( |
| 29 | + 2022, 8, 6, 7, 35, 23, 189062, |
| 30 | + tzinfo=datetime.timezone.utc)}, |
| 31 | + at=datetime.datetime.utcnow()) |
| 32 | + |
| 33 | + # If no 'at' param is passed, the server will use its own timestamp. |
| 34 | + sender.row( |
| 35 | + 'trades', |
| 36 | + symbols={'pair': 'EURJPY'}, |
| 37 | + columns={ |
| 38 | + 'traded_price': 135.97, |
| 39 | + 'qty': 400, |
| 40 | + 'limit_price': None}) # NULL columns can be passed as None, |
| 41 | + # or simply be left out. |
| 42 | + |
| 43 | + # We recommend flushing periodically, for example every few seconds. |
| 44 | + # If you don't flush explicitly, the server will flush automatically |
| 45 | + # once the buffer is reaches 63KiB and just before the connection |
| 46 | + # is closed. |
| 47 | + sender.flush() |
| 48 | + |
| 49 | + except IngressError as e: |
| 50 | + sys.stderr.write(f'Got error: {e}\n') |
21 | 51 |
|
22 | 52 |
|
23 | 53 | if __name__ == '__main__': |
|
0 commit comments