Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the simplest helloWorld

2025-02-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to write the simplest helloWorld. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Experimental environment

JDK 1.8

IDE Intellij idea

Flink 1.8.1

Content of the experiment

Create a Flink simple Demo that counts the number of words from the stream data.

Experimental procedure

First, create a maven project with the following contents in the pom.xml file:

1.8.1 org.apache.flink flink-java ${flink.version} org.apache.flink flink-streaming-java_2.11 ${flink.version} org.apache.flink flink-streaming-scala_2.11 ${flink.version} org.apache.flink flink-connector-wikiedits_2.11 ${flink.version} org.apache.maven.plugins maven-compiler-plugin 8 8 org.springframework.boot spring-boot-maven-plugin 2.1.4.RELEASE wikiedits.StreamingJob Repackage org.apache.maven.plugins maven-surefire-plugin true

Create a package com.vincent and create a class StreamingJob.java

Public class WikipediaAnalysis {public static void main (String [] args) throws Exception {}}

The first step in the Flink program is to create a StreamExecutionEnvironment. StreamExecutionEnvironment can set parameters and import data sources from external systems.

Final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment ()

Next, create an external data source that uses nc-l 9000 to indicate that port 9000 is open on the server side and can send data.

DataStream text = env.socketTextStream ("192.168.152.45", 9000)

This adds a streaming text data source, and with DataStream, you can get the data, and then analyze the data:

DataStream dataStream = text.flatMap (new FlatMapFunction () {@ Override public void flatMap (String s, Collector collector) throws Exception {String [] tokens = s.toLowerCase () .split ("\\ W+"); for (String token: tokens) {if (token.length () > 0) {collector.collect (new Tuple2 (token, 1)) }) .keyby (0) .timeWindow (Time.seconds (5)) .sum (1)

FlatMap means to convert and tile a nested collection into a non-nested collection with a string of s and a return value of Collector. And the statistical addition operation is carried out according to keyBy (0), that is, the 0th field. .timeWindow () specifies that the window size is 5 seconds.

So the overall code is as follows:

Public class StreamingJob {public static void main (String [] args) throws Exception {final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment (); DataStream text = env.socketTextStream ("192.168.152.45", 9000); DataStream dataStream = text.flatMap (new FlatMapFunction () {@ Override public void flatMap (String s, Collector collector) throws Exception {String [] tokens = s.toLowerCase (). Split ("\\ W+") For (String token: tokens) {if (token.length () > 0) {collector.collect (new Tuple2 (token, 1));}) .keyby (0) .timeWindow (Time.seconds (5)) .sum (1); dataStream.print () / / execute program env.execute ("Java WordCount from SocketTextStream Example");}} run

Run the main method, then execute nc-l 9000 on the server side and enter the text:

Iie4bu@swarm-manager:~$ nc-l 9000a b d d e f

Then output in the intellij console:

1 > (bmeme1) 3 > (aPhone1) 1 > (fmagin1) 3 > (dPhone2) 1 > (ePerson1)

You can count the number of times per word.

The above is the editor for you to share how to write the simplest helloWorld, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report