You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/08/10 04:43:25 UTC

[GitHub] [flink] hequn8128 commented on a change in pull request #13095: [FLINK-18861][python] Support add_source() to get a DataStream for Py…

hequn8128 commented on a change in pull request #13095:
URL: https://github.com/apache/flink/pull/13095#discussion_r467685223



##########
File path: flink-python/src/test/java/org/apache/flink/python/util/MyCustomSourceFunction.java
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.
+ */
+
+package org.apache.flink.python.util;
+
+import org.apache.flink.streaming.api.functions.source.SourceFunction;
+import org.apache.flink.types.Row;
+
+import java.util.Random;
+
+/**
+ * A custom source function to for testing add a custom source in Python StreamExecutionEnvironment.
+ */
+public class MyCustomSourceFunction implements SourceFunction<Row> {
+
+	private static final String[] NAMES = {"Bob", "Marry", "Henry", "Mike", "Ted", "Jack"};
+
+	private int recordCount = 50;
+
+	public MyCustomSourceFunction() {
+	}
+
+	public MyCustomSourceFunction(int recordCount) {
+		this.recordCount = recordCount;
+	}
+
+	public void run(SourceContext sourceContext) {
+		Random random = new Random();
+		for (int i = 0; i < recordCount; i++) {
+			Row row = Row.of(random.nextInt(1000), NAMES[random.nextInt(NAMES.length)], random.nextDouble());
+			sourceContext.collect(row);
+		}
+

Review comment:
       Remove the empty line.

##########
File path: flink-python/pyflink/datastream/connectors.py
##########
@@ -0,0 +1,61 @@
+################################################################################
+#  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.
+################################################################################
+from pyflink.java_gateway import get_gateway
+
+
+class SourceFunction(object):

Review comment:
       Same as the comments [here](https://github.com/apache/flink/pull/13094#discussion_r467659569), so that we don't need to introduce the `CustomSourceFunction`.

##########
File path: flink-python/pyflink/datastream/stream_execution_environment.py
##########
@@ -447,6 +447,23 @@ def get_execution_environment():
             .StreamExecutionEnvironment.getExecutionEnvironment()
         return StreamExecutionEnvironment(j_stream_exection_environment)
 
+    def add_source(self, source_func: SourceFunction, source_name: str = 'Custom Source',
+                   type_info: TypeInformation = None) -> 'DataStream':
+        """
+        Adds a data source to the streaming topology.
+
+        :param source_func: the user defined function.
+        :param source_name: name of the data source. Optional.
+        :param type_info: type of the returned stream. Optional.
+        :return: the data stream constructed.
+        """
+        j_type_info = type_info.get_java_type_info() if type_info is not None else None
+        j_data_stream = self._j_stream_execution_environment.addSource(source_func

Review comment:
       If the type_info is None, we should call `_j_stream_execution_environment.addSource(SourceFunction<OUT> function, String sourceName)`

##########
File path: flink-python/src/test/java/org/apache/flink/python/util/MyCustomSourceFunction.java
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.
+ */
+
+package org.apache.flink.python.util;
+
+import org.apache.flink.streaming.api.functions.source.SourceFunction;
+import org.apache.flink.types.Row;
+
+import java.util.Random;
+
+/**
+ * A custom source function to for testing add a custom source in Python StreamExecutionEnvironment.

Review comment:
       A custom source function for testing adds a custom source in Python StreamExecutionEnvironment.

##########
File path: flink-python/pyflink/datastream/stream_execution_environment.py
##########
@@ -17,7 +17,6 @@
 ################################################################################
 import os
 import tempfile
-

Review comment:
       remove the unnecessary changes.




----------------------------------------------------------------
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