-
Notifications
You must be signed in to change notification settings - Fork 10
/
custom_query.rs
83 lines (75 loc) · 2.79 KB
/
custom_query.rs
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
use bigtable_rs::bigtable;
use bigtable_rs::bigtable::read_rows::decode_read_rows_response;
use bigtable_rs::google::bigtable::v2::row_filter::{Chain, Filter};
use bigtable_rs::google::bigtable::v2::{ReadRowsRequest, RowFilter, RowSet};
use bigtable_rs::util::get_row_range_from_prefix;
use env_logger;
use std::error::Error;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();
let project_id = "project-1";
let instance_name = "instance-1";
let table_name = "table-1";
let channel_size = 4;
let timeout = Duration::from_secs(10);
let prefix_a: String = "j".to_owned();
let prefix_b: String = "p".to_owned();
// make a bigtable client
let connection = bigtable::BigTableConnection::new(
project_id,
instance_name,
true,
channel_size,
Some(timeout),
)
.await?;
let mut bigtable = connection.client();
// prepare a ReadRowsRequest with fully customized query
let request = ReadRowsRequest {
app_profile_id: "default".to_owned(),
table_name: bigtable.get_full_table_name(table_name),
rows_limit: 20,
rows: Some(RowSet {
row_keys: vec![],
row_ranges: vec![
get_row_range_from_prefix(prefix_a.into_bytes()),
get_row_range_from_prefix(prefix_b.into_bytes()),
],
}),
filter: Some(RowFilter {
filter: Some(Filter::Chain(Chain {
filters: vec![
RowFilter {
filter: Some(Filter::FamilyNameRegexFilter("cf1".to_owned())),
},
RowFilter {
filter: Some(Filter::ColumnQualifierRegexFilter("c1".as_bytes().to_vec())),
},
RowFilter {
filter: Some(Filter::CellsPerColumnLimitFilter(1)),
},
],
})),
}),
..ReadRowsRequest::default()
};
// calling bigtable API to get results using inner client and customized query
let response_stream = bigtable.get_client().read_rows(request).await?.into_inner();
let response = decode_read_rows_response(&Some(timeout), response_stream).await?;
// simply print results for example usage
response.into_iter().for_each(|(key, data)| {
println!("------------\n{}", String::from_utf8(key.clone()).unwrap());
data.into_iter().for_each(|row_cell| {
println!(
" [{}:{}] \"{}\" @ {}",
row_cell.family_name,
String::from_utf8(row_cell.qualifier).unwrap(),
String::from_utf8(row_cell.value).unwrap(),
row_cell.timestamp_micros
)
})
});
Ok(())
}