|
| 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 | +use datafusion::arrow::datatypes::{DataType, Field, Schema}; |
| 19 | +use datafusion::error::Result; |
| 20 | +use datafusion::prelude::*; |
| 21 | +use std::sync::Arc; |
| 22 | + |
| 23 | +/// This example demonstrates executing a simple query against an Arrow data source (CSV) and |
| 24 | +/// fetching results |
| 25 | +#[tokio::main] |
| 26 | +async fn main() -> Result<()> { |
| 27 | + // create local execution context |
| 28 | + let mut ctx = ExecutionContext::new(); |
| 29 | + |
| 30 | + let testdata = datafusion::test_util::arrow_test_data(); |
| 31 | + |
| 32 | + // schema with decimal type |
| 33 | + let schema = Arc::new(Schema::new(vec![ |
| 34 | + Field::new("c1", DataType::Decimal(10, 6), false), |
| 35 | + Field::new("c2", DataType::Float64, false), |
| 36 | + Field::new("c3", DataType::Boolean, false), |
| 37 | + ])); |
| 38 | + |
| 39 | + // register csv file with the execution context |
| 40 | + ctx.register_csv( |
| 41 | + "aggregate_simple", |
| 42 | + &format!("{}/csv/aggregate_simple.csv", testdata), |
| 43 | + CsvReadOptions::new().schema(&schema), |
| 44 | + ) |
| 45 | + .await?; |
| 46 | + |
| 47 | + // execute the query |
| 48 | + let df = ctx.sql("select c1 from aggregate_simple").await?; |
| 49 | + |
| 50 | + // print the results |
| 51 | + df.show().await?; |
| 52 | + |
| 53 | + Ok(()) |
| 54 | +} |
0 commit comments