|
17 | 17 |
|
18 | 18 | """Example demonstrating CsvReadOptions usage.""" |
19 | 19 |
|
| 20 | +import gzip |
| 21 | +import shutil |
| 22 | +import tempfile |
| 23 | +from pathlib import Path |
| 24 | + |
20 | 25 | from datafusion import CsvReadOptions, SessionContext |
21 | 26 |
|
| 27 | +# This example is self-contained: it writes its own small CSV file, plus a |
| 28 | +# gzipped copy, into a temporary directory instead of expecting a |
| 29 | +# ``data.csv`` file that is not part of this repository. |
| 30 | +tmp_dir = Path(tempfile.mkdtemp()) |
| 31 | +csv_path = tmp_dir / "data.csv" |
| 32 | +csv_path.write_text("a,b,c\n1,2,3\n4,5,6\n7,8,9\n") |
| 33 | +gz_path = tmp_dir / "data.csv.gz" |
| 34 | +with csv_path.open("rb") as f_in, gzip.open(gz_path, "wb") as f_out: |
| 35 | + shutil.copyfileobj(f_in, f_out) |
| 36 | + |
22 | 37 | # Create a SessionContext |
23 | 38 | ctx = SessionContext() |
24 | 39 |
|
25 | 40 | # Example 1: Using CsvReadOptions with default values |
26 | 41 | print("Example 1: Default CsvReadOptions") |
27 | 42 | options = CsvReadOptions() |
28 | | -df = ctx.read_csv("data.csv", options=options) |
| 43 | +df = ctx.read_csv(str(csv_path), options=options) |
29 | 44 |
|
30 | 45 | # Example 2: Using CsvReadOptions with custom parameters |
31 | 46 | print("\nExample 2: Custom CsvReadOptions") |
|
36 | 51 | schema_infer_max_records=1000, |
37 | 52 | file_extension=".csv", |
38 | 53 | ) |
39 | | -df = ctx.read_csv("data.csv", options=options) |
| 54 | +df = ctx.read_csv(str(csv_path), options=options) |
40 | 55 |
|
41 | 56 | # Example 3: Using the builder pattern (recommended for readability) |
42 | 57 | print("\nExample 3: Builder pattern") |
|
49 | 64 | .with_truncated_rows(False) # noqa: FBT003 |
50 | 65 | .with_newlines_in_values(True) # noqa: FBT003 |
51 | 66 | ) |
52 | | -df = ctx.read_csv("data.csv", options=options) |
| 67 | +df = ctx.read_csv(str(csv_path), options=options) |
53 | 68 |
|
54 | 69 | # Example 4: Advanced options |
55 | 70 | print("\nExample 4: Advanced options") |
|
64 | 79 | .with_file_compression_type("gzip") # Read gzipped CSV |
65 | 80 | .with_file_extension(".gz") |
66 | 81 | ) |
67 | | -df = ctx.read_csv("data.csv.gz", options=options) |
| 82 | +df = ctx.read_csv(str(gz_path), options=options) |
68 | 83 |
|
69 | 84 | # Example 5: Register CSV table with options |
70 | 85 | print("\nExample 5: Register CSV table") |
71 | 86 | options = CsvReadOptions().with_has_header(True).with_delimiter(",") # noqa: FBT003 |
72 | | -ctx.register_csv("my_table", "data.csv", options=options) |
| 87 | +ctx.register_csv("my_table", str(csv_path), options=options) |
73 | 88 | df = ctx.sql("SELECT * FROM my_table") |
74 | 89 |
|
75 | 90 | # Example 6: Backward compatibility (without options) |
76 | 91 | print("\nExample 6: Backward compatibility") |
77 | 92 | # Still works the old way! |
78 | | -df = ctx.read_csv("data.csv", has_header=True, delimiter=",") |
| 93 | +df = ctx.read_csv(str(csv_path), has_header=True, delimiter=",") |
79 | 94 |
|
80 | 95 | print("\nAll examples completed!") |
81 | 96 | print("\nFor all available options, see the CsvReadOptions documentation:") |
|
0 commit comments