|
| 1 | +// Copyright 2023 Greptime Team |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#![allow(clippy::print_stderr)] |
| 16 | +#![allow(clippy::print_stdout)] |
| 17 | + |
| 18 | +mod config_utils; |
| 19 | +use config_utils::DbConfig; |
| 20 | + |
| 21 | +use greptimedb_ingester::api::v1::*; |
| 22 | +use greptimedb_ingester::helpers::schema::*; |
| 23 | +use greptimedb_ingester::helpers::values::*; |
| 24 | +use greptimedb_ingester::{ClientBuilder, Database, Result}; |
| 25 | + |
| 26 | +/// Example of sensor data insertion using low-level API |
| 27 | +pub async fn low_level_sensor_ingest() -> Result<()> { |
| 28 | + let config = DbConfig::from_env(); |
| 29 | + |
| 30 | + let grpc_client = ClientBuilder::default() |
| 31 | + .peers(vec![&config.endpoint]) |
| 32 | + .build(); |
| 33 | + |
| 34 | + let database = Database::new_with_dbname(&config.database, grpc_client); |
| 35 | + |
| 36 | + let sensor_schema = build_sensor_schema(); |
| 37 | + |
| 38 | + let sensor_data = build_sensor_data(); |
| 39 | + |
| 40 | + let insert_request = RowInsertRequests { |
| 41 | + inserts: vec![RowInsertRequest { |
| 42 | + table_name: "sensor_readings".to_owned(), |
| 43 | + rows: Some(Rows { |
| 44 | + schema: sensor_schema, |
| 45 | + rows: sensor_data, |
| 46 | + }), |
| 47 | + }], |
| 48 | + }; |
| 49 | + |
| 50 | + let affected_rows = database.row_insert(insert_request).await?; |
| 51 | + println!("Successfully inserted {} rows", affected_rows); |
| 52 | + |
| 53 | + Ok(()) |
| 54 | +} |
| 55 | + |
| 56 | +/// Use schema helpers to build column definitions for sensor table |
| 57 | +fn build_sensor_schema() -> Vec<ColumnSchema> { |
| 58 | + vec![ |
| 59 | + // Tag columns - for indexing and grouping |
| 60 | + tag("device_id", ColumnDataType::String), |
| 61 | + tag("location", ColumnDataType::String), |
| 62 | + // Timestamp column - timeline for time series data |
| 63 | + timestamp("timestamp", ColumnDataType::TimestampMillisecond), |
| 64 | + // Field columns - actual measurement values |
| 65 | + field("temperature", ColumnDataType::Float64), |
| 66 | + field("humidity", ColumnDataType::Float64), |
| 67 | + field("pressure", ColumnDataType::Float64), |
| 68 | + field("battery_level", ColumnDataType::Int32), |
| 69 | + field("is_online", ColumnDataType::Boolean), |
| 70 | + ] |
| 71 | +} |
| 72 | + |
| 73 | +/// Use value helpers to build sensor data rows |
| 74 | +fn build_sensor_data() -> Vec<Row> { |
| 75 | + let mut rows = Vec::new(); |
| 76 | + |
| 77 | + rows.push(Row { |
| 78 | + values: vec![ |
| 79 | + string_value("sensor_001".to_string()), |
| 80 | + string_value("building_a_floor_1".to_string()), |
| 81 | + timestamp_millisecond_value(1748500685000), |
| 82 | + f64_value(23.5), |
| 83 | + f64_value(65.2), |
| 84 | + f64_value(1013.25), |
| 85 | + i32_value(85), |
| 86 | + bool_value(true), |
| 87 | + ], |
| 88 | + }); |
| 89 | + |
| 90 | + rows.push(Row { |
| 91 | + values: vec![ |
| 92 | + string_value("sensor_002".to_string()), |
| 93 | + string_value("building_a_floor_2".to_string()), |
| 94 | + timestamp_millisecond_value(1748500685000), |
| 95 | + f64_value(24.1), |
| 96 | + f64_value(62.8), |
| 97 | + f64_value(1012.80), |
| 98 | + i32_value(78), |
| 99 | + bool_value(true), |
| 100 | + ], |
| 101 | + }); |
| 102 | + |
| 103 | + rows.push(Row { |
| 104 | + values: vec![ |
| 105 | + string_value("sensor_003".to_string()), |
| 106 | + string_value("building_b_floor_1".to_string()), |
| 107 | + timestamp_millisecond_value(1748500685000), |
| 108 | + f64_value(22.8), |
| 109 | + f64_value(68.5), |
| 110 | + f64_value(1014.10), |
| 111 | + i32_value(15), |
| 112 | + bool_value(false), |
| 113 | + ], |
| 114 | + }); |
| 115 | + |
| 116 | + rows |
| 117 | +} |
| 118 | + |
| 119 | +/// Demonstrate batch insertion of different data types |
| 120 | +pub async fn low_level_mixed_data_ingest() -> Result<()> { |
| 121 | + let config = DbConfig::from_env(); |
| 122 | + |
| 123 | + let grpc_client = ClientBuilder::default() |
| 124 | + .peers(vec![&config.endpoint]) |
| 125 | + .build(); |
| 126 | + let database = Database::new_with_dbname(&config.database, grpc_client); |
| 127 | + |
| 128 | + // Build table with various data types |
| 129 | + let mixed_schema = vec![ |
| 130 | + tag("category", ColumnDataType::String), |
| 131 | + timestamp("event_time", ColumnDataType::TimestampNanosecond), |
| 132 | + field("int8_val", ColumnDataType::Int8), |
| 133 | + field("int16_val", ColumnDataType::Int16), |
| 134 | + field("int64_val", ColumnDataType::Int64), |
| 135 | + field("uint32_val", ColumnDataType::Uint32), |
| 136 | + field("float32_val", ColumnDataType::Float32), |
| 137 | + field("binary_data", ColumnDataType::Binary), |
| 138 | + field("date_val", ColumnDataType::Date), |
| 139 | + ]; |
| 140 | + |
| 141 | + let mixed_data = vec![Row { |
| 142 | + values: vec![ |
| 143 | + string_value("test_category".to_string()), |
| 144 | + timestamp_nanosecond_value(1748500685000000000), |
| 145 | + i8_value(127), |
| 146 | + i16_value(32767), |
| 147 | + i64_value(9223372036854775807), |
| 148 | + u32_value(4294967295), |
| 149 | + f32_value(std::f32::consts::PI), |
| 150 | + binary_value(vec![0x48, 0x65, 0x6c, 0x6c, 0x6f]), |
| 151 | + date_value(19358), |
| 152 | + ], |
| 153 | + }]; |
| 154 | + |
| 155 | + let insert_request = RowInsertRequests { |
| 156 | + inserts: vec![RowInsertRequest { |
| 157 | + table_name: "mixed_data_table".to_string(), |
| 158 | + rows: Some(Rows { |
| 159 | + schema: mixed_schema, |
| 160 | + rows: mixed_data, |
| 161 | + }), |
| 162 | + }], |
| 163 | + }; |
| 164 | + |
| 165 | + let affected_rows = database.row_insert(insert_request).await?; |
| 166 | + println!("Mixed data insert: {} rows affected", affected_rows); |
| 167 | + |
| 168 | + Ok(()) |
| 169 | +} |
| 170 | + |
| 171 | +#[tokio::main] |
| 172 | +async fn main() -> Result<()> { |
| 173 | + println!("Starting low-level ingestion examples..."); |
| 174 | + |
| 175 | + low_level_sensor_ingest().await?; |
| 176 | + |
| 177 | + low_level_mixed_data_ingest().await?; |
| 178 | + |
| 179 | + println!("All examples completed successfully!"); |
| 180 | + Ok(()) |
| 181 | +} |
0 commit comments