You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by GitBox <gi...@apache.org> on 2022/03/01 11:30:38 UTC

[GitHub] [incubator-seatunnel] leo65535 commented on a change in pull request #1296: [Feature][connector] Add flink socket connector based on table api

leo65535 commented on a change in pull request #1296:
URL: https://github.com/apache/incubator-seatunnel/pull/1296#discussion_r816686544



##########
File path: seatunnel-connectors/seatunnel-connector-flink-socket/src/main/java/org/apache/seatunnel/flink/connector/socket/source/SocketSourceFunction.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.seatunnel.flink.connector.socket.source;
+
+import org.apache.flink.api.common.serialization.DeserializationSchema;
+import org.apache.flink.api.common.serialization.RuntimeContextInitializationContextAdapters;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.streaming.api.functions.source.RichSourceFunction;
+import org.apache.flink.table.data.RowData;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+
+/**
+ * The {@link SocketSourceFunction} opens a socket and consumes bytes.
+ *
+ * <p>It splits records by the given byte delimiter (`\n` by default) and delegates the decoding to
+ * a pluggable {@link DeserializationSchema}.
+ *
+ * <p>Note: This is only an example and should not be used in production. The source function is not
+ * fault-tolerant and can only work with a parallelism of 1.
+ */
+public final class SocketSourceFunction extends RichSourceFunction<RowData>

Review comment:
       Done

##########
File path: seatunnel-connectors/seatunnel-connector-flink-socket/src/main/java/org/apache/seatunnel/flink/connector/socket/source/SocketSourceFunction.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.seatunnel.flink.connector.socket.source;
+
+import org.apache.flink.api.common.serialization.DeserializationSchema;
+import org.apache.flink.api.common.serialization.RuntimeContextInitializationContextAdapters;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.ResultTypeQueryable;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.streaming.api.functions.source.RichSourceFunction;
+import org.apache.flink.table.data.RowData;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+
+/**
+ * The {@link SocketSourceFunction} opens a socket and consumes bytes.
+ *
+ * <p>It splits records by the given byte delimiter (`\n` by default) and delegates the decoding to
+ * a pluggable {@link DeserializationSchema}.
+ *
+ * <p>Note: This is only an example and should not be used in production. The source function is not
+ * fault-tolerant and can only work with a parallelism of 1.
+ */
+public final class SocketSourceFunction extends RichSourceFunction<RowData>
+        implements ResultTypeQueryable<RowData> {
+
+    private static final Logger LOG = LoggerFactory.getLogger(SocketSourceFunction.class);
+
+    private final String hostname;
+    private final int port;
+    private final byte byteDelimiter;
+    private final DeserializationSchema<RowData> deserializer;
+    private static final int WAIT_TIME = 1000;
+
+    private volatile boolean isRunning = true;
+    private Socket currentSocket;
+
+    public SocketSourceFunction(
+            String hostname,
+            int port,
+            byte byteDelimiter,
+            DeserializationSchema<RowData> deserializer) {
+        this.hostname = hostname;
+        this.port = port;
+        this.byteDelimiter = byteDelimiter;
+        this.deserializer = deserializer;
+    }
+
+    @Override
+    public TypeInformation<RowData> getProducedType() {
+        return deserializer.getProducedType();
+    }
+
+    @Override
+    public void open(Configuration parameters) throws Exception {
+        deserializer.open(
+                RuntimeContextInitializationContextAdapters.deserializationAdapter(
+                        getRuntimeContext()));
+    }
+
+    @Override
+    public void run(SourceContext<RowData> ctx) throws Exception {
+        while (isRunning) {
+            // open and consume from socket
+            try (final Socket socket = new Socket()) {
+                currentSocket = socket;
+                socket.connect(new InetSocketAddress(hostname, port), 0);
+                try (InputStream stream = socket.getInputStream()) {
+                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+                    int b;
+                    while ((b = stream.read()) >= 0) {
+                        // buffer until delimiter
+                        if (b != byteDelimiter) {
+                            buffer.write(b);
+                        }
+                        // decode and emit record
+                        else {
+                            ctx.collect(deserializer.deserialize(buffer.toByteArray()));
+                            buffer.reset();
+                        }
+                    }
+                }
+            } catch (Exception e) {
+                LOG.warn("Meet exception when reading from socket", e);
+            }
+            Thread.sleep(WAIT_TIME);
+        }
+    }
+
+    @Override
+    public void cancel() {
+        isRunning = false;
+        try {
+            currentSocket.close();
+        } catch (Exception t) {
+            // ignore

Review comment:
       Make sense, thanks.




-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org