forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflight_client.rs
79 lines (66 loc) · 3.02 KB
/
flight_client.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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use std::collections::HashMap;
use std::sync::Arc;
use datafusion::arrow::datatypes::Schema;
use arrow_flight::flight_descriptor;
use arrow_flight::flight_service_client::FlightServiceClient;
use arrow_flight::utils::flight_data_to_arrow_batch;
use arrow_flight::{FlightDescriptor, Ticket};
use datafusion::arrow::util::pretty;
/// This example shows how to wrap DataFusion with `FlightService` to support looking up schema information for
/// Parquet files and executing SQL queries against them on a remote server.
/// This example is run along-side the example `flight_server`.
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let testdata = datafusion::test_util::parquet_test_data();
// Create Flight client
let mut client = FlightServiceClient::connect("http://localhost:50051").await?;
// Call get_schema to get the schema of a Parquet file
let request = tonic::Request::new(FlightDescriptor {
r#type: flight_descriptor::DescriptorType::Path as i32,
cmd: Default::default(),
path: vec![format!("{testdata}/alltypes_plain.parquet")],
});
let schema_result = client.get_schema(request).await?.into_inner();
let schema = Schema::try_from(&schema_result)?;
println!("Schema: {schema:?}");
// Call do_get to execute a SQL query and receive results
let request = tonic::Request::new(Ticket {
ticket: "SELECT id FROM alltypes_plain".into(),
});
let mut stream = client.do_get(request).await?.into_inner();
// the schema should be the first message returned, else client should error
let flight_data = stream.message().await?.unwrap();
// convert FlightData to a stream
let schema = Arc::new(Schema::try_from(&flight_data)?);
println!("Schema: {schema:?}");
// all the remaining stream messages should be dictionary and record batches
let mut results = vec![];
let dictionaries_by_field = HashMap::new();
while let Some(flight_data) = stream.message().await? {
let record_batch = flight_data_to_arrow_batch(
&flight_data,
schema.clone(),
&dictionaries_by_field,
)?;
results.push(record_batch);
}
// print the results
pretty::print_batches(&results)?;
Ok(())
}