Python
Introduction
horaedb-client is the python client for HoraeDB.
Thanks to PyO3, the python client is actually a wrapper on the rust client.
The guide will give a basic introduction to the python client by example.
Requirements
- Python >= 3.7
Installation
|
|
You can get latest version here.
Init HoraeDB Client
The client initialization comes first, here is a code snippet:
|
|
Firstly, it’s worth noting that the imported packages are used across all the code snippets in this guide, and they will not be repeated in the following.
The initialization requires at least two parameters:
Endpoint
: the server endpoint consisting of ip address and serving port, e.g.127.0.0.1:8831
;Mode
: The mode of the communication between client and server, and there are two kinds ofMode
:Direct
andProxy
.
Endpoint
is simple, while Mode
deserves more explanation. The Direct
mode should be adopted to avoid forwarding overhead if all the servers are accessible to the client. However, the Proxy
mode is the only choice if the access to the servers from the client must go through a gateway.
The default_database
can be set and will be used if following rpc calling without setting the database in the RpcContext
.
By configuring the RpcConfig
, resource and performance of the client can be manipulated, and all of the configurations can be referred at here.
Create Table
For ease of use, when using gRPC’s write interface for writing, if a table does not exist, HoraeDB will automatically create a table based on the first write.
Of course, you can also use create table
statement to manage the table more finely (such as adding indexes).
Here is a example for creating table by the initialized client:
|
|
RpcContext
can be used to overwrite the default database and timeout defined in the initialization of the client.
Write Data
PointBuilder
can be used to construct a point, which is actually a row in data set. The write request consists of multiple points.
The example is simple:
|
|
Query Data
By sql_query
interface, it is easy to retrieve the data from the server:
req = SqlQueryRequest(['demo'], 'select * from demo')
event_loop = asyncio.get_event_loop()
resp = event_loop.run_until_complete(async_query(client, ctx, req))
As the example shows, two parameters are needed to construct the SqlQueryRequest
:
- The tables involved by this sql query;
- The query sql.
Currently, the first parameter is necessary for performance on routing.
With retrieved data, we can process it row by row and column by column:
|
|
Drop Table
Finally, we can drop the table by the sql api, which is similar to the table creation:
|
|