/** * Creates a new data stream that contains the strings received infinitely from a socket. Received strings are * decoded by the system's default character set. The reader is terminated immediately when the socket is down. * * @param hostname * The host name which a server socket binds * @param port * The port number which a server socket binds. A port number of 0 means that the port number is automatically * allocated. * @param delimiter * A character which splits received strings into records * @return A data stream containing the strings received from the socket * * @deprecated Use {@link #socketTextStream(String, int, String)} instead. */ @Deprecated @SuppressWarnings("deprecation") public DataStreamSource<String> socketTextStream(String hostname, int port, char delimiter) { return socketTextStream(hostname, port, delimiter, 0); }
/** * Creates a new data stream that contains the strings received infinitely from a socket. Received strings are * decoded by the system's default character set. On the termination of the socket server connection retries can be * initiated. * * <p>Let us note that the socket itself does not report on abort and as a consequence retries are only initiated when * the socket was gracefully terminated. * * @param hostname * The host name which a server socket binds * @param port * The port number which a server socket binds. A port number of 0 means that the port number is automatically * allocated. * @param delimiter * A character which splits received strings into records * @param maxRetry * The maximal retry interval in seconds while the program waits for a socket that is temporarily down. * Reconnection is initiated every second. A number of 0 means that the reader is immediately terminated, * while * a negative value ensures retrying forever. * @return A data stream containing the strings received from the socket * * @deprecated Use {@link #socketTextStream(String, int, String, long)} instead. */ @Deprecated public DataStreamSource<String> socketTextStream(String hostname, int port, char delimiter, long maxRetry) { return socketTextStream(hostname, port, String.valueOf(delimiter), maxRetry); }
/** * Creates a new data stream that contains the strings received infinitely from a socket. Received strings are * decoded by the system's default character set, using"\n" as delimiter. The reader is terminated immediately when * the socket is down. * * @param hostname * The host name which a server socket binds * @param port * The port number which a server socket binds. A port number of 0 means that the port number is automatically * allocated. * @return A data stream containing the strings received from the socket */ @PublicEvolving public DataStreamSource<String> socketTextStream(String hostname, int port) { return socketTextStream(hostname, port, "\n"); }
/** * Creates a new data stream that contains the strings received infinitely from a socket. Received strings are * decoded by the system's default character set. The reader is terminated immediately when the socket is down. * * @param hostname * The host name which a server socket binds * @param port * The port number which a server socket binds. A port number of 0 means that the port number is automatically * allocated. * @param delimiter * A string which splits received strings into records * @return A data stream containing the strings received from the socket */ @PublicEvolving public DataStreamSource<String> socketTextStream(String hostname, int port, String delimiter) { return socketTextStream(hostname, port, delimiter, 0); }
/** * A thin wrapper layer over {@link StreamExecutionEnvironment#socketTextStream(java.lang.String, int)}. * * @param host The host name which a server socket binds * @param port The port number which a server socket binds. A port number of 0 means that the port number is automatically * allocated. * @return A python data stream containing the strings received from the socket */ public PythonDataStream socket_text_stream(String host, int port) { return new PythonDataStream<>(env.socketTextStream(host, port).map(new AdapterMap<String>())); }
DataStream<String> text = env.socketTextStream(hostname, port, "\n");
/** * Creates a new data stream that contains the strings received infinitely from a socket. Received strings are * decoded by the system's default character set. The reader is terminated immediately when the socket is down. * * @param hostname * The host name which a server socket binds * @param port * The port number which a server socket binds. A port number of 0 means that the port number is automatically * allocated. * @param delimiter * A character which splits received strings into records * @return A data stream containing the strings received from the socket * * @deprecated Use {@link #socketTextStream(String, int, String)} instead. */ @Deprecated @SuppressWarnings("deprecation") public DataStreamSource<String> socketTextStream(String hostname, int port, char delimiter) { return socketTextStream(hostname, port, delimiter, 0); }
/** * Creates a new data stream that contains the strings received infinitely from a socket. Received strings are * decoded by the system's default character set. The reader is terminated immediately when the socket is down. * * @param hostname * The host name which a server socket binds * @param port * The port number which a server socket binds. A port number of 0 means that the port number is automatically * allocated. * @param delimiter * A character which splits received strings into records * @return A data stream containing the strings received from the socket * * @deprecated Use {@link #socketTextStream(String, int, String)} instead. */ @Deprecated @SuppressWarnings("deprecation") public DataStreamSource<String> socketTextStream(String hostname, int port, char delimiter) { return socketTextStream(hostname, port, delimiter, 0); }
/** * Creates a new data stream that contains the strings received infinitely from a socket. Received strings are * decoded by the system's default character set. The reader is terminated immediately when the socket is down. * * @param hostname * The host name which a server socket binds * @param port * The port number which a server socket binds. A port number of 0 means that the port number is automatically * allocated. * @param delimiter * A character which splits received strings into records * @return A data stream containing the strings received from the socket * * @deprecated Use {@link #socketTextStream(String, int, String)} instead. */ @Deprecated @SuppressWarnings("deprecation") public DataStreamSource<String> socketTextStream(String hostname, int port, char delimiter) { return socketTextStream(hostname, port, delimiter, 0); }
/** * Creates a new data stream that contains the strings received infinitely from a socket. Received strings are * decoded by the system's default character set. The reader is terminated immediately when the socket is down. * * @param hostname * The host name which a server socket binds * @param port * The port number which a server socket binds. A port number of 0 means that the port number is automatically * allocated. * @param delimiter * A character which splits received strings into records * @return A data stream containing the strings received from the socket * * @deprecated Use {@link #socketTextStream(String, int, String)} instead. */ @Deprecated @SuppressWarnings("deprecation") public DataStreamSource<String> socketTextStream(String hostname, int port, char delimiter) { return socketTextStream(hostname, port, delimiter, 0); }
/** * Creates a new data stream that contains the strings received infinitely from a socket. Received strings are * decoded by the system's default character set, using"\n" as delimiter. The reader is terminated immediately when * the socket is down. * * @param hostname * The host name which a server socket binds * @param port * The port number which a server socket binds. A port number of 0 means that the port number is automatically * allocated. * @return A data stream containing the strings received from the socket */ @PublicEvolving public DataStreamSource<String> socketTextStream(String hostname, int port) { return socketTextStream(hostname, port, "\n"); }
/** * Creates a new data stream that contains the strings received infinitely from a socket. Received strings are * decoded by the system's default character set, using"\n" as delimiter. The reader is terminated immediately when * the socket is down. * * @param hostname * The host name which a server socket binds * @param port * The port number which a server socket binds. A port number of 0 means that the port number is automatically * allocated. * @return A data stream containing the strings received from the socket */ @PublicEvolving public DataStreamSource<String> socketTextStream(String hostname, int port) { return socketTextStream(hostname, port, "\n"); }
/** * Creates a new data stream that contains the strings received infinitely from a socket. Received strings are * decoded by the system's default character set, using"\n" as delimiter. The reader is terminated immediately when * the socket is down. * * @param hostname * The host name which a server socket binds * @param port * The port number which a server socket binds. A port number of 0 means that the port number is automatically * allocated. * @return A data stream containing the strings received from the socket */ @PublicEvolving public DataStreamSource<String> socketTextStream(String hostname, int port) { return socketTextStream(hostname, port, "\n"); }
/** * Creates a new data stream that contains the strings received infinitely from a socket. Received strings are * decoded by the system's default character set. The reader is terminated immediately when the socket is down. * * @param hostname * The host name which a server socket binds * @param port * The port number which a server socket binds. A port number of 0 means that the port number is automatically * allocated. * @param delimiter * A string which splits received strings into records * @return A data stream containing the strings received from the socket */ @PublicEvolving public DataStreamSource<String> socketTextStream(String hostname, int port, String delimiter) { return socketTextStream(hostname, port, delimiter, 0); }
/** * Creates a new data stream that contains the strings received infinitely from a socket. Received strings are * decoded by the system's default character set, using"\n" as delimiter. The reader is terminated immediately when * the socket is down. * * @param hostname * The host name which a server socket binds * @param port * The port number which a server socket binds. A port number of 0 means that the port number is automatically * allocated. * @return A data stream containing the strings received from the socket */ @PublicEvolving public DataStreamSource<String> socketTextStream(String hostname, int port) { return socketTextStream(hostname, port, "\n"); }
/** * Creates a new data stream that contains the strings received infinitely from a socket. Received strings are * decoded by the system's default character set. The reader is terminated immediately when the socket is down. * * @param hostname * The host name which a server socket binds * @param port * The port number which a server socket binds. A port number of 0 means that the port number is automatically * allocated. * @param delimiter * A string which splits received strings into records * @return A data stream containing the strings received from the socket */ @PublicEvolving public DataStreamSource<String> socketTextStream(String hostname, int port, String delimiter) { return socketTextStream(hostname, port, delimiter, 0); }
/** * Creates a new data stream that contains the strings received infinitely from a socket. Received strings are * decoded by the system's default character set. The reader is terminated immediately when the socket is down. * * @param hostname * The host name which a server socket binds * @param port * The port number which a server socket binds. A port number of 0 means that the port number is automatically * allocated. * @param delimiter * A string which splits received strings into records * @return A data stream containing the strings received from the socket */ @PublicEvolving public DataStreamSource<String> socketTextStream(String hostname, int port, String delimiter) { return socketTextStream(hostname, port, delimiter, 0); }
/** * Creates a new data stream that contains the strings received infinitely from a socket. Received strings are * decoded by the system's default character set. The reader is terminated immediately when the socket is down. * * @param hostname * The host name which a server socket binds * @param port * The port number which a server socket binds. A port number of 0 means that the port number is automatically * allocated. * @param delimiter * A string which splits received strings into records * @return A data stream containing the strings received from the socket */ @PublicEvolving public DataStreamSource<String> socketTextStream(String hostname, int port, String delimiter) { return socketTextStream(hostname, port, delimiter, 0); }
@Override public Object doOperation(final AddElementsFromSocket op, final Context context, final Store store) throws OperationException { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); if (null != op.getParallelism()) { env.setParallelism(op.getParallelism()); } final DataStream<Element> builder = env.socketTextStream(op.getHostname(), op.getPort(), op.getDelimiter()) .flatMap(new GafferMapFunction(String.class, op.getElementGenerator())); if (Boolean.parseBoolean(op.getOption(FlinkConstants.SKIP_REBALANCING))) { builder.addSink(new GafferSink(op, store)); } else { builder.rebalance().addSink(new GafferSink(op, store)); } try { env.execute(op.getClass().getSimpleName() + "-" + op.getHostname() + ":" + op.getPort()); } catch (final Exception e) { throw new OperationException("Failed to add elements from port: " + op.getPort(), e); } return null; } }
public static void main(String[] args) throws Exception { // 创建 execution environment StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); // 通过连接 socket 获取输入数据,这里连接到本地9000端口,如果9000端口已被占用,请换一个端口 DataStream<String> text = env.socketTextStream("localhost", 9000, "\n"); // 解析数据,按 word 分组,开窗,聚合 DataStream<Tuple2<String, Integer>> windowCounts = text .flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() { @Override public void flatMap(String value, Collector<Tuple2<String, Integer>> out) { for (String word : value.split("\\s")) { out.collect(Tuple2.of(word, 1)); } } }) .keyBy(0) .timeWindow(Time.seconds(5)) .sum(1); // 将结果打印到控制台,注意这里使用的是单线程打印,而非多线程 windowCounts.print().setParallelism(1); env.execute("Socket Window WordCount"); } }