|
22 | 22 | //!
|
23 | 23 | //! ```toml
|
24 | 24 | //! [dependencies]
|
25 |
| -//! iotdb = "0.0.6" |
| 25 | +//! iotdb = "0.0.7" |
| 26 | +//! simplelog = "0.11.0" |
26 | 27 | //! ```
|
27 | 28 | //!
|
28 | 29 | //! # Example
|
29 | 30 | //!
|
30 | 31 | //! ```rust
|
31 | 32 | //! use chrono::Local;
|
32 |
| -//! use thrift::Error; |
33 | 33 | //!
|
34 |
| -//! use iotdb::common::{Compressor, DataType, Encoding}; |
35 |
| -//! use iotdb::{ConfigBuilder, Session}; |
| 34 | +//! use iotdb::*; |
36 | 35 | //!
|
37 |
| -//! fn main() -> Result<(), Error> { |
38 |
| -//! let config = ConfigBuilder::new() |
39 |
| -//! .endpoint("127.0.0.1", "6667") |
| 36 | +//! fn main() -> Result<(), anyhow::Error> { |
| 37 | +//! debug(false); |
| 38 | +//! |
| 39 | +//! let config = iotdb::ConfigBuilder::new() |
| 40 | +//! .endpoint("localhost:6667") |
40 | 41 | //! .user("root")
|
41 | 42 | //! .password("root")
|
42 |
| -//! .zone_id("UTC+8") |
43 |
| -//! .debug(true) |
| 43 | +//! .time_zone("UTC+8") |
44 | 44 | //! .build();
|
45 | 45 | //!
|
46 |
| -//! // open session |
47 |
| -//! let mut session = Session::new(config).open()?; |
| 46 | +//! // open session |
| 47 | +//! let mut session = Session::connect(config)?; |
48 | 48 | //! println!("time_zone: {}", session.time_zone()?);
|
49 | 49 | //! session.delete_storage_group("root.ln")?;
|
50 | 50 | //! session.set_storage_group("root.ln")?;
|
51 | 51 | //! session.create_time_series(
|
52 | 52 | //! "root.ln.wf01.wt01.temperature",
|
53 |
| -//! DataType::INT64, |
| 53 | +//! DataType::FLOAT, |
54 | 54 | //! Encoding::default(),
|
55 | 55 | //! Compressor::default(),
|
56 | 56 | //! )?;
|
57 | 57 | //!
|
58 | 58 | //! session.create_time_series(
|
59 |
| -//! "root.ln.wf01.wt01.humidity", |
60 |
| -//! DataType::INT64, |
| 59 | +//! "root.ln.wf01.wt01.status", |
| 60 | +//! DataType::BOOLEAN, |
61 | 61 | //! Encoding::default(),
|
62 | 62 | //! Compressor::default(),
|
63 | 63 | //! )?;
|
64 | 64 | //!
|
65 |
| -//! let now = Local::now().timestamp(); |
| 65 | +//! let now = Local::now().timestamp_millis(); |
66 | 66 | //! session.sql(
|
67 | 67 | //! format!(
|
68 | 68 | //! "INSERT INTO root.ln.wf01.wt01(timestamp,status) values({},true)",
|
|
91 | 91 | //! )
|
92 | 92 | //! .as_str(),
|
93 | 93 | //! )?;
|
94 |
| -//! session.sql("select * from root.ln")?.show(); |
95 |
| -//! session.close()?; |
| 94 | +//! session.sql("select * from root.ln")?.show(); |
| 95 | +//! |
| 96 | +//! // DF (TODO) |
| 97 | +//! let df = session.sql("select * from root.ln")?.to_df()?; |
| 98 | +//! println!("IoTDB DF is empty: {}", df.is_empty()); |
| 99 | +//! |
| 100 | +//! session.close()?; |
| 101 | +//! |
| 102 | +//! Ok(()) |
| 103 | +//! } |
96 | 104 | //!
|
97 |
| -//! Ok(()) |
| 105 | +//! fn debug(enable: bool) { |
| 106 | +//! use simplelog::*; |
| 107 | +//! let mut log_level = LevelFilter::Info; |
| 108 | +//! if enable { |
| 109 | +//! log_level = LevelFilter::Debug; |
| 110 | +//! } |
| 111 | +//! let _ = CombinedLogger::init(vec![TermLogger::new( |
| 112 | +//! log_level, |
| 113 | +//! Default::default(), |
| 114 | +//! TerminalMode::Mixed, |
| 115 | +//! ColorChoice::Auto, |
| 116 | +//! )]); |
98 | 117 | //! }
|
99 | 118 | //! ```
|
100 | 119 | #[macro_use]
|
101 | 120 | extern crate prettytable;
|
102 | 121 |
|
103 |
| -pub use chrono; |
104 |
| -pub use polars; |
105 | 122 | use std::collections::BTreeMap;
|
106 | 123 | use std::net::TcpStream;
|
107 | 124 | use std::str::FromStr;
|
108 |
| -pub use thrift; |
109 | 125 |
|
110 | 126 | use anyhow::bail;
|
| 127 | +pub use chrono; |
111 | 128 | use chrono::{Local, Utc};
|
112 | 129 | use log::{debug, error, info};
|
113 | 130 | use mimalloc::MiMalloc;
|
114 |
| -use thrift::protocol::{ |
115 |
| - TBinaryInputProtocol, TBinaryOutputProtocol, TCompactInputProtocol, TCompactOutputProtocol, |
116 |
| - TInputProtocol, TOutputProtocol, |
117 |
| -}; |
118 |
| -use thrift::transport::{TFramedReadTransport, TFramedWriteTransport, TIoChannel, TTcpChannel}; |
| 131 | +pub use polars; |
| 132 | +pub use thrift; |
| 133 | +use thrift::protocol::*; |
| 134 | +use thrift::transport::*; |
119 | 135 |
|
120 | 136 | use crate::ds::DataSet;
|
121 |
| -pub use crate::ds::*; |
122 |
| -use crate::rpc::{ |
123 |
| - TSCancelOperationReq, TSCloseSessionReq, TSCreateMultiTimeseriesReq, TSCreateTimeseriesReq, |
124 |
| - TSDeleteDataReq, TSExecuteBatchStatementReq, TSExecuteStatementReq, TSExecuteStatementResp, |
125 |
| - TSIServiceSyncClient, TSInsertRecordReq, TSInsertRecordsReq, TSInsertStringRecordsReq, |
126 |
| - TSInsertTabletReq, TSInsertTabletsReq, TSOpenSessionReq, TSOpenSessionResp, TSProtocolVersion, |
127 |
| - TSRawDataQueryReq, TSSetTimeZoneReq, TSStatus, TTSIServiceSyncClient, |
128 |
| -}; |
| 137 | +use crate::rpc::*; |
129 | 138 |
|
130 | 139 | mod ds;
|
131 | 140 | mod errors;
|
@@ -345,6 +354,7 @@ impl Into<i32> for Compressor {
|
345 | 354 | }
|
346 | 355 | }
|
347 | 356 |
|
| 357 | +/// Session Endpoint |
348 | 358 | #[derive(Clone, Debug)]
|
349 | 359 | pub struct Endpoint {
|
350 | 360 | pub host: String,
|
@@ -421,6 +431,7 @@ impl Default for Config {
|
421 | 431 | }
|
422 | 432 | }
|
423 | 433 |
|
| 434 | +/// IotDB Config Builder |
424 | 435 | pub struct ConfigBuilder(Config);
|
425 | 436 |
|
426 | 437 | impl Default for ConfigBuilder {
|
@@ -499,6 +510,7 @@ impl ConfigBuilder {
|
499 | 510 | }
|
500 | 511 | }
|
501 | 512 |
|
| 513 | +/// IotDB Session |
502 | 514 | pub struct Session {
|
503 | 515 | client: ClientType,
|
504 | 516 | config: Config,
|
@@ -574,7 +586,7 @@ impl Session {
|
574 | 586 | })
|
575 | 587 | }
|
576 | 588 | } else {
|
577 |
| - let msg = format!("{}", status.message.unwrap_or_else(|| "None".to_string())); |
| 589 | + let msg = status.message.unwrap_or_else(|| "None".to_string()); |
578 | 590 | error!("{}", msg);
|
579 | 591 | bail!(msg)
|
580 | 592 | }
|
@@ -635,7 +647,7 @@ impl Session {
|
635 | 647 | /// Delete a storage group.
|
636 | 648 | pub fn delete_storage_group(&mut self, storage_group: &str) -> anyhow::Result<()> {
|
637 | 649 | debug!("Delete storage group {:?}", storage_group);
|
638 |
| - Ok(self.delete_storage_groups(vec![storage_group.to_string()])?) |
| 650 | + self.delete_storage_groups(vec![storage_group.to_string()]) |
639 | 651 | }
|
640 | 652 |
|
641 | 653 | /// Delete storage groups.
|
@@ -775,14 +787,12 @@ impl Session {
|
775 | 787 | false,
|
776 | 788 | );
|
777 | 789 |
|
778 |
| - match self.client.execute_query_statement(req)? { |
779 |
| - TSExecuteStatementResp { query_data_set, .. } => { |
780 |
| - if query_data_set.is_none() { |
781 |
| - Ok(false) |
782 |
| - } else { |
783 |
| - Ok(query_data_set.unwrap().value_list.is_empty()) |
784 |
| - } |
785 |
| - } |
| 790 | + let TSExecuteStatementResp { query_data_set, .. } = |
| 791 | + self.client.execute_query_statement(req)?; |
| 792 | + if let Some(..) = query_data_set { |
| 793 | + Ok(false) |
| 794 | + } else { |
| 795 | + Ok(query_data_set.unwrap().value_list.is_empty()) |
786 | 796 | }
|
787 | 797 | }
|
788 | 798 |
|
@@ -1109,14 +1119,10 @@ impl Session {
|
1109 | 1119 | let status = resp.clone().status;
|
1110 | 1120 | let msg = status.clone().message.unwrap_or_else(|| "None".to_string());
|
1111 | 1121 | if self.is_success(&status) {
|
1112 |
| - debug!( |
1113 |
| - "Execute statement {:?}, message: {:?}", |
1114 |
| - statement, |
1115 |
| - msg.clone() |
1116 |
| - ); |
| 1122 | + debug!("Execute statement {:?}, message: {:?}", statement, msg); |
1117 | 1123 | Ok(DataSet::new(resp))
|
1118 | 1124 | } else {
|
1119 |
| - error!("{}", msg.clone()); |
| 1125 | + error!("{}", msg); |
1120 | 1126 | bail!(msg)
|
1121 | 1127 | }
|
1122 | 1128 | }
|
|
0 commit comments