You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "reeba212 (via GitHub)" <gi...@apache.org> on 2024/08/22 17:52:50 UTC
[PR] Add examples for Spanner IO in YAML [beam]
reeba212 opened a new pull request, #32288:
URL: https://github.com/apache/beam/pull/32288
Added examples with explanatory comments for reading from and writing to a spanner table
------------------------
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
- [ ] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
- [ ] Update `CHANGES.md` with noteworthy changes.
- [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://github.com/apache/beam/blob/master/CONTRIBUTING.md#make-the-reviewers-job-easier).
To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
GitHub Actions Tests Status (on master branch)
------------------------------------------------------------------------------------------------
[](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI or the [workflows README](https://github.com/apache/beam/blob/master/.github/workflows/README.md) to see a list of phrases to trigger workflows.
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [yaml] Add examples for Spanner IO in YAML [beam]
Posted by "damccorm (via GitHub)" <gi...@apache.org>.
damccorm merged PR #32288:
URL: https://github.com/apache/beam/pull/32288
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [yaml] Add examples for Spanner IO in YAML [beam]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #32288:
URL: https://github.com/apache/beam/pull/32288#issuecomment-2338582359
Assigning reviewers. If you would like to opt out of this review, comment `assign to next reviewer`:
R: @tvalentyn for label python.
Available commands:
- `stop reviewer notifications` - opt out of the automated review tooling
- `remind me after tests pass` - tag the comment author after tests pass
- `waiting on author` - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)
The PR bot will only process comments in the main thread (not review comments).
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [yaml] Add examples for Spanner IO in YAML [beam]
Posted by "tvalentyn (via GitHub)" <gi...@apache.org>.
tvalentyn commented on PR #32288:
URL: https://github.com/apache/beam/pull/32288#issuecomment-2339315093
R: @Polber
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [yaml] Add examples for Spanner IO in YAML [beam]
Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #32288:
URL: https://github.com/apache/beam/pull/32288#issuecomment-2338488686
## [Codecov](https://app.codecov.io/gh/apache/beam/pull/32288?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 57.31%. Comparing base [(`2573106`)](https://app.codecov.io/gh/apache/beam/commit/2573106f3673eeb987874560778f50fdde03379f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) to head [(`92f8d24`)](https://app.codecov.io/gh/apache/beam/commit/92f8d24334256bb0697355db4b821c59c674d050?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
> Report is 1 commits behind head on master.
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## master #32288 +/- ##
============================================
- Coverage 57.32% 57.31% -0.02%
Complexity 1474 1474
============================================
Files 965 965
Lines 153454 153454
Branches 1076 1076
============================================
- Hits 87974 87956 -18
- Misses 63276 63294 +18
Partials 2204 2204
```
| [Flag](https://app.codecov.io/gh/apache/beam/pull/32288/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [python](https://app.codecov.io/gh/apache/beam/pull/32288/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `81.50% <ø> (-0.03%)` | :arrow_down: |
Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
</details>
[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/beam/pull/32288?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [yaml] Add examples for Spanner IO in YAML [beam]
Posted by "Polber (via GitHub)" <gi...@apache.org>.
Polber commented on code in PR #32288:
URL: https://github.com/apache/beam/pull/32288#discussion_r1755793430
##########
sdks/python/apache_beam/yaml/examples/io/spanner_read.yaml:
##########
@@ -0,0 +1,63 @@
+pipeline:
Review Comment:
Please add a license
```suggestion
# coding=utf-8
#
# 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.
pipeline:
```
##########
sdks/python/apache_beam/yaml/examples/io/spanner_write.yaml:
##########
@@ -0,0 +1,36 @@
+pipeline:
Review Comment:
Here too
```suggestion
# coding=utf-8
#
# 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.
pipeline:
```
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [yaml] Add examples for Spanner IO in YAML [beam]
Posted by "Polber (via GitHub)" <gi...@apache.org>.
Polber commented on code in PR #32288:
URL: https://github.com/apache/beam/pull/32288#discussion_r1731939455
##########
sdks/python/apache_beam/yaml/examples/io/spanner_write.yaml:
##########
Review Comment:
Could you add a "good" element to the `Create` transform and validate that it does not go to error_handling (updating Expected comment as well)?
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [yaml] Add examples for Spanner IO in YAML [beam]
Posted by "Polber (via GitHub)" <gi...@apache.org>.
Polber commented on code in PR #32288:
URL: https://github.com/apache/beam/pull/32288#discussion_r1727723306
##########
sdks/python/apache_beam/yaml/examples/spanner_read.yaml:
##########
@@ -0,0 +1,63 @@
+pipeline:
+ transforms:
+
+ # Reading data from a Spanner database. The table used here has the following columns:
+ # shipment_id (String), customer_id (String), shipment_date (String), shipment_cost (Float64), customer_name (String), customer_email (String)
+ # ReadFromSpanner transform is called using project_id, instance_id, database_id and a query
+ # A table with a list of columns can also be specified instead of a query
+ - type: ReadFromSpanner
+ name: ReadShipments
+ config:
+ project_id: 'apache-beam-testing'
+ instance_id: 'shipment-test'
+ database_id: 'shipment'
Review Comment:
@damccorm Are Spanner resources (specifically these) exposed to general public? i.e., would anyone be able to run this YAML as-is?
##########
sdks/python/apache_beam/yaml/examples/spanner_read.yaml:
##########
Review Comment:
Let's create a new folder `examples/io` and put these files in there
##########
sdks/python/apache_beam/yaml/examples/spanner_read.yaml:
##########
@@ -0,0 +1,63 @@
+pipeline:
+ transforms:
+
+ # Reading data from a Spanner database. The table used here has the following columns:
+ # shipment_id (String), customer_id (String), shipment_date (String), shipment_cost (Float64), customer_name (String), customer_email (String)
+ # ReadFromSpanner transform is called using project_id, instance_id, database_id and a query
+ # A table with a list of columns can also be specified instead of a query
+ - type: ReadFromSpanner
+ name: ReadShipments
+ config:
+ project_id: 'apache-beam-testing'
+ instance_id: 'shipment-test'
+ database_id: 'shipment'
+ query: 'SELECT * FROM shipments'
+
+ # Filtering the data based on a specific condition
+ # Here, the condition is used to keep only the rows where the customer_id is 'C1'
+ - type: Filter
+ name: FilterShipments
+ input: ReadShipments
+ config:
+ language: python
+ keep: "customer_id == 'C1'"
+
+ # Mapping the data fields and applying transformations
+ # A new field 'shipment_cost_category' is added with a custom transformation
+ # A callable is defined to categorize shipment cost
+ - type: MapToFields
+ name: MapFieldsForSpanner
+ input: FilterShipments
+ config:
+ language: python
+ fields:
+ shipment_id: shipment_id
+ customer_id: customer_id
+ shipment_date: shipment_date
+ shipment_cost: shipment_cost
+ customer_name: customer_name
+ customer_email: customer_email
+ shipment_cost_category:
+ callable: |
+ def categorize_cost(row):
+ cost = float(row[3])
+ if cost < 50:
+ return 'Low Cost'
+ elif cost < 200:
+ return 'Medium Cost'
+ else:
+ return 'High Cost'
+
+ # Writing the transformed data to a CSV file
+ - type: WriteToCsv
+ name: WriteBig
+ input: MapFieldsForSpanner
+ config:
+ path: shipments.csv
+
+
+ # On executing the above pipeline, a new CSV file is created with the following records
+
+ # shipment_id,customer_id,shipment_date,shipment_cost,customer_name,customer_email,shipment_cost_category
+ # S1,C1,2023-05-01,150.0,Alice,alice@example.com,Medium Cost
+ # S3,C1,2023-05-10,20.0,Alice,alice@example.com,Low Cost
Review Comment:
You will notice all the other example YAML files have a block like:
```
# Expected:
# Row(...)
# Row(...)
# Row(...)
```
This is actually used by the testing framework which runs the YAML and validates against this Expected output. So, it provides not only a way for the user to see expected output, but also allows us to incorporate automated testing.
You should be able to refactor to something like:
```
# Expected:
# Row(shipment_id='S1', customer_id='C1', shipment_date='2023-05-01', shipment_cost=150.0, customer_name='Alice', customer_email='alice@example.com', shipment_cost_category='Medium Cost')
# Row(shipment_id='S3', customer_id='C1', shipment_date='2023-05-10', shipment_cost=20.0, customer_name='Alice', customer_email='alice@example.com', shipment_cost_category='Low Cost')
```
You will also need to add test suite in https://github.com/apache/beam/blob/master/sdks/python/apache_beam/yaml/examples/testing/examples_test.py#L208:
```
IOTest = YamlExamplesTestSuite(
'AggregationExamplesTest',
os.path.join(YAML_DOCS_DIR, '../io/*.yaml')).run()
```
##########
sdks/python/apache_beam/yaml/examples/spanner_write.yaml:
##########
@@ -0,0 +1,41 @@
+pipeline:
+ transforms:
+
+ # Step 1: Creating rows to be written to Spanner
+ # The element names correspond to the column names in the Spanner table
+ - type: Create
+ name: CreateRows
+ config:
+ elements:
+ - shipment_id: "S5"
+ customer_id: "C5"
+ shipment_date: "2023-05-09"
+ shipment_cost: "300"
+ customer_name: "Erin"
+ customer_email: "erin@example.com"
+
+ # Step 2: Writing the created rows to a Spanner database
+ # We require the project ID, instance ID, database ID and table ID to connect to Spanner
+ # Error handling can be specified optionally to ensure any failed operations aren't lost
+ # The failed data is passed on in the pipeline and can be handled
+ - type: WriteToSpanner
+ name: WriteSpanner
+ input: CreateRows
+ config:
+ project_id: 'apache-beam-testing'
+ instance_id: 'shipment-test'
+ database_id: 'shipment'
+ table_id: 'shipments'
+ error_handling:
+ output: my_error_output
+
+ # Step 3: Writing the failed records to a JSON file
+ - type: WriteToJson
+ input: WriteSpanner.my_error_output
+ config:
+ path: errors.json
+
+ # The above pipeline on execution creates a JSON file where the following failed record is stored:
+ # {"error_message":"INSERT_OR_UPDATE operation failed at instance: shipment-test, database: shipment, table: shipments","failed_row":{"shipment_id":"S5","customer_id":"C5","shipment_date":"2023-05-09","shipment_cost":300,"customer_name":"Erin","customer_email":"erin@example.com"}}
+ # The operation fails as "shipment_cost" required a float value, whereas a string value is provided in the CreateRows transform
+ # Changing the value of "shipment_cost" from "300" to 300.0 will result in a successful write operation
Review Comment:
Same Expected block as other file
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [yaml] Add examples for Spanner IO in YAML [beam]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #32288:
URL: https://github.com/apache/beam/pull/32288#issuecomment-2305540299
Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment `assign set of reviewers`
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [yaml] Add examples for Spanner IO in YAML [beam]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #32288:
URL: https://github.com/apache/beam/pull/32288#issuecomment-2339316235
Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment `assign set of reviewers`
--
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: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org