You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/10/02 22:27:28 UTC
[GitHub] [beam] TheNeuralBit opened a new pull request #13005: Add python schema inference docs
TheNeuralBit opened a new pull request #13005:
URL: https://github.com/apache/beam/pull/13005
R: @robertwb
Post-Commit Tests Status (on master branch)
------------------------------------------------------------------------------------------------
Lang | SDK | Dataflow | Flink | Samza | Spark | Twister2
--- | --- | --- | --- | --- | --- | ---
Go | [](https://ci-beam.apache.org/job/beam_PostCommit_Go/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_Go_VR_Flink/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_Go_VR_Spark/lastCompletedBuild/) | ---
Java | [](https://ci-beam.apache.org/job/beam_PostCommit_Java/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow_Java11/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink_Java11/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Flink_Batch/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Flink_Streaming/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Samza/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Spark/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Spark_Batch/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_SparkStructuredStreaming/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Twister2/lastCompletedBuild/)
Python | [](https://ci-beam.apache.org/job/beam_PostCommit_Python36/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Python37/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Python38/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Py_VR_Dataflow/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Py_VR_Dataflow_V2/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Py_ValCont/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Python_PVR_Flink_Cron/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Python_VR_Flink/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_Python_VR_Spark/lastCompletedBuild/) | ---
XLang | [](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Direct/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Flink/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Spark/lastCompletedBuild/) | ---
Pre-Commit Tests Status (on master branch)
------------------------------------------------------------------------------------------------
--- |Java | Python | Go | Website | Whitespace | Typescript
--- | --- | --- | --- | --- | --- | ---
Non-portable | [](https://ci-beam.apache.org/job/beam_PreCommit_Java_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Python_Cron/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PreCommit_PythonLint_Cron/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PreCommit_PythonDocker_Cron/lastCompletedBuild/) <br>[](https://ci-beam.apache.org/job/beam_PreCommit_PythonDocs_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Go_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Website_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Whitespace_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Typescript_Cron/lastCompletedBuild/)
Portable | --- | [](https://ci-beam.apache.org/job/beam_PreCommit_Portable_Python_Cron/lastCompletedBuild/) | --- | --- | --- | ---
See [.test-infra/jenkins/README](https://github.com/apache/beam/blob/master/.test-infra/jenkins/README.md) for trigger phrase, status and link of all Jenkins jobs.
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)
See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] TheNeuralBit commented on a change in pull request #13005: Add python schema inference docs
Posted by GitBox <gi...@apache.org>.
TheNeuralBit commented on a change in pull request #13005:
URL: https://github.com/apache/beam/pull/13005#discussion_r501372151
##########
File path: website/www/site/content/en/documentation/programming-guide.md
##########
@@ -2546,10 +2611,68 @@ public abstract class TransactionValue {
}
{{< /highlight >}}
+{{< paragraph class="language-java" >}}
This is all that’s needed to generate a simple AutoValue class, and the above `@DefaultSchema` annotation tells Beam to
infer a schema from it. This also allows AutoValue elements to be used inside of `PCollection`s.
+{{< /paragraph >}}
+{{< paragraph class="language-java" >}}
`@SchemaFieldName` and `@SchemaIgnore` can be used to alter the schema inferred.
+{{< /paragraph >}}
+
+{{< paragraph class="language-py" >}}
+Beam has a few different mechanisms for inferring schemas from Python code.
+{{< /paragraph >}}
+
+{{< paragraph class="language-py" >}}
+**NamedTuple classes**
+{{< /paragraph >}}
+
+{{< paragraph class="language-py" >}}
+A [NamedTuple](https://docs.python.org/3/library/typing.html#typing.NamedTuple)
+class is a Python class that wraps a `tuple`, assigning a name to each element
+and restricting it to a particular type. Beam will automatically infer the
+schema for PCollections with `NamedTuple` output types. For example:
+{{< /paragraph >}}
+
+{{< highlight py >}}
+class Transaction(typing.NamedTuple):
+ bank: str
+ purchase_amount: float
+
+pc = input | beam.Map(lambda ...).with_output_types(Transaction)
+{{< /highlight >}}
+
+
+{{< paragraph class="language-py" >}}
+**beam.Row**
+{{< /paragraph >}}
+
+{{< paragraph class="language-py" >}}
+It's also possible to create ad-hoc schema declarations with a simple lambda
+that returns instances of `beam.Row`:
+{{< /paragraph >}}
+
+{{< highlight py >}}
+input_pc = ... # {"bank": ..., "purchase_amount": ...}
+output_pc = input_pc | beam.Map(lambda item: beam.Row(bank=item["bank"],
+ purchase_amount=item["purchase_amount"])
+{{< /highlight >}}
+
+{{< paragraph class="language-py" >}}
+Note that this declaration doesn't include any specific information about the
+types of the `bank` and `purchase_amount` fields. Beam will attempt to infer
+type information, if it's unable to it will fall back to the generic type
+`Any`. Sometimes this is not ideal, you can use casts to make sure Beam
+correctly infers types with `beam.Row`:
+{{< /paragraph >}}
+
+{{< highlight py >}}
+input_pc = ... # {"bank": ..., "purchase_amount": ...}
+output_pc = input_pc | beam.Map(lambda item: beam.Row(bank=str(item["bank"]),
+ purchase_amount=float(item["purchase_amount"]))
+{{< /highlight >}}
+
Review comment:
Good idea! I'll add it in a separate PR and we can merge it once Select is available in 2.25.0
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] TheNeuralBit merged pull request #13005: Add python schema inference docs
Posted by GitBox <gi...@apache.org>.
TheNeuralBit merged pull request #13005:
URL: https://github.com/apache/beam/pull/13005
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] robertwb commented on a change in pull request #13005: Add python schema inference docs
Posted by GitBox <gi...@apache.org>.
robertwb commented on a change in pull request #13005:
URL: https://github.com/apache/beam/pull/13005#discussion_r499786330
##########
File path: website/www/site/content/en/documentation/programming-guide.md
##########
@@ -2546,10 +2611,68 @@ public abstract class TransactionValue {
}
{{< /highlight >}}
+{{< paragraph class="language-java" >}}
This is all that’s needed to generate a simple AutoValue class, and the above `@DefaultSchema` annotation tells Beam to
infer a schema from it. This also allows AutoValue elements to be used inside of `PCollection`s.
+{{< /paragraph >}}
+{{< paragraph class="language-java" >}}
`@SchemaFieldName` and `@SchemaIgnore` can be used to alter the schema inferred.
+{{< /paragraph >}}
+
+{{< paragraph class="language-py" >}}
+Beam has a few different mechanisms for inferring schemas from Python code.
+{{< /paragraph >}}
+
+{{< paragraph class="language-py" >}}
+**NamedTuple classes**
+{{< /paragraph >}}
+
+{{< paragraph class="language-py" >}}
+A [NamedTuple](https://docs.python.org/3/library/typing.html#typing.NamedTuple)
+class is a Python class that wraps a `tuple`, assigning a name to each element
+and restricting it to a particular type. Beam will automatically infer the
+schema for PCollections with `NamedTuple` output types. For example:
+{{< /paragraph >}}
+
+{{< highlight py >}}
+class Transaction(typing.NamedTuple):
+ bank: str
+ purchase_amount: float
+
+pc = input | beam.Map(lambda ...).with_output_types(Transaction)
+{{< /highlight >}}
+
+
+{{< paragraph class="language-py" >}}
+**beam.Row**
+{{< /paragraph >}}
+
+{{< paragraph class="language-py" >}}
+It's also possible to create ad-hoc schema declarations with a simple lambda
+that returns instances of `beam.Row`:
+{{< /paragraph >}}
+
+{{< highlight py >}}
+input_pc = ... # {"bank": ..., "purchase_amount": ...}
+output_pc = input_pc | beam.Map(lambda item: beam.Row(bank=item["bank"],
+ purchase_amount=item["purchase_amount"])
+{{< /highlight >}}
+
+{{< paragraph class="language-py" >}}
+Note that this declaration doesn't include any specific information about the
+types of the `bank` and `purchase_amount` fields. Beam will attempt to infer
+type information, if it's unable to it will fall back to the generic type
+`Any`. Sometimes this is not ideal, you can use casts to make sure Beam
+correctly infers types with `beam.Row`:
+{{< /paragraph >}}
+
+{{< highlight py >}}
+input_pc = ... # {"bank": ..., "purchase_amount": ...}
+output_pc = input_pc | beam.Map(lambda item: beam.Row(bank=str(item["bank"]),
+ purchase_amount=float(item["purchase_amount"]))
+{{< /highlight >}}
+
Review comment:
Is it worth calling out Select 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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org