[
https://issues.apache.org/jira/browse/BEAM-6695?focusedWorklogId=224695&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-224695
]
ASF GitHub Bot logged work on BEAM-6695:
----------------------------------------
Author: ASF GitHub Bot
Created on: 09/Apr/19 00:10
Start Date: 09/Apr/19 00:10
Worklog Time Spent: 10m
Work Description: robinyqiu commented on pull request #8206: [BEAM-6695]
Latest PTransform for Python SDK
URL: https://github.com/apache/beam/pull/8206#discussion_r273282739
##########
File path: sdks/python/apache_beam/transforms/combiners.py
##########
@@ -858,3 +860,60 @@ def merge_only(self, accumulators):
def extract_only(self, accumulator):
return self.combine_fn.extract_output(accumulator)
+
+
+class Latest(object):
+ """Combiners for computing the latest element"""
+
+ class Globally(ptransform.PTransform):
+ """Compute the element with the latest timestamp from a
+ PCollection."""
+
+ @staticmethod
+ def add_timestamp(element, timestamp=core.DoFn.TimestampParam):
+ return [(element, timestamp)]
+
+ def expand(self, pcoll):
+ return (pcoll
+ | core.ParDo(self.add_timestamp)
+ | core.CombineGlobally(LatestCombineFn()))
+
+ class PerKey(ptransform.PTransform):
+ """Compute elements with the latest timestamp for each key
+ from a keyed PCollection"""
+
+ @staticmethod
+ def add_timestamp(element, timestamp=core.DoFn.TimestampParam):
+ _check_instance_type(KV[T, T], element)
+ (K, V) = element
+ return [(K, (V, timestamp))]
+
+ def expand(self, pcoll):
+ return (pcoll
+ | core.ParDo(self.add_timestamp)
+ | core.CombinePerKey(LatestCombineFn()))
+
+
+@with_input_types(KV[T, T])
Review comment:
Using KV as type hint seems a bit weird to me because the input is really a
(element, timestamp) tuple. Maybe we can just use something like Tuple[E, T] as
input type and E as output type. For more about Python type hints, see:
https://beam.apache.org/documentation/sdks/python-type-safety/
----------------------------------------------------------------
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:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 224695)
Time Spent: 4h 10m (was: 4h)
> Latest transform for Python SDK
> -------------------------------
>
> Key: BEAM-6695
> URL: https://issues.apache.org/jira/browse/BEAM-6695
> Project: Beam
> Issue Type: New Feature
> Components: sdk-py-core
> Reporter: Ahmet Altay
> Assignee: Tanay Tummalapalli
> Priority: Minor
> Time Spent: 4h 10m
> Remaining Estimate: 0h
>
> Add a PTransform} and Combine.CombineFn for computing the latest element in a
> PCollection.
> It should offer the same API as its Java counterpart:
> https://github.com/apache/beam/blob/11a977b8b26eff2274d706541127c19dc93131a2/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/Latest.java
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)