flink structures and pseudo-distributed locally connected idea measured flink

Download and install flink:
Upload Package: flink-1.7.2-bin-scala_2.12.tgz
decompression: tar -zxvf /flink-1.7.2-bin-scala_2.12.tgz -C ../hone
copy the file to extract the child nodes:
SCP -R & lt /home/flink-1.7.2/ Slave1 the root @: / Home /
SCP -R & lt /home/flink-1.7.2/ slave2 the root @: / Home /
modify the configuration files: select a master node, Configuring the conf / conf.yaml Flink-
VI the conf / Flink-conf.yaml
provided jobmanager.rpc.address CI node hostname or IP for
jobmanager.rpc.address: 10.108.4.202
then add child node configuration:
in all node: flink directory under: vi conf / slaves
add all child nodes ip then save
start the local flink cluster:
cd to the directory under flink
./bin/start-cluster.sh
View webui: ip: 8081
start the listener: nc -lk 9000
when Times nc command does not exist (yum install nc)
and then perform a test jar:
stop flink cluster: bin / stop-cluster.sh
submit jobs to the cluster ways: under flink directory
./bin/flink run -m yarn-cluster -c com.demo.florian.WordCount $DEMO_DIR/target/flink-demo-1.0-SNAPSHOT.jar --port 9000

New program maven
pom.xml rely as follows:
Then create a TestSocketWindowWordCount class specific code as follows
and then start flink cluster -> Create a new listener: nc -lk 6666
and then start TestSocketWindowWordCount class
to enter the code in linux monitor page
observed there statistics idea Console output
------- pom.xml start -----------
<Dependencies>
<dependency>
<the groupId> org.apache.flink </ the groupId>
<the artifactId> Flink-Java </ the artifactId >
<Version> 1.9.0 </ Version>
<scope> the compile </ scope>
</ dependency>
<dependency>
<the groupId> org.apache.flink </ the groupId>
<the artifactId> Flink-Streaming-java_2.11 </ the artifactId>
<Version> 1.9.0 </ Version>
<scope> the compile </ scope>
</ dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-scala_2.11</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-clients_2.11</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
-------pom.xml结束-----------
-------TestSocketWindowWordCount开始------------------
package com.gyb;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import javax.xml.soap.Text;

public class TestSocketWindowWordCount {
public static void main(String args[]) {
String hostname = "192.168.198.130";
int port = 6666;
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStream text = env.socketTextStream(hostname, port, "\n");//获取执行环境
SingleOutputStreamOperator windowCounts = text
.flatMap(new FlatMapFunction<String, SocketWindowWordCount.WordWithCount>() {br/>@Override
public void flatMap(String value, Collector<SocketWindowWordCount.WordWithCount> out) {
for (String word : value.split("\s")) {
out.collect(new SocketWindowWordCount.WordWithCount(word, 1L));
}
}
})
.keyBy("word")
.timeWindow(Time.seconds(5), Time.seconds(5))
.reduce(new ReduceFunction<SocketWindowWordCount.WordWithCount>() {
br/>@Override
public SocketWindowWordCount.WordWithCount reduce(SocketWindowWordCount.WordWithCount a, SocketWindowWordCount.WordWithCount b) {
return new SocketWindowWordCount.WordWithCount(a.word, a.count + b.count);
}
});

// print the results with a single thread, rather than in parallel
windowCounts.print().setParallelism(1);
//env.execute("Socket Window WordCount");
try {
    env.execute("Socket Window WordCount");
} catch (Exception e) {
    e.printStackTrace();
}

}

public static class WordWithCount {

    public String word;
    public long count;

    public WordWithCount() {}

    public WordWithCount(String word, long count) {
        this.word = word;
        this.count = count;
    }

    @Override
    public String toString() {
        return word + " : " + count;
    }
}

}
------- End ------------------ TestSocketWindowWordCount

Guess you like

Origin blog.51cto.com/13184837/2437778