Rust
Install
cargo add ceresdb-client
You can get latest version here.
Init Client
At first, we need to init the client.
- New builder for the client, and you must set
endpoint
andmode
:endpoint
is a string which is usually like "ip/domain_name:port".mode
is used to define the way to access horaedb server, detail about mode.
#![allow(unused)] fn main() { let mut builder = Builder::new("ip/domain_name:port", Mode::Direct/Mode::Proxy); }
- New and set
rpc_config
, it can be defined on demand or just use the default value, detail about rpc config:
#![allow(unused)] fn main() { let rpc_config = RpcConfig { thread_num: Some(1), default_write_timeout: Duration::from_millis(1000), ..Default::default() }; let builder = builder.rpc_config(rpc_config); }
- Set
default_database
, it will be used if following rpc calling without setting the database in theRpcContext
(will be introduced in later):
#![allow(unused)] fn main() { let builder = builder.default_database("public"); }
- Finally, we build client from builder:
#![allow(unused)] fn main() { let client = 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:
#![allow(unused)] fn main() { let create_table_sql = r#"CREATE TABLE IF NOT EXISTS ceresdb ( 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')"#; let req = SqlQueryRequest { tables: vec!["ceresdb".to_string()], sql: create_table_sql.to_string(), }; let resp = client .sql_query(rpc_ctx, &req) .await .expect("Should succeed to create table"); }
- Drop table:
#![allow(unused)] fn main() { let drop_table_sql = "DROP TABLE ceresdb"; let req = SqlQueryRequest { tables: vec!["ceresdb".to_string()], sql: drop_table_sql.to_string(), }; let resp = 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 byPointBuilder
, the related data structure oftag value
andfield value
in it is defined asValue
, detail about Value:
#![allow(unused)] fn main() { let test_table = "ceresdb"; let ts = Local::now().timestamp_millis(); let point = PointBuilder::new(test_table.to_string()) .timestamp(ts) .tag("str_tag".to_string(), Value::String("tag_val".to_string())) .tag("int_tag".to_string(), Value::Int32(42)) .tag( "var_tag".to_string(), Value::Varbinary(b"tag_bin_val".to_vec()), ) .field( "str_field".to_string(), Value::String("field_val".to_string()), ) .field("int_field".to_string(), Value::Int32(42)) .field( "bin_field".to_string(), Value::Varbinary(b"field_bin_val".to_vec()), ) .build() .unwrap(); }
- Add the
point
towrite request
:
#![allow(unused)] fn main() { let mut write_req = WriteRequest::default(); write_req.add_point(point); }
-
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.
#![allow(unused)] fn main() { let rpc_ctx = RpcContext { database: Some("public".to_string()), ..Default::default() }; let resp = 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
:
#![allow(unused)] fn main() { let req = SqlQueryRequest { tables: vec![table name 1,...,table name n], sql: sql string (e.g. select * from xxx), }; }
- Query by client:
#![allow(unused)] fn main() { let resp = client.sql_query(rpc_ctx, &req).await.expect("Should success to write"); }
Example
You can find the complete example in the project.