hequn8128 commented on a change in pull request #13066:
URL: https://github.com/apache/flink/pull/13066#discussion_r466921528
##########
File path: flink-python/pyflink/datastream/data_stream.py
##########
@@ -160,3 +169,122 @@ def set_buffer_timeout(self, timeout_millis: int):
"""
self._j_data_stream.setBufferTimeout(timeout_millis)
return self
+
+ def map(self, func: Union[Callable, MapFunction], type_info:
TypeInformation = None):
+ """
+ Applies a Map transformation on a DataStream. The transformation calls
a MapFunction for
+ each element of the DataStream. Each MapFunction call returns exactly
one element. The user
+ can also extend RichMapFunction to gain access to other features
provided by the
+ RichFunction interface.
+
+ Note that If user does not specify the output data type, the output
data will be serialized
+ as pickle primitive byte array.
+
+ :param func: The MapFunction that is called for each element of the
DataStream.
+ :param type_info: The type information of the MapFunction output data.
+ :return: The transformed DataStream.
+ """
+ if not isinstance(func, MapFunction):
+ if callable(func):
+ func = MapFunctionWrapper(func)
+ else:
+ raise TypeError("The input must be a MapFunction or a callable
function")
+ func_name = "Map"
+ j_python_data_stream_scalar_function_operator, output_type_info = \
+ self._get_java_python_function_operator(func,
+ type_info,
+ func_name,
+ flink_fn_execution_pb2
+
.UserDefinedDataStreamFunction.MAP)
+ return DataStream(self._j_data_stream.transform(
+ func_name,
+ output_type_info.get_java_type_info(),
+ j_python_data_stream_scalar_function_operator
+ ))
+
+ def flat_map(self, func: Union[Callable, FlatMapFunction], type_info:
TypeInformation = None):
+ """
+ Applies a FlatMap transformation on a DataStream. The transformation
calls a FlatMapFunction
+ for each element of the DataStream. Each FlatMapFunction call can
return any number of
+ elements including none. The user can also extend RichFlatMapFunction
to gain access to
+ other features provided by the RichFUnction.
+
+ :param func: The FlatMapFunction that is called for each element of
the DataStream.
+ :param type_info: The type information of output data.
+ :return: The transformed DataStream.
+ """
+ if not isinstance(func, FlatMapFunction):
+ if callable(func):
+ func = FlatMapFunctionWrapper(func)
+ else:
+ raise TypeError("The input must be a FlatMapFunction or a
callable function")
+ func_name = "m_flat_map" + str(uuid.uuid1())
Review comment:
ditto
##########
File path: flink-python/pyflink/datastream/data_stream.py
##########
@@ -160,3 +169,122 @@ def set_buffer_timeout(self, timeout_millis: int):
"""
self._j_data_stream.setBufferTimeout(timeout_millis)
return self
+
+ def map(self, func: Union[Callable, MapFunction], type_info:
TypeInformation = None):
+ """
+ Applies a Map transformation on a DataStream. The transformation calls
a MapFunction for
+ each element of the DataStream. Each MapFunction call returns exactly
one element. The user
+ can also extend RichMapFunction to gain access to other features
provided by the
+ RichFunction interface.
+
+ Note that If user does not specify the output data type, the output
data will be serialized
+ as pickle primitive byte array.
+
+ :param func: The MapFunction that is called for each element of the
DataStream.
+ :param type_info: The type information of the MapFunction output data.
+ :return: The transformed DataStream.
+ """
+ if not isinstance(func, MapFunction):
+ if callable(func):
+ func = MapFunctionWrapper(func)
+ else:
+ raise TypeError("The input must be a MapFunction or a callable
function")
+ func_name = "Map"
Review comment:
1. func_name = str(func)
2. perform transform with "Map"
```
func_name = str(func)
j_python_data_stream_scalar_function_operator, output_type_info = \
self._get_java_python_function_operator(func,
type_info,
func_name,
flink_fn_execution_pb2
.UserDefinedDataStreamFunction.MAP)
return DataStream(self._j_data_stream.transform(
"Map",
output_type_info.get_java_type_info(),
j_python_data_stream_scalar_function_operator
))
```
##########
File path: flink-python/pyflink/fn_execution/beam/beam_coders.py
##########
@@ -17,6 +17,7 @@
################################################################################
import os
+
Review comment:
remove the unnecessary change
##########
File path: flink-python/pyflink/datastream/functions.py
##########
@@ -0,0 +1,149 @@
+################################################################################
+# 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.
+################################################################################
+
+import abc
+
+from pyflink.java_gateway import get_gateway
+
+
+class Function(abc.ABC):
+ """
+ The base class for all user-defined functions.
+ """
+ pass
+
+
+class MapFunction(Function):
+ """
+ Base class for Map functions. Map functions take elements and transform
them, element wise. A
+ Map function always produces a single result element for each input
element. Typical
+ applications are parsing elements, converting data types, or projecting
out fields. Operations
+ that produce multiple result elements from a single input element can be
implemented using the
+ FlatMapFunction.
+ The basic syntax for using a MapFUnction is as follows:
Review comment:
MapFUnction => MapFunction
----------------------------------------------------------------
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]