Examples¶
Basics¶
The following example connects to the database and sends two rows (lines).
The connection is unauthenticated and the data is sent at the end of the
with
block.
Here the questdb.ingress.Sender
is constructed with just host
and
port
.
from questdb.ingress import Sender
def example():
with Sender('localhost', 9009) as sender:
sender.row(
'line_sender_example',
symbols={'id': 'OMEGA'},
columns={'price': 111222233333, 'qty': 3.5})
sender.row(
'line_sender_example',
symbols={'id': 'ZHETA'},
columns={'price': 111222233330, 'qty': 2.5})
sender.flush()
if __name__ == '__main__':
example()
Authentication and TLS¶
Continuing from the previous example, the connection is authenticated and also uses TLS.
Here the questdb.ingress.Sender
is also constructed with the auth
and tls
arguments.
from questdb.ingress import Sender
def example():
# See: https://questdb.io/docs/reference/api/ilp/authenticate
auth = (
"testUser1", # kid
"5UjEMuA0Pj5pjK8a-fa24dyIf-Es5mYny3oE_Wmus48", # d
"fLKYEaoEb9lrn3nkwLDA-M_xnuFOdSt9y0Z7_vWSHLU", # x
"Dt5tbS1dEDMSYfym3fgMv0B99szno-dFc1rYF9t0aac") # y
with Sender('localhost', 9009, auth=auth, tls=True) as sender:
sender.row(
'line_sender_example',
symbols={'id': 'OMEGA'},
columns={'price': 111222233333, 'qty': 3.5})
sender.row(
'line_sender_example',
symbols={'id': 'ZHETA'},
columns={'price': 111222233330, 'qty': 2.5})
sender.flush()
if __name__ == '__main__':
example()
Explicit Buffers¶
For more advanced use cases where the same messages need to be sent to multiple
questdb instances or you want to decouple serialization and sending (as may be
in a multi-threaded application) construct :class:questdb.ingress.Buffer
objects explicitly, then pass them to the :func:questdb.ingress.Sender.flush
method.
Note that this bypasses auto-flush
logic
(see questdb.ingress.Sender
) and you are fully responsible for ensuring
all data is sent.
from questdb.ingress import Sender, TimestampNanos
def example():
with Sender('localhost', 9009) as sender:
buffer = sender.new_buffer()
buffer.row(
'line_sender_buffer_example',
symbols={'id': 'Hola'},
columns={'price': 111222233333, 'qty': 3.5},
at=TimestampNanos(111222233333))
buffer.row(
'line_sender_example',
symbols={'id': 'Adios'},
columns={'price': 111222233343, 'qty': 2.5},
at=TimestampNanos(111222233343))
sender.flush(buffer)
if __name__ == '__main__':
example()