首页 / MAC / 【Flink】Mac设置本地运行环境
【Flink】Mac设置本地运行环境
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【Flink】Mac设置本地运行环境,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3397字,纯文字阅读大概需要5分钟。
内容图文
下载并启动flink
运行flink,唯一的前置要求是就是安装了java8。
可以本地查看命令:
java -version
如果是安装了java8,则输出的命令则类似于:
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
对于mac系统,可以通过Homebrew来安装
$ brew install apache-flink
...
$ flink --version
Version: 1.2.0, Commit ID: 1c659cf
启动本地的flink集群
$ ./bin/start-cluster.sh # Start Flink
你可以检查 http://localhost:8081,是否有页面正常可以访问。
你也可以校验log文件路径的数据:
$ tail log/flink-*-standalonesession-*.log
INFO ... - Rest endpoint listening at localhost:8081
INFO ... - http://localhost:8081 was granted leadership ...
INFO ... - Web frontend listening at http://localhost:8081.
INFO ... - Starting RPC endpoint for StandaloneResourceManager at akka://flink/user/resourcemanager .
INFO ... - Starting RPC endpoint for StandaloneDispatcher at akka://flink/user/dispatcher .
INFO ... - ResourceManager akka.tcp://flink@localhost:6123/user/resourcemanager was granted leadership ...
INFO ... - Starting the SlotManager.
INFO ... - Dispatcher akka.tcp://flink@localhost:6123/user/dispatcher was granted leadership ...
INFO ... - Recovering all persisted jobs.
INFO ... - Registering TaskManager ... at ResourceManager
阅读代码
你可以编译并运行SocketWindowWordCount 代码,github地址为 SocketWindowWordCount
public class SocketWindowWordCount {
public static void main(String[] args) throws Exception {
// the port to connect to
final int port;
try {
final ParameterTool params = ParameterTool.fromArgs(args);
port = params.getInt("port");
} catch (Exception e) {
System.err.println("No port specified. Please run 'SocketWindowWordCount --port <port>'");
return;
}
// get the execution environment
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
// get input data by connecting to the socket
DataStream<String> text = env.socketTextStream("localhost", port, "\n");
// parse the data, group it, window it, and aggregate the counts
DataStream<WordWithCount> windowCounts = text
.flatMap(new FlatMapFunction<String, WordWithCount>() {
@Override
public void flatMap(String value, Collector<WordWithCount> out) {
for (String word : value.split("\\s")) {
out.collect(new WordWithCount(word, 1L));
}
}
})
.keyBy("word")
.timeWindow(Time.seconds(5), Time.seconds(1))
.reduce(new ReduceFunction<WordWithCount>() {
@Override
public WordWithCount reduce(WordWithCount a, WordWithCount b) {
return new 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");
}
// Data type for words with count
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;
}
}
}
运行示例
运行一个flink程序,它将从套接字读取文本,并且每5秒打印一次在前5秒内每个不同单词的出现次数。
首先,我们使用netcat启动本地服务器
$ nc -l 9000
提交flink程序
$ ./bin/flink run examples/streaming/SocketWindowWordCount.jar --port 9000
Starting execution of program
这个程序通过连接套接字接受等待输入。
$ nc -l 9000
lorem ipsum
ipsum ipsum ipsum
bye
那么就可以在log中看出print的输出
$ tail -f log/flink-*-taskexecutor-*.out
lorem : 1
bye : 1
ipsum : 4
最后停止flink集群的命令为:
$ ./bin/stop-cluster.sh
原文:https://www.cnblogs.com/yankang/p/11921022.html
内容总结
以上是互联网集市为您收集整理的【Flink】Mac设置本地运行环境全部内容,希望文章能够帮你解决【Flink】Mac设置本地运行环境所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。