cargo add ceresdb-client
You can get latest version here.
At first, we need to init the client.
endpoint and mode: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.let mut builder = Builder::new("ip/domain_name:port", Mode::Direct/Mode::Proxy);
rpc_config, it can be defined on demand or just use the default value, detail about rpc config:let rpc_config = RpcConfig { thread_num: Some(1), default_write_timeout: Duration::from_millis(1000), ..Default::default() }; let builder = builder.rpc_config(rpc_config);
default_database, it will be used if following rpc calling without setting the database in the RpcContext(will be introduced in later):let builder = builder.default_database("public");
let client = builder.build();
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.
let create_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')"#; let req = SqlQueryRequest { tables: vec!["horaedb".to_string()], sql: create_table_sql.to_string(), }; let resp = client .sql_query(rpc_ctx, &req) .await .expect("Should succeed to create table");
let drop_table_sql = "DROP TABLE horaedb"; let req = SqlQueryRequest { tables: vec!["horaedb".to_string()], sql: drop_table_sql.to_string(), }; let resp = client .sql_query(rpc_ctx, &req) .await .expect("Should succeed to create table");
We support to write with the time series data model like InfluxDB.
point first by PointBuilder, the related data structure of tag value and field value in it is defined as Value, detail about Value:let test_table = "horaedb"; 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();
point to write request: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.
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");
We support to query data with sql.
sql query request:let req = SqlQueryRequest { tables: vec![table name 1,...,table name n], sql: sql string (e.g. select * from xxx), };
let resp = client.sql_query(rpc_ctx, &req).await.expect("Should success to write");
You can find the complete example in the project.