Set default_database, it will be used if following rpc calling without setting the database in the RpcContext(will be introduced in later):
1
letbuilder=builder.default_database("public");
Finally, we build client from builder:
1
letclient=builder.build();
Manage 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).
You can use the sql query interface to create or drop table, related setting will be introduced in sql query section.
Create table:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
letcreate_table_sql=r#"CREATE TABLE IF NOT EXISTS horaedb (
str_tag string TAG,
int_tag int32 TAG,
var_tag varbinary TAG,
str_field string,
int_field int32,
bin_field varbinary,
t timestamp NOT NULL,
TIMESTAMP KEY(t)) ENGINE=Analytic with
(enable_ttl='false')"#;letreq=SqlQueryRequest{tables: vec!["horaedb".to_string()],sql: create_table_sql.to_string(),};letresp=client.sql_query(rpc_ctx,&req).await.expect("Should succeed to create table");
Drop table:
1
2
3
4
5
6
7
8
9
10
letdrop_table_sql="DROP TABLE horaedb";letreq=SqlQueryRequest{tables: vec!["horaedb".to_string()],sql: drop_table_sql.to_string(),};letresp=client.sql_query(rpc_ctx,&req).await.expect("Should succeed to create table");
Write
We support to write with the time series data model like InfluxDB.
Build the point first by PointBuilder, the related data structure of tag value and field value in it is defined as Value, detail about Value:
New rpc_ctx, and it can also be defined on demand or just use the default value, detail about rpc ctx:
Finally, write to server by client.
1
2
3
4
5
letrpc_ctx=RpcContext{database: Some("public".to_string()),..Default::default()};letresp=client.write(rpc_ctx,&write_req).await.expect("Should success to write");
Sql Query
We support to query data with sql.
Define related tables and sql in sql query request: