|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! # Arrow Flight Examples |
| 19 | +//! |
| 20 | +//! These examples demonstrate Arrow Flight usage. |
| 21 | +//! |
| 22 | +//! Each subcommand runs a corresponding example: |
| 23 | +//! - `client` — run DataFusion as a standalone process and execute SQL queries from a client using the Flight protocol |
| 24 | +//! - `server` — run DataFusion as a standalone process and execute SQL queries from a client using the Flight protocol |
| 25 | +//! - `sql_server` — run DataFusion as a standalone process and execute SQL queries from JDBC clients |
| 26 | +
|
| 27 | +mod client; |
| 28 | +mod server; |
| 29 | +mod sql_server; |
| 30 | + |
| 31 | +use std::str::FromStr; |
| 32 | + |
| 33 | +use datafusion::error::{DataFusionError, Result}; |
| 34 | + |
| 35 | +enum ExampleKind { |
| 36 | + Client, |
| 37 | + Server, |
| 38 | + SqlServer, |
| 39 | +} |
| 40 | + |
| 41 | +impl AsRef<str> for ExampleKind { |
| 42 | + fn as_ref(&self) -> &str { |
| 43 | + match self { |
| 44 | + Self::Client => "client", |
| 45 | + Self::Server => "server", |
| 46 | + Self::SqlServer => "sql_server", |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl FromStr for ExampleKind { |
| 52 | + type Err = DataFusionError; |
| 53 | + |
| 54 | + fn from_str(s: &str) -> Result<Self> { |
| 55 | + match s { |
| 56 | + "client" => Ok(Self::Client), |
| 57 | + "server" => Ok(Self::Server), |
| 58 | + "sql_server" => Ok(Self::SqlServer), |
| 59 | + _ => Err(DataFusionError::Execution(format!("Unknown example: {s}"))), |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl ExampleKind { |
| 65 | + const ALL: [Self; 3] = [Self::Client, Self::Server, Self::SqlServer]; |
| 66 | + |
| 67 | + const EXAMPLE_NAME: &str = "flight"; |
| 68 | + |
| 69 | + fn variants() -> Vec<&'static str> { |
| 70 | + Self::ALL.iter().map(|x| x.as_ref()).collect() |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +#[tokio::main] |
| 75 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 76 | + let usage = format!( |
| 77 | + "Usage: cargo run --example {} -- [{}]", |
| 78 | + ExampleKind::EXAMPLE_NAME, |
| 79 | + ExampleKind::variants().join("|") |
| 80 | + ); |
| 81 | + |
| 82 | + let arg = std::env::args().nth(1).ok_or_else(|| { |
| 83 | + eprintln!("{usage}"); |
| 84 | + DataFusionError::Execution("Missing argument".to_string()) |
| 85 | + })?; |
| 86 | + |
| 87 | + match arg.parse::<ExampleKind>()? { |
| 88 | + ExampleKind::Client => client::client().await?, |
| 89 | + ExampleKind::Server => server::server().await?, |
| 90 | + ExampleKind::SqlServer => sql_server::sql_server().await?, |
| 91 | + } |
| 92 | + |
| 93 | + Ok(()) |
| 94 | +} |
0 commit comments