|
| 1 | +using Dapper; |
| 2 | +using DataFusionSharp; |
| 3 | +using DataFusionSharp.Data; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | + |
| 6 | +// Setup logging |
| 7 | +var loggerFactory = LoggerFactory.Create(builder => |
| 8 | +{ |
| 9 | + builder |
| 10 | + .SetMinimumLevel(LogLevel.Information) |
| 11 | + .AddSimpleConsole(o => o.IncludeScopes = true); |
| 12 | +}); |
| 13 | +DataFusionNativeLogger.ConfigureLogger(loggerFactory.CreateLogger("DataFusionSharp"), LogLevel.Information); |
| 14 | + |
| 15 | +// Init runtime and single session |
| 16 | +using var runtime = DataFusionRuntime.Create(); |
| 17 | +using var session = runtime.CreateSessionContext(); |
| 18 | + |
| 19 | +// Load customers CSV into an in-memory object store |
| 20 | +using var store = runtime.CreateInMemoryStore(); |
| 21 | + |
| 22 | +var customersCsvBytes = await File.ReadAllBytesAsync(Path.Combine("Data", "orders", "csv", "customers.csv")); |
| 23 | +await store.PutAsync("customers.csv", customersCsvBytes); |
| 24 | + |
| 25 | +session.RegisterInMemoryObjectStore("memory://", store); |
| 26 | +await session.RegisterCsvAsync("customers", "memory:///customers.csv"); |
| 27 | + |
| 28 | +// Register orders from a regular file on disk |
| 29 | +await session.RegisterCsvAsync("orders", Path.Combine("Data", "orders", "csv", "orders.csv")); |
| 30 | + |
| 31 | +// Create a connection wrapper around the SessionContext for ADO.NET operations. |
| 32 | +await using var connection = session.AsConnection(); |
| 33 | + |
| 34 | + |
| 35 | +// QueryAsync<T> – map rows to a strongly-typed record |
| 36 | +Console.WriteLine("=== Completed orders per customer (in-memory JOIN file) ==="); |
| 37 | + |
| 38 | +var summaries = await connection.QueryAsync<OrderSummary>( |
| 39 | + """ |
| 40 | + SELECT |
| 41 | + c.customer_name AS CustomerName, |
| 42 | + c.country AS Country, |
| 43 | + COUNT(*) AS OrderCount, |
| 44 | + SUM(o.order_amount) AS TotalAmount |
| 45 | + FROM customers AS c |
| 46 | + JOIN orders AS o ON c.customer_id = o.customer_id |
| 47 | + WHERE o.order_status = @status |
| 48 | + GROUP BY c.customer_name, c.country |
| 49 | + ORDER BY TotalAmount DESC |
| 50 | + """, |
| 51 | + new { status = "Completed" }); |
| 52 | + |
| 53 | +foreach (var s in summaries) |
| 54 | + Console.WriteLine($" {s.CustomerName,-25} {s.Country,-10} orders: {s.OrderCount,3} total: {s.TotalAmount,10:N0}"); |
| 55 | + |
| 56 | +// Model types |
| 57 | + |
| 58 | +record OrderSummary(string CustomerName, string Country, long OrderCount, long TotalAmount); |
0 commit comments