|
| 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 | +"""Example demonstrating CsvReadOptions usage.""" |
| 19 | + |
| 20 | +from datafusion import CsvReadOptions, SessionContext |
| 21 | + |
| 22 | +# Create a SessionContext |
| 23 | +ctx = SessionContext() |
| 24 | + |
| 25 | +# Example 1: Using CsvReadOptions with default values |
| 26 | +print("Example 1: Default CsvReadOptions") |
| 27 | +options = CsvReadOptions() |
| 28 | +df = ctx.read_csv("data.csv", options=options) |
| 29 | + |
| 30 | +# Example 2: Using CsvReadOptions with custom parameters |
| 31 | +print("\nExample 2: Custom CsvReadOptions") |
| 32 | +options = CsvReadOptions( |
| 33 | + has_header=True, |
| 34 | + delimiter=",", |
| 35 | + quote='"', |
| 36 | + schema_infer_max_records=1000, |
| 37 | + file_extension=".csv", |
| 38 | +) |
| 39 | +df = ctx.read_csv("data.csv", options=options) |
| 40 | + |
| 41 | +# Example 3: Using the builder pattern (recommended for readability) |
| 42 | +print("\nExample 3: Builder pattern") |
| 43 | +options = ( |
| 44 | + CsvReadOptions() |
| 45 | + .with_has_header(True) # noqa: FBT003 |
| 46 | + .with_delimiter("|") |
| 47 | + .with_quote("'") |
| 48 | + .with_schema_infer_max_records(500) |
| 49 | + .with_truncated_rows(False) # noqa: FBT003 |
| 50 | + .with_newlines_in_values(True) # noqa: FBT003 |
| 51 | +) |
| 52 | +df = ctx.read_csv("data.csv", options=options) |
| 53 | + |
| 54 | +# Example 4: Advanced options |
| 55 | +print("\nExample 4: Advanced options") |
| 56 | +options = ( |
| 57 | + CsvReadOptions() |
| 58 | + .with_has_header(True) # noqa: FBT003 |
| 59 | + .with_delimiter(",") |
| 60 | + .with_comment("#") # Skip lines starting with # |
| 61 | + .with_escape("\\") # Escape character |
| 62 | + .with_null_regex(r"^(null|NULL|N/A)$") # Treat these as NULL |
| 63 | + .with_truncated_rows(True) # noqa: FBT003 |
| 64 | + .with_file_compression_type("gzip") # Read gzipped CSV |
| 65 | + .with_file_extension(".gz") |
| 66 | +) |
| 67 | +df = ctx.read_csv("data.csv.gz", options=options) |
| 68 | + |
| 69 | +# Example 5: Register CSV table with options |
| 70 | +print("\nExample 5: Register CSV table") |
| 71 | +options = CsvReadOptions().with_has_header(True).with_delimiter(",") # noqa: FBT003 |
| 72 | +ctx.register_csv("my_table", "data.csv", options=options) |
| 73 | +df = ctx.sql("SELECT * FROM my_table") |
| 74 | + |
| 75 | +# Example 6: Backward compatibility (without options) |
| 76 | +print("\nExample 6: Backward compatibility") |
| 77 | +# Still works the old way! |
| 78 | +df = ctx.read_csv("data.csv", has_header=True, delimiter=",") |
| 79 | + |
| 80 | +print("\nAll examples completed!") |
| 81 | +print("\nFor all available options, see the CsvReadOptions documentation:") |
| 82 | +print(" - has_header: bool") |
| 83 | +print(" - delimiter: str") |
| 84 | +print(" - quote: str") |
| 85 | +print(" - terminator: str | None") |
| 86 | +print(" - escape: str | None") |
| 87 | +print(" - comment: str | None") |
| 88 | +print(" - newlines_in_values: bool") |
| 89 | +print(" - schema: pa.Schema | None") |
| 90 | +print(" - schema_infer_max_records: int") |
| 91 | +print(" - file_extension: str") |
| 92 | +print(" - table_partition_cols: list[tuple[str, pa.DataType]]") |
| 93 | +print(" - file_compression_type: str") |
| 94 | +print(" - file_sort_order: list[list[SortExpr]]") |
| 95 | +print(" - null_regex: str | None") |
| 96 | +print(" - truncated_rows: bool") |
0 commit comments