suhuruli commented on code in PR #60546: URL: https://github.com/apache/spark/pull/60546#discussion_r4592099326
########## examples/src/main/python/pipelines/hello_world/transformations/orders.py: ########## @@ -0,0 +1,49 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Python datasets for the hello_world pipeline. + +- `customers` is a materialized view computed from a small in-line batch dataset. +- `orders` is a streaming table that incrementally ingests the JSON files in `data/orders/`. + Each run processes only the files that arrived since the previous run. +""" + +from pathlib import Path + +from pyspark import pipelines as dp +from pyspark.sql import DataFrame, SparkSession + +# SDP also injects the session as `spark`; assigning it explicitly keeps linters happy. +spark = SparkSession.active() + +ORDERS_DIR = (Path(__file__).parent.parent / "data" / "orders").resolve().as_uri() + + [email protected]_view +def customers() -> DataFrame: + return spark.createDataFrame( + [(1, "Alice", "US"), (2, "Bob", "DE"), (3, "Carol", "US")], + "customer_id INT, name STRING, country STRING", Review Comment: Thanks, you are right: with Spark Connect a string schema goes through a separate `ddl_parse` analysis request, and the server rejects analysis requests made inside a query function with `ATTEMPT_ANALYSIS_IN_PIPELINE_QUERY_FUNCTION`. I could reproduce it with `spark-pipelines dry-run` on the copied example, which failed while registering `customers`. The `customers` view now builds its schema as a `StructType` (`customer_id` as `IntegerType`, `name` and `country` as `StringType`) in `transformations/orders.py`, with a short comment on why a DDL string does not work there. `spark-pipelines dry-run` and `run` now load every definition, and `customers` has the columns `customer_id INT, name STRING, country STRING`. The `orders` reader keeps its DDL string, since the streaming reader sends it inside the plan. ########## examples/src/main/python/pipelines/hello_world/spark-pipeline.yml: ########## @@ -0,0 +1,27 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +name: hello_world +# Relative paths are resolved against the directory that contains this file. +storage: pipeline-storage Review Comment: Confirmed: the CLI passes `storage` to the server unchanged, and `PipelineUpdateContextImpl.validateStorageRoot` rejects anything that is not an absolute URI with a scheme, so the relative value failed every run and dry-run with `PIPELINE_STORAGE_ROOT_INVALID` once the `customers` problem was fixed. I removed the `storage` line and the comment about relative paths from `spark-pipeline.yml` and left a comment saying that README step 1 adds the field. README step 1 now appends `storage: "file://$PWD/pipeline-storage"` to the spec right after `cd /tmp/hello_world`, which gives the copied project an absolute `file://` URI the same way `spark-pipelines init` does. `spark.sql.catalogImplementation: hive` stays in the spec. I reran the README from step 1 to step 5 on the committed files, with the two companion changes (apache/spark#60579 and apache/spark#60580) applied, and every command completed with the outputs shown there; after deleting `spark-warehouse/`, `metastore_db/` and `pipel ine-storage/`, a new run rebuilt `orders` from both batches, so no checkpoint is left outside the project. ########## docs/declarative-pipelines-quick-start.md: ########## @@ -0,0 +1,236 @@ +--- +layout: global +title: Spark Declarative Pipelines Quick Start +displayTitle: Spark Declarative Pipelines Quick Start +license: | + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--- + +* Table of contents +{:toc} + +This page takes you from an empty machine to a running [Spark Declarative Pipelines (SDP)](declarative-pipelines-programming-guide.html) +pipeline on your laptop. You install PySpark, generate a pipeline project, run it, query its +output tables, and then add a streaming table and a materialized view that depends on it. + +## Prerequisites + +- Java 21 or 25. `java` must be on your `PATH`, or `JAVA_HOME` must point to the Java installation. +- Python 3.11 or later. + +## Install + +Create a virtual environment and install PySpark with the `pipelines` extra. The extra installs the +Spark Connect and YAML dependencies that the `spark-pipelines` command needs. + +```bash +python3 -m venv .venv +source .venv/bin/activate +pip install "pyspark[pipelines]" +``` + +The quotes around `pyspark[pipelines]` are required in shells such as zsh, which otherwise treat +the brackets as a glob pattern. + +## Create a pipeline project + +```bash +spark-pipelines init --name hello_pipeline +cd hello_pipeline +``` + +This creates the following project: + +``` +hello_pipeline/ +|-- spark-pipeline.yml +|-- pipeline-storage/ +`-- transformations/ + |-- example_python_materialized_view.py + `-- example_sql_materialized_view.sql +``` + +`spark-pipeline.yml` is the pipeline spec. It names the pipeline, points `libraries` at the source +files under `transformations/`, sets `storage` to the directory that holds streaming checkpoints, +and sets `spark.sql.catalogImplementation: hive` so that table metadata is kept in a local Hive +metastore and survives across runs. See [Pipeline Projects](declarative-pipelines-programming-guide.html#pipeline-projects) +for all fields. + +The two example files define one materialized view each. The Python file: + +```python +from pyspark import pipelines as dp +from pyspark.sql import DataFrame + [email protected]_view +def example_python_materialized_view() -> DataFrame: + return spark.range(10) +``` + +The SQL file reads from the Python one: + +```sql +CREATE MATERIALIZED VIEW example_sql_materialized_view AS +SELECT id FROM example_python_materialized_view +WHERE id % 2 = 0 +``` + +## Run the pipeline + +```bash +spark-pipelines run +``` + +SDP analyzes the dependencies between the datasets, runs +`example_python_materialized_view` first, and then `example_sql_materialized_view`. The output +ends with: + +``` +... Flow spark_catalog.default.example_python_materialized_view has COMPLETED. +... +... Flow spark_catalog.default.example_sql_materialized_view has COMPLETED. +... Run is COMPLETED. +``` + +The run writes table data to `spark-warehouse/` and table metadata to `metastore_db/`, both in the +current directory. + +## Query the output tables + +From the project directory, query the tables with the `spark-sql` command installed with PySpark: + +```bash +spark-sql -e "SELECT * FROM example_sql_materialized_view ORDER BY id" +``` + +``` +0 +2 +4 +6 +8 +``` + +You can also start the PySpark shell with `pyspark` from the same directory and run +`spark.table("example_sql_materialized_view").show()`. + +Run `spark-pipelines`, `spark-sql` and `pyspark` from the project directory: the local Hive +metastore lives in `metastore_db/` under the current directory. + +## Add a streaming table and a materialized view + +A streaming table processes only the data that arrived since the previous run. Create a directory +of JSON order events with a first batch of two orders: + +```bash +mkdir -p data/orders +cat > data/orders/batch-1.json <<'EOF' +{"order_id": 1, "customer": "alice", "amount": 20.0} +{"order_id": 2, "customer": "bob", "amount": 35.5} +EOF +``` + +Define a streaming table that ingests the files, in `transformations/orders.py`: + +```bash +cat > transformations/orders.py <<'EOF' +from pathlib import Path + +from pyspark import pipelines as dp +from pyspark.sql import DataFrame + +ORDERS_DIR = (Path(__file__).parent.parent / "data" / "orders").resolve().as_uri() Review Comment: Agreed. `Path.as_uri()` percent-encodes the path, and the file source hands the string to Hadoop's `Path`, which does not decode it, so a project under a directory with a space or a non-ASCII letter failed with `PATH_NOT_FOUND` naming the encoded path. Both copies of the snippet, the quick start's `transformations/orders.py` and the example's `transformations/orders.py`, now build the URI as `f"file://{(Path(__file__).parent.parent / 'data' / 'orders').resolve()}"`, with no call to `as_uri()`. I ran the example and the quick start from a directory whose path contains a space and a non-ASCII letter: the previous form failed with `PATH_NOT_FOUND` naming the percent-encoded path, and the new form ingests both order batches into `orders` across the two runs and reaches the `revenue_by_customer` output the quick start shows. Paths with glob characters such as `[` or `*` remain unsupported, as you noted. ########## docs/declarative-pipelines-quick-start.md: ########## @@ -0,0 +1,236 @@ +--- +layout: global +title: Spark Declarative Pipelines Quick Start +displayTitle: Spark Declarative Pipelines Quick Start +license: | + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--- + +* Table of contents +{:toc} + +This page takes you from an empty machine to a running [Spark Declarative Pipelines (SDP)](declarative-pipelines-programming-guide.html) +pipeline on your laptop. You install PySpark, generate a pipeline project, run it, query its +output tables, and then add a streaming table and a materialized view that depends on it. + +## Prerequisites + +- Java 21 or 25. `java` must be on your `PATH`, or `JAVA_HOME` must point to the Java installation. +- Python 3.11 or later. + +## Install + +Create a virtual environment and install PySpark with the `pipelines` extra. The extra installs the +Spark Connect and YAML dependencies that the `spark-pipelines` command needs. + +```bash +python3 -m venv .venv +source .venv/bin/activate +pip install "pyspark[pipelines]" +``` + +The quotes around `pyspark[pipelines]` are required in shells such as zsh, which otherwise treat +the brackets as a glob pattern. + +## Create a pipeline project + +```bash +spark-pipelines init --name hello_pipeline +cd hello_pipeline +``` + +This creates the following project: + +``` +hello_pipeline/ +|-- spark-pipeline.yml +|-- pipeline-storage/ +`-- transformations/ + |-- example_python_materialized_view.py + `-- example_sql_materialized_view.sql +``` + +`spark-pipeline.yml` is the pipeline spec. It names the pipeline, points `libraries` at the source +files under `transformations/`, sets `storage` to the directory that holds streaming checkpoints, +and sets `spark.sql.catalogImplementation: hive` so that table metadata is kept in a local Hive +metastore and survives across runs. See [Pipeline Projects](declarative-pipelines-programming-guide.html#pipeline-projects) +for all fields. + +The two example files define one materialized view each. The Python file: + +```python +from pyspark import pipelines as dp +from pyspark.sql import DataFrame + [email protected]_view +def example_python_materialized_view() -> DataFrame: + return spark.range(10) +``` + +The SQL file reads from the Python one: + +```sql +CREATE MATERIALIZED VIEW example_sql_materialized_view AS +SELECT id FROM example_python_materialized_view +WHERE id % 2 = 0 +``` + +## Run the pipeline + +```bash +spark-pipelines run +``` + +SDP analyzes the dependencies between the datasets, runs +`example_python_materialized_view` first, and then `example_sql_materialized_view`. The output +ends with: + +``` +... Flow spark_catalog.default.example_python_materialized_view has COMPLETED. +... +... Flow spark_catalog.default.example_sql_materialized_view has COMPLETED. +... Run is COMPLETED. +``` + +The run writes table data to `spark-warehouse/` and table metadata to `metastore_db/`, both in the +current directory. + +## Query the output tables + +From the project directory, query the tables with the `spark-sql` command installed with PySpark: + +```bash +spark-sql -e "SELECT * FROM example_sql_materialized_view ORDER BY id" +``` + +``` +0 +2 +4 +6 +8 +``` + +You can also start the PySpark shell with `pyspark` from the same directory and run +`spark.table("example_sql_materialized_view").show()`. + +Run `spark-pipelines`, `spark-sql` and `pyspark` from the project directory: the local Hive +metastore lives in `metastore_db/` under the current directory. + +## Add a streaming table and a materialized view + +A streaming table processes only the data that arrived since the previous run. Create a directory +of JSON order events with a first batch of two orders: + +```bash +mkdir -p data/orders +cat > data/orders/batch-1.json <<'EOF' +{"order_id": 1, "customer": "alice", "amount": 20.0} +{"order_id": 2, "customer": "bob", "amount": 35.5} +EOF +``` + +Define a streaming table that ingests the files, in `transformations/orders.py`: + +```bash +cat > transformations/orders.py <<'EOF' +from pathlib import Path + +from pyspark import pipelines as dp +from pyspark.sql import DataFrame + +ORDERS_DIR = (Path(__file__).parent.parent / "data" / "orders").resolve().as_uri() + + [email protected] +def orders() -> DataFrame: + return ( + spark.readStream + .schema("order_id INT, customer STRING, amount DOUBLE") + .json(ORDERS_DIR) + ) +EOF +``` + +`@dp.table` defines a streaming table because the function returns a streaming DataFrame +(`spark.readStream`). Streaming file sources need an explicit schema. Review Comment: Good catch; the decorator sets the dataset type, not the return value. The quick start now says that `@dp.table` defines a streaming table, so its function must return a streaming DataFrame (`spark.readStream`), and that streaming file sources need an explicit schema. That matches the programming guide's streaming table section and the for-loop fix in this PR, where `@dp.table` over a batch read fails with `INVALID_FLOW_QUERY_TYPE.BATCH_RELATION_FOR_STREAMING_TABLE`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
