Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use clickhouse rs to add datetime32 data? #182

Open
trollhe opened this issue Oct 31, 2022 · 1 comment
Open

How to use clickhouse rs to add datetime32 data? #182

trollhe opened this issue Oct 31, 2022 · 1 comment

Comments

@trollhe
Copy link

trollhe commented Oct 31, 2022

table :

drop table if exists tmp.tmp_demo;
CREATE TABLE if not exists tmp.tmp_demo
(
    `p_time` DateTime
)
ENGINE = MergeTree
PARTITION BY p_time
ORDER BY p_time
TTL p_time + toIntervalHour(1)
SETTINGS index_granularity = 8192;

codes:

use std::{env, error::Error};
use clickhouse_rs::{row, types::Block, Pool};
use futures_util::StreamExt;
use clickhouse_rs::types::DateTimeType;

#[cfg(all(feature = "tokio_io"))]
#[tokio::main]
async fn main()-> Result<(), Box<dyn Error>>{
    // table name
    let table_name:&str = "tmp.tmp_demo";

    // clickhouse url
    let ch_url: &str = "tcp://default:abc123@localhost:9000/tmp?compression=lz4&ping_timeout=42ms";

    // get client
    let pool = Pool::new(ch_url);
    let mut client = pool.get_handle().await?;

    // assembly data
    let block = Block::new()
     .column("p_time",  vec![1667210027_u32,1667210027_u32]);

    // insert data
    client.insert(table_name, block).await?;
    Ok(())

}

error logs:

Error: FromSql(InvalidType { src: "UInt32", dst: "DateTime" })
@prk3
Copy link
Contributor

prk3 commented Jan 14, 2023

You can't insert integer timestampts into a column of type DateTime. Convert timestamps into chrono::DateTime objects first, like this:

use chrono_tz::Tz::UTC;

let block = Block::new().column(
    "p_time",
    vec![
        UTC.timestamp_opt(1667210027_i64, 0).unwrap(),
        UTC.timestamp_opt(1667210027_i64, 0).unwrap(),
    ],
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants