本地部署

安装

  1. 在官网安装Flink,并解压到/usr/local/flink

    • sudo tar -zxf flink-1.6.2-bin-hadoop27-scala_2.11.tgz -C /usr/local
      cd /usr/local
      
    • 54388226982

  2. 修改文件名字,并设置权限

    • sudo mv ./flink-*/ ./flink
      sudo chown -R hadoop:hadoop ./flink
      

修改配置文件

  • Flink对于本地模式是开箱即用的,如果要修改Java运行环境,可修改conf/flink-conf.yaml中的env.java.home,设置为本地java的绝对路径

添加环境变量

vim ~/.bashrc
export FLINK_HOME=/usr/local/flink
export PATH=$FLINK_HOME/bin:$PATH

54388242695

启动Flink

start-cluster.sh
  • 可以通过观察logs目录下的日志来检测系统是否正在运行了
tail log/flink--jobmanager-.log

54388315301

  • JobManager同时会在8081端口上启动一个web前端,通过http://localhost:8081来访问

54388290147

可以发现flink已经正常启动

运行示例

使用Maven创建Flink项目,在pom.xml中添加以下依赖:

    <dependencies><dependency><groupId>org.apache.flink</groupId><artifactId>flink-java</artifactId><version>1.6.2</version></dependency><dependency><groupId>org.apache.flink</groupId><artifactId>flink-streaming-java_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>

批处理运行WordCount

官方示例

可以直接在/usr/local/flink/examples/batch中运行WordCount程序,并且这里还有更多示例:

54388437325

运行:

flink run WordCount.jar

54388443638

代码

WordCountData

提供原始数据

import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;public class WordCountData {public static final String[] WORDS=new String[]{"To be, or not to be,--that is the question:--", "Whether \'tis nobler in the mind to suffer", "The slings and arrows of outrageous fortune", "Or to take arms against a sea of troubles,", "And by opposing end them?--To die,--to sleep,--", "No more; and by a sleep to say we end", "The heartache, and the thousand natural shocks", "That flesh is heir to,--\'tis a consummation", "Devoutly to be wish\'d. To die,--to sleep;--", "To sleep! perchance to dream:--ay, there\'s the rub;", "For in that sleep of death what dreams may come,", "When we have shuffled off this mortal coil,", "Must give us pause: there\'s the respect", "That makes calamity of so long life;", "For who would bear the whips and scorns of time,", "The oppressor\'s wrong, the proud man\'s contumely,", "The pangs of despis\'d love, the law\'s delay,", "The insolence of office, and the spurns", "That patient merit of the unworthy takes,", "When he himself might his quietus make", "With a bare bodkin? who would these fardels bear,", "To grunt and sweat under a weary life,", "But that the dread of something after death,--", "The undiscover\'d country, from whose bourn", "No traveller returns,--puzzles the will,", "And makes us rather bear those ills we have", "Than fly to others that we know not of?", "Thus conscience does make cowards of us all;", "And thus the native hue of resolution", "Is sicklied o\'er with the pale cast of thought;", "And enterprises of great pith and moment,", "With this regard, their currents turn awry,", "And lose the name of action.--Soft you now!", "The fair Ophelia!--Nymph, in thy orisons", "Be all my sins remember\'d."};public WordCountData() {}public static DataSet<String> getDefaultTextLineDataset(ExecutionEnvironment env){return env.fromElements(WORDS);}
}

WordCountTokenizer

切分句子

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;public class WordCountTokenizer implements FlatMapFunction<String, Tuple2<String,Integer>>{public WordCountTokenizer(){}public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {String[] tokens = value.toLowerCase().split("\\W+");int len = tokens.length;for(int i = 0; i<len;i++){String tmp = tokens[i];if(tmp.length()>0){out.collect(new Tuple2<String, Integer>(tmp,Integer.valueOf(1)));}}}
}

WordCount

主函数

