How to insert custom data which is hard to pre-defined
caibirdme opened this issue · comments
I'm writing a kafka consumer, it consumes message and insert those message into clickhouse according to message's meta info, like:
{
"table": "ch_table_1",
"data": [
{"col_name": "foo", "type": "uint32", "val": "3"},
{"col_name": "bar", "type": "string", "val": "hello"}
//...
]
}
How do I construct the Row to insert? Didn't find any docs about this
The ideal way is to use the Row
derive macro if the structure is static. In the recent string of releases, I added RawRow
and UnitValue
structs. RawRow
functions identically to that in tokio-postgres
, while UnitValue
functions the same, but to receive a single statically defined column (i.e. a UInt64
from a count
operation).
Since your format doesn't look static, RawRow
would be the way to go... alas, but I didn't add support to write to the RawRow
. I'll push an update shortly.
I just published v0.7.2
which includes some utility functions to create and write a RawRow
.
It can be used as such:
let mut row = RawRow::default();
row.set("col1", 5u64);
row.set_typed("col2", Type::UInt64, 5u64);
client.insert_into_block("...", vec![row]);
If you want custom column based operations, I don't have a clean API for those yet, but working with the raw Block
type is the way to go.
docs.rs: https://docs.rs/klickhouse/0.7.2/klickhouse/struct.RawRow.html (at the time of posting, docs.rs
has not yet built docs for the version I just published, but this link should work in short order)