|
| 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 | +//! See `main.rs` for how to run it. |
| 19 | +//! |
| 20 | +//! This follows the recommended approach: implement the `ObjectStore` trait |
| 21 | +//! (or use an existing implementation), register it with DataFusion, and then |
| 22 | +//! read a URL "path" from that store. |
| 23 | +//! See the in-memory reference implementation: |
| 24 | +//! https://docs.rs/object_store/latest/object_store/memory/struct.InMemory.html |
| 25 | +
|
| 26 | +use std::sync::Arc; |
| 27 | + |
| 28 | +use arrow::datatypes::{DataType, Field, Schema}; |
| 29 | +use datafusion::assert_batches_eq; |
| 30 | +use datafusion::common::Result; |
| 31 | +use datafusion::execution::object_store::ObjectStoreUrl; |
| 32 | +use datafusion::prelude::{CsvReadOptions, SessionContext}; |
| 33 | +use object_store::memory::InMemory; |
| 34 | +use object_store::path::Path; |
| 35 | +use object_store::{ObjectStore, ObjectStoreExt, PutPayload}; |
| 36 | + |
| 37 | +/// Demonstrates reading CSV data from an in-memory object store. |
| 38 | +/// |
| 39 | +/// The same pattern applies to JSON/Parquet: register a store for a URL |
| 40 | +/// prefix, write bytes into the store, then read via that URL. |
| 41 | +pub async fn in_memory_object_store() -> Result<()> { |
| 42 | + let store: Arc<dyn ObjectStore> = Arc::new(InMemory::new()); |
| 43 | + let ctx = SessionContext::new(); |
| 44 | + let object_store_url = ObjectStoreUrl::parse("memory://")?; |
| 45 | + // Register a URL prefix to route reads through this object store. |
| 46 | + ctx.register_object_store(object_store_url.as_ref(), Arc::clone(&store)); |
| 47 | + |
| 48 | + let schema = Schema::new(vec![ |
| 49 | + Field::new("id", DataType::Int64, false), |
| 50 | + Field::new("name", DataType::Utf8, false), |
| 51 | + ]); |
| 52 | + |
| 53 | + println!("=== CSV from memory ==="); |
| 54 | + let csv_path = Path::from("/people.csv"); |
| 55 | + let csv_data = b"id,name\n1,Alice\n2,Bob\n"; |
| 56 | + // Write bytes into the in-memory object store. |
| 57 | + store |
| 58 | + .put(&csv_path, PutPayload::from_static(csv_data)) |
| 59 | + .await?; |
| 60 | + // Read using the URL that matches the registered prefix. |
| 61 | + let csv = ctx |
| 62 | + .read_csv( |
| 63 | + "memory:///people.csv", |
| 64 | + CsvReadOptions::new().schema(&schema), |
| 65 | + ) |
| 66 | + .await? |
| 67 | + .collect() |
| 68 | + .await?; |
| 69 | + #[rustfmt::skip] |
| 70 | + let expected = [ |
| 71 | + "+----+-------+", |
| 72 | + "| id | name |", |
| 73 | + "+----+-------+", |
| 74 | + "| 1 | Alice |", |
| 75 | + "| 2 | Bob |", |
| 76 | + "+----+-------+", |
| 77 | + ]; |
| 78 | + assert_batches_eq!(expected, &csv); |
| 79 | + |
| 80 | + Ok(()) |
| 81 | +} |
0 commit comments