知识点

Flink介绍
1、无界数据-->数据不断产生
2、有界数据-->最终不再改变的数据
3、有界数据集是无界数据集的一个特例
4、有界数据集在flink内部是以一种终态数据集进行处理的
5、在flink中,有界和无界的差别非常小
6、使用同一套流计算引擎上的API操作两种数据类型 流计算:
数据不断产生,就一直处于计算状态
批处理:
完成一定时间段的计算任务后,就释放资源 Flink特性:
结果精准,即使是无序数据或者延迟到达的数据
有状态以及容错。
有状态,表示一直保存计算结果,以便往后传递计算值 实现精准一次计算的应用状态
大规模计算,几千台节点上运算,高吞吐和低延迟的特点
Flink通过检查点机制实现精准一次的计算保证,在故障时可以体现出来
flink支持流计算以及窗口化操作
flink支持灵活的基础时间的窗口计算
flink容错是轻量级的,保证零数据丢失。

1、下载并安装

官网安装步骤:https://ci.apache.org/projects/flink/flink-docs-release-1.8/tutorials/local_setup.html

2、本人安装版本

1、flink-1.7.2-bin-hadoop24-scala_2.11.tgz
2、tar -xzvf flink-1.7.2-bin-hadoop24-scala_2.11.tgz
3、mv flink-1.7.2 /usr/local/flink

3、运行flink

./bin/start-cluster.sh 

4、web ui查看flink界面

http://ip:8081

5、查看日志信息

查看flink启动日志信息 log/flink-root-standalonesession-0-localhost.localdomain.log
查看job任务启动信息 log/flink-root-taskexecutor-0-localhost.localdomain.log
查看job任务输出信息 tail -100f flink-root-taskexecutor-0-localhost.localdomain.out

6、编写wordcout程序,这个可以查看官网

  a)pom.xml , 注意将<scope>provided</scope>注释,否则找不到dataset类

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.ywj</groupId>
<artifactId>flink.test</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.flink/flink-core -->
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-core</artifactId>
<version>1.7.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.flink/flink-streaming-java -->
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-java_2.12</artifactId>
<version>1.7.2</version>
<!--<scope>provided</scope>-->
</dependency> </dependencies>
</project>

  b)SocketWindowWordCount.java

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.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector; public class SocketWindowWordCount { public static void main(String[] args) throws Exception { // 定义连接端口
final int port=8888;
// 得到执行环境对象
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); // 连接套接字socket之后,读取输入数据text
DataStream<String> text = env.socketTextStream("localhost", port, "\n"); // 解析数据:分组,窗口,聚合,计数
DataStream<WordWithCount> windowCounts = text
.flatMap(new FlatMapFunction<String, WordWithCount>() {
public void flatMap(String value, Collector<WordWithCount> out) {
//value.split("\\s") 将value按照空格或者制表符等进行切割
for (String word : value.split("\\s")) {//value为输入的一行数据
out.collect(new WordWithCount(word, 1L)); //封装成WordWithCount对象
}
}
})
.keyBy("word") //根据key进行分组 ,word和 new WordWithCount(word,1L)中的word要一致
.timeWindow(Time.seconds(5),Time.seconds(1))//5秒一窗口,1秒一计算
.reduce(new ReduceFunction<WordWithCount>() {
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;
}
}
}

  c)Exception in thread "main" java.lang.VerifyError: Uninitialized object exists on backward branch 96,这种错误

请升级JDK版本,本人使用的是jdk8-211版本

7、打包运行

a) mvn package    打成jar包,放入centos中
b)查看flinks时候运行 netstat -ano | grep 8081
c) nc -l 8888 -v
d) ./bin/flink run -c SocketWindowWordCount /home/ywj/flink.test-1.0-SNAPSHOT.jar
e) 查看输出信息 tail -100f flink-root-taskexecutor-0-localhost.localdomain.out

############如果在windows进行测试,可以在win10中使用netcat#######

netcat使用

netcat测试
linux (centos)
1、nc 192.168.227.128 5000 客户端
2、 nc -l 5000 -v 服务端 windows(win10)
1、nc -L -p 8888 服务端
2、nc localhost 8888 客户端

windows调试wordcout

windows10测试:
7、cmd启动执行 nc -L -p 8888
8、运行flink代码

Flink初探wordCout的更多相关文章

  1. Flink初探-为什么选择Flink

    本文主要记录一些关于Flink与storm,spark的区别, 优势, 劣势, 以及为什么这么多公司都转向Flink. What Is Flink 一个通俗易懂的概念: Apache Flink 是近 ...

  2. Flink的sink实战之一:初探

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  3. Flink SQL Client初探

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  4. [转]为何选择 Flink

    本文转自:https://www.ituring.com.cn/book/tupubarticle/23229 第 1 章 为何选择 Flink 人们对某件事的正确理解往往来自基于有效论据的结论.要获 ...

  5. 入门大数据---Flink学习总括

    第一节 初识 Flink 在数据激增的时代,催生出了一批计算框架.最早期比较流行的有MapReduce,然后有Spark,直到现在越来越多的公司采用Flink处理.Flink相对前两个框架真正做到了高 ...

  6. Flink的sink实战之二:kafka

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  7. Flink的sink实战之三:cassandra3

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  8. Flink的sink实战之四:自定义

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  9. Flink SQL Client综合实战

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

随机推荐

  1. linux在菜单中添加SEG选项

  2. 微信小程序html(wxml)传参

    欢迎加入前端交流群交流知识:749539640 习惯了vue.angular用微信小程序有时候真感觉非人类..需要用data-xxx 先说下我们在vue.angular里事件传参 //html < ...

  3. 使用Mysql-magic获取Mysql账户密码

    版权声明:本文为博主原创文章,欢迎转载,转载请注明原文超链接https://www.cnblogs.com/zerotrust/p/10846530.html 本文仅限于技术讨论与分享,严禁用于非法用 ...

  4. Physical Education Lessons CodeForces - 915E (动态开点线段树)

    Physical Education Lessons CodeForces - 915E This year Alex has finished school, and now he is a fir ...

  5. EasyUi Datagrid中footer renderFooter

    默认的'rowStyler' 选项不支持footer,想让footer支持rowStyler的话,dategird就得重写.代码如下. var myview = $.extend({}, $.fn.d ...

  6. PHP 提取数组中奇数或偶数的元素array_filter

    //提取奇数 $filter = array_filter($ql,function($var){ return($var & 1); },ARRAY_FILTER_USE_KEY); pri ...

  7. CentOS 6 自定义单实例 二进制方式 安装mariadb-5.5.59

    系统平台: CentOS release 6.9 (Final) 内核 2.6.32-696.el6.x86_64 1.去官网下载适合的二进制包 http://mariadb.org/ mariadb ...

  8. dcm4che-core导包失败! mvn pom文件导包总是失败

    原因可能是所导的包不在共有项目中,可能在个人项目中,需要添加远程仓库 <!--远程仓库部署--><repositories> <repository> <id ...

  9. MultipartFile类

    MultipartFile类常用的一些方法: String getContentType()//获取文件MIME类型InputStream getInputStream()//后去文件流String ...

  10. 题解 [BZOJ1295][SCOI2009] 最长距离

    题面 解析 \(n\)只有\(30\)可以直接枚举每个矩形, 判断他们的左上角到右下角或右上角到左上角的最短路是否小于\(T\). 最短路可以用\(dijkstra\). 一开始想用\(DP\)写最短 ...