MCPcopy Create free account
hub / github.com/apache/datafusion / create_context

Function create_context

datafusion-examples/examples/udf/simple_udwf.rs:33–78  ·  view source on GitHub ↗

create local execution context with `cars.csv` registered as a table named `cars`

()

Source from the content-addressed store, hash-verified

31
32// create local execution context with `cars.csv` registered as a table named `cars`
33async fn create_context() -> Result<SessionContext> {
34 // declare a new context. In spark API, this corresponds to a new spark SQL session
35 let ctx = SessionContext::new();
36
37 // content from file 'datafusion/core/tests/data/cars.csv'
38 let csv_data = r#"car,speed,time
39red,20.0,1996-04-12T12:05:03.000000000
40red,20.3,1996-04-12T12:05:04.000000000
41red,21.4,1996-04-12T12:05:05.000000000
42red,21.5,1996-04-12T12:05:06.000000000
43red,19.0,1996-04-12T12:05:07.000000000
44red,18.0,1996-04-12T12:05:08.000000000
45red,17.0,1996-04-12T12:05:09.000000000
46red,7.0,1996-04-12T12:05:10.000000000
47red,7.1,1996-04-12T12:05:11.000000000
48red,7.2,1996-04-12T12:05:12.000000000
49red,3.0,1996-04-12T12:05:13.000000000
50red,1.0,1996-04-12T12:05:14.000000000
51red,0.0,1996-04-12T12:05:15.000000000
52green,10.0,1996-04-12T12:05:03.000000000
53green,10.3,1996-04-12T12:05:04.000000000
54green,10.4,1996-04-12T12:05:05.000000000
55green,10.5,1996-04-12T12:05:06.000000000
56green,11.0,1996-04-12T12:05:07.000000000
57green,12.0,1996-04-12T12:05:08.000000000
58green,14.0,1996-04-12T12:05:09.000000000
59green,15.0,1996-04-12T12:05:10.000000000
60green,15.1,1996-04-12T12:05:11.000000000
61green,15.2,1996-04-12T12:05:12.000000000
62green,8.0,1996-04-12T12:05:13.000000000
63green,2.0,1996-04-12T12:05:14.000000000
64"#;
65 let dir = tempdir()?;
66 let file_path = dir.path().join("cars.csv");
67 {
68 let mut file = File::create(&file_path)?;
69 // write CSV data
70 file.write_all(csv_data.as_bytes())?;
71 } // scope closes the file
72 let file_path = file_path.to_str().unwrap();
73
74 ctx.register_csv("cars", file_path, CsvReadOptions::new())
75 .await?;
76
77 Ok(ctx)
78}
79
80/// In this example we will declare a user defined window function that computes a moving average and then run it using SQL
81pub async fn simple_udwf() -> Result<()> {

Callers 1

simple_udwfFunction · 0.70

Calls 6

newFunction · 0.85
createFunction · 0.85
joinMethod · 0.45
pathMethod · 0.45
write_allMethod · 0.45
register_csvMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…