import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.AggregateOperator;
import org.apache.flink.api.java.utils.ParameterTool;public class WordCount {public WordCount(){}public static void main(String[] args) throws Exception {ParameterTool params = ParameterTool.fromArgs(args);ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();env.getConfig().setGlobalJobParameters(params);Object text;//如果没有指定输入路径,则默认使用WordCountData中提供的数据if(params.has("input")){text = env.readTextFile(params.get("input"));}else{System.out.println("Executing WordCount example with default input data set.");System.out.println("Use -- input to specify file input.");text = WordCountData.getDefaultTextLineDataset(env);}AggregateOperator counts = ((DataSet)text).flatMap(new WordCountTokenizer()).groupBy(new int[]{0}).sum(1);//如果没有指定输出,则默认打印到控制台if(params.has("output")){counts.writeAsCsv(params.get("output"),"\n", " ");env.execute();}else{System.out.println("Printing result to stdout. Use --output to specify output path.");counts.print();}}
}

首先打包成JAR包,这里需要使用-c指定main函数:

flink run -c WordCount WordCount.jar

流处理运行WordCount

官方示例

可以直接在/usr/local/flink/examples/streaming中运行WordCount程序,并且这里还有更多示例:

54388669798

代码

SocketWindowWordCount

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;import java.sql.Time;
import java.util.stream.Collector;public class SocketWindowWordCount {public static void main(String[] args) throws Exception {// the port to connect tofinal 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 environmentfinal StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();// get input data by connecting to the socketDataStream<String> text = env.socketTextStream("localhost", port, "\n");// parse the data, group it, window it, and aggregate the countsDataStream<WordWithCount> windowCounts = text.flatMap(new FlatMapFunction<String, WordWithCount>() {@Overridepublic 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>() {@Overridepublic 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 parallelwindowCounts.print().setParallelism(1);env.execute("Socket Window WordCount");}// Data type for words with countpublic static class WordWithCount {public String word;public long count;public WordWithCount() {}public WordWithCount(String word, long count) {this.word = word;this.count = count;}@Overridepublic String toString() {return word + " : " + count;}}
}

首先打包成JAR包,然后启动netcat

nc -l 9000

将终端启动netcat作为输入流:

提交Jar包:

flink run -c SocketWindowWordCount WordCountSteaming.jar --port 9000

这样终端会一直等待netcat的输入流

54388822906

在netcat中输入字符流:

54388825265

可以在WebUI中查看运行结果:

54388897680

作者:zealscott
链接:https://www.jianshu.com/p/bbaa8d72cfcf/
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

Flink安装及使用相关推荐

  1. 2021年大数据Flink(三):​​​​​​​Flink安装部署 Local本地模式

    目录 Flink安装部署 Local本地模式 原理 操作 测试 Flink安装部署 Flink支持多种安装模式 - Local-本地单机模式,学习测试时使用 - Standalone-独立集群模式,F ...

  2. Flink安装与编程实践(Flink1.9.1)

    Flink安装与编程实践(Flink1.9.1) 1.安装Flink Flink的运行需要Java环境的支持,因此,在安装Flink之前,请先参照相关资料安装Java环境(比如Java8).然后,到F ...

  3. 学习笔记Flink(三)—— Flink安装启动与监控

    一.Linux环境准备 Centos7, 1 CPU , 2G Memory ,20G Disk , Virtual System Hostname : node110.centos.com .nod ...

  4. 第一天:什么是Flink、WordCount入门、Flink安装、并行度

    1. 初识 Flink 在当前数据量激增的时代,各种业务场景都有大量的业务数据产生,对于这些不断产的数据应该如何进行有效的处理,成为当下大多数公司所面临的问题.目前比较流行的大数据处理引擎 Apach ...

  5. 大数据Flink安装部署

    目录 1 Local本地模式 1.1 原理 1.2 操作 1.3 测试 2 Standalone独立集群模式 2.1 原理 2.2 操作 2.3 测试 3 Standalone-HA高可用集群模式 3 ...

  6. 0001-Flink安装---Flink安装(Standlone模式)

    一.获取安装包,上传至服务器并解压 在安装Flink之前,我们先要获取到flink-1.10.1-bin-scala_2.12.tgz.zip安装包. (注意:Flin有两个版本分别是flink-1. ...

  7. 凌波微步Flink——Flink安装及简单实用

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/88819199 本文出自[我是干勾鱼的博客] Ingredients: Java: ...

  8. flink安装以及运行自带wordcount示例(单机版,无hadoop环境)

    1.下载安装包到/opt目录 2.解压安装包 tar zxf flink-1.6.1-bin-hadoop26-scala_2.11.tgz 3.启动flink cd /opt/flink-1.6.1 ...

  9. Flink安装及运行说明

    目录 Flink部署Linux集群版 Flink任务提交方式 Flink运行架构 Flink部署Linux集群版 修改flink-conf.yaml,指定master节点地址 修改masters,配置 ...

最新文章

  1. 如何用知识图谱挖掘商业数据背后的宝藏?
  2. Asp.net常用的操作函数
  3. windows启动管理器_win7系统任务管理器的五种打开方式,很实用,学习一下
  4. 将数组前n个和后m-n个整体逆置的实现
  5. mybatis插入数据后返回自增主键ID详解
  6. JQuery AJAX基本使用
  7. 三、Tableau筛选器的使用
  8. 使用HTML5 canvas做地图(1)基础知识
  9. 亲密关系沟通-【匹配度】调整沟通模式
  10. 测试总结该怎么写...
  11. DIY远程控制开关(tiny6410+LED+yeelink+curl)
  12. zk不同页面之间的即时刷新
  13. 韩信点兵php,说说大脑的“同时性信息加工功能”
  14. 微信api接口调用-微信群管理
  15. 微信小程序04 数据绑定
  16. 计算机设备招标书范文,计算机设备招标书
  17. 华氏温度转换为摄氏温度(PTA厦大慕课)
  18. 外观检验人员一致性(Kappa)分析
  19. excel2010 向程序发送命令时出现问题
  20. Linux查看服务器硬件网卡cpu型号内存BIOS、主板型号信息

热门文章

  1. python遍历目录下所有文件_Python递归遍历目录下所有文件
  2. 远程访问及控制(详解)——SSH远程管理及TCP Wrappers 访问控制
  3. python 声明变量_Python的变量声明
  4. linux笔记之 开机服务启动的控制,系统日志的查看,防火墙的关闭
  5. seir模型的微分方程怎么写_抖音文案怎么写?6种热门文案写作模型
  6. java修车_JAVA小练习34——使用java描述一个车类与一个修车厂类
  7. linux点亮硬盘灯命令 简书,威联通NAS交流学习:用虚拟机安装荒野无灯大佬的精简win10系统...
  8. mysql 主键 下一个值_INNODB自增主键的一些问题 vs mysql获得自增字段下一个值
  9. 华为新系统鸿蒙有哪些手机_华为鸿蒙OS系统传来新消息!外媒宣布:未来几年内华为手机都将无缘...
  10. java中hashcode作用_Java中hashCode的作用