-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathmain.rs
More file actions
96 lines (87 loc) · 3.03 KB
/
main.rs
File metadata and controls
96 lines (87 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use std::env;
const FIELD_VALUE_SIZE_ENV: &str = "FIELD_VALUE_SIZE";
const COLUMN_FAMILIES_ENV: &str = "COLUMN_FAMILIES";
const COLUMNS_PER_FAMILI_ENV: &str = "COLUMNS_PER_FAMILY";
const DELIMITER: &str = "|";
const VALUE_CHAR: &str = "0";
fn main() {
let field_value_size: usize = get_env_var_u64(FIELD_VALUE_SIZE_ENV) as usize;
let column_families: u64 = get_env_var_u64(COLUMN_FAMILIES_ENV);
let columns_per_family: u64 = get_env_var_u64(COLUMNS_PER_FAMILI_ENV);
let field_value: String = VALUE_CHAR.repeat(field_value_size);
let (value_schema, value) = if column_families == 0 {
string_schema_and_value(field_value)
} else {
struct_schema_and_value(column_families, columns_per_family, field_value)
};
let schema_and_value: String = format!("{{\"schema\":{},\"payload\":{}}}", value_schema, value);
let mut i: u64 = 0;
loop {
println!("\"{}\"{}{}", i, DELIMITER, schema_and_value);
i += 1;
}
}
fn get_env_var_u64(name: &str) -> u64 {
let missing_error: String = format!("Missing env var: {}.", name);
let invalid_error: String = format!("Non-u64 env var: {}.", name);
env::var(name)
.expect(&missing_error)
.parse::<u64>()
.expect(&invalid_error)
}
fn struct_schema_and_value(
column_families: u64,
columns_per_family: u64,
field_value: String,
) -> (String, String) {
let columns: Vec<String> = (1..=columns_per_family)
.map(|i| format!("c{}", i))
.collect();
let column_families: Vec<String> = (1..=column_families).map(|i| format!("cf{}", i)).collect();
let column_family_schemas: Vec<String> = column_families
.iter()
.map(|cf| {
format!(
"{{\"type\":\"struct\",\"optional\":true,\"field\":\"{}\",\"fields\":[{}]}}",
cf,
columns
.iter()
.map(|c| format!(
"{{\"type\":\"string\",\"optional\":true,\"field\":\"{}\"}}",
c
))
.collect::<Vec<String>>()
.join(",")
)
})
.collect();
let schema: String = format!(
"{{\"name\":\"record\",\"type\":\"struct\",\"optional\":true,\"fields\":[{}]}}",
column_family_schemas.join(",")
);
let value: String = format!(
"{{{}}}",
column_families
.iter()
.map(|cf| {
format!(
"\"{}\":{{{}}}",
cf,
columns
.iter()
.map(|c| format!("\"{}\":\"{}\"", c, field_value))
.collect::<Vec<String>>()
.join(",")
)
})
.collect::<Vec<String>>()
.join(",")
);
(schema, value)
}
fn string_schema_and_value(field_value: String) -> (String, String) {
(
"{\"type\":\"string\",\"optional\":true}".to_string(),
format!("\"{}\"", field_value),
)
}