flink 并行计数器实现
1、flink实现计数器的灵感来源于Hadoop的MapReduce计算框架里的理念。
flink通过实现Accumulator接口实现并行计数。并行管理是由flink实现的。
public interface Accumulator<V, R extends Serializable> extends Serializable, Cloneable
计数的结果通过JobExecutionResul的getAccumulatorResult方法t获取。
2、示例,在正常业务处理流程中对空字段计数,空字段包括null、空格、TAB等内容。这场景比较多见。
public class EmptyFieldsCountAccumulator {
private static final String EMPTY_FIELD_ACCUMULATOR= "empty-fields";
public static void main(String args[]) throws Exception{
final ParameterTool params = ParameterTool.fromArgs(args);
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
// make parameters available in the web interface
env.getConfig().setGlobalJobParameters(params);
// get the data set
final DataSet<StringTriple> file = getDataSet(env, params);
// filter lines with empty fields
final DataSet<StringTriple> filteredLines = file.filter(new EmptyFieldFilter());
// Here, we could do further processing with the filtered lines...
JobExecutionResult result;
// output the filtered lines
if (params.has("output")) {
filteredLines.writeAsCsv(params.get("output"));
// execute program
result = env.execute("Accumulator example");
} else {
System.out.println("Printing result to stdout. Use --output to specify output path.");
filteredLines.print();
result = env.getLastJobExecutionResult();
}
// get the accumulator result via its registration key
final List<Integer> emptyFields = result.getAccumulatorResult(EMPTY_FIELD_ACCUMULATOR);
System.out.format("Number of detected empty fields per column: %s\n", emptyFields);
}
@SuppressWarnings("unchecked")
private static DataSet<StringTriple> getDataSet(ExecutionEnvironment env, ParameterTool params) {
if (params.has("input")) {
return env.readCsvFile(params.get("input"))
.fieldDelimiter(";")
.pojoType(StringTriple.class);
} else {
System.out.println("Executing EmptyFieldsCountAccumulator example with default input data set.");
System.out.println("Use --input to specify file input.");
return env.fromCollection(getExampleInputTuples());
}
}
private static Collection<StringTriple> getExampleInputTuples() {
Collection<StringTriple> inputTuples = new ArrayList<StringTriple>();
inputTuples.add(new StringTriple("John", "Doe", "Foo Str."));
inputTuples.add(new StringTriple("Joe", "Johnson", ""));
inputTuples.add(new StringTriple(null, "Kate Morn", "Bar Blvd."));
inputTuples.add(new StringTriple("Tim", "Rinny", ""));
inputTuples.add(new StringTriple("Alicia", "Jackson", " "));
inputTuples.add(new StringTriple("Alicia", "Jackson", " "));
inputTuples.add(new StringTriple("Alicia", "Jackson", " "));
inputTuples.add(new StringTriple("Tom", "Jackson", "A"));
inputTuples.add(new StringTriple("Amy", "li", "B "));
return inputTuples;
}
/**
* This function filters all incoming tuples that have one or more empty fields.
* In doing so, it also counts the number of empty fields per attribute with an accumulator (registered under
* {@link EmptyFieldsCountAccumulator#EMPTY_FIELD_ACCUMULATOR}).
*/
public static final class EmptyFieldFilter extends RichFilterFunction<StringTriple> {
// create a new accumulator in each filter function instance
// accumulators can be merged later on
private final VectorAccumulator emptyFieldCounter = new VectorAccumulator();
@Override
public void open(final Configuration parameters) throws Exception {
super.open(parameters);
// register the accumulator instance
getRuntimeContext().addAccumulator(EMPTY_FIELD_ACCUMULATOR,
this.emptyFieldCounter);
}
@Override
public boolean filter(final StringTriple t) {
boolean containsEmptyFields = false;
// iterate over the tuple fields looking for empty ones
for (int pos = 0; pos < t.getArity(); pos++) {
final String field = t.getField(pos);
if (field == null || field.trim().isEmpty()) {
containsEmptyFields = true;
// if an empty field is encountered, update the
// accumulator
this.emptyFieldCounter.add(pos);
}
}
return !containsEmptyFields;
}
}
/**
* This accumulator maintains a vector of counts. Calling {@link #add(Integer)} increments the
* <i>n</i>-th vector component. The size of the vector is automatically managed.
* 这个向量计数器输入是整数,输出是List,并按字段位置计数,List里的索引就是字段计数位置,其值就是计数结果
*/
public static class VectorAccumulator implements Accumulator<Integer,ArrayList<Integer>>{
//存储计数器向量
private final ArrayList<Integer> resultVector;
public VectorAccumulator() {
this(new ArrayList<>());
}
public VectorAccumulator(ArrayList<Integer> resultVector) {
this.resultVector = resultVector;
}
private void updateResultVector(int position,int delta){
//如果给出的位置不够就扩充向量容器
while (this.resultVector.size()<=position){
this.resultVector.add(0);
}
final int component = this.resultVector.get(position);
this.resultVector.set(position,component+delta);
}
//在指定位置加1
@Override
public void add(Integer position) {
updateResultVector(position,1);
}
@Override
public ArrayList<Integer> getLocalValue() {
return this.resultVector;
}
@Override
public void resetLocal() {
this.resultVector.clear();
}
@Override
public void merge(Accumulator<Integer, ArrayList<Integer>> other) {
//合并两个向量计数器容器,按容器的索引合并
final ArrayList<Integer> otherVector = other.getLocalValue();
for(int i=0;i<otherVector.size();i++){
updateResultVector(i,otherVector.get(i));
}
}
@Override
public Accumulator<Integer, ArrayList<Integer>> clone() {
return new VectorAccumulator(new ArrayList<>(this.resultVector));
}
@Override
public String toString() {
return StringUtils.join(this.resultVector,':');
}
}
public static class StringTriple extends Tuple3<String, String, String> {
public StringTriple() {}
public StringTriple(String f0, String f1, String f2) {
super(f0, f1, f2);
}
}
}
flink 并行计数器实现的更多相关文章
- 一文让你彻底了解大数据实时计算引擎 Flink
前言 在上一篇文章 你公司到底需不需要引入实时计算引擎? 中我讲解了日常中常见的实时需求,然后分析了这些需求的实现方式,接着对比了实时计算和离线计算.随着这些年大数据的飞速发展,也出现了不少计算的框架 ...
- [源码解析] 当 Java Stream 遇见 Flink
[源码解析] 当 Java Stream 遇见 Flink 目录 [源码解析] 当 Java Stream 遇见 Flink 0x00 摘要 0x01 领域 1.1 Flink 1.2 Java St ...
- Flink 的运行架构详细剖析
1. Flink 程序结构 Flink 程序的基本构建块是流和转换(请注意,Flink 的 DataSet API 中使用的 DataSet 也是内部流 ).从概念上讲,流是(可能永无止境的)数据记录 ...
- C# Parallel.Invoke 实现
Parallel.Invoke应该是Parallel几个方法中最简单的一个了,我们来看看它的实现,为了方法大家理解,我尽量保留源码中的注释: public static class Parallel ...
- Flink01
1. 什么是Flink? 1.1 4代大数据计算引擎 第一代: MapReducer 批处理 Mapper, Reducer Hadoop的MapReducer将计算分为两个阶段, 分别为Map和Re ...
- Flink Program Guide (1) -- 基本API概念(Basic API Concepts -- For Java)
false false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTable {mso-style-n ...
- Flink项目实战(一)---核心概念及基本使用
前言.flink介绍: Apache Flink 是一个分布式处理引擎,用于在无界和有界数据流上进行有状态的计算.通过对时间精确控制以及状态化控制,Flink能够运行在任何处理无界流的应用中,同时对有 ...
- flink03-----1.Task的划分 2.共享资源槽 3.flink的容错
1. Task的划分 在flink中,划分task的依据是发生shuffle(也叫redistrubute),或者是并行度发生变化 1. wordcount为例 package cn._51doit ...
- Flink调优
第1章 资源配置调优 Flink性能调优的第一步,就是为任务分配合适的资源,在一定范围内,增加资源的分配与性能的提升是成正比的,实现了最优的资源配置后,在此基础上再考虑进行后面论述的性能调优策略. ...
随机推荐
- [收藏]Dubbo官方资料
首页 || 下载 || 用户指南 || 开发者指南 || 管理员指南 || 培训文档 || 常见问题解答 || 发布记录 || 发展路线 || 社区 English ...
- [20190913]完善vim的bccacl插件2.txt
[20190913]完善vim的bccacl插件2.txt --//继续完善vim的bccacl插件.--//\bc 计算也可以直接使用 \bb 操作,这样操作更快一些.--//增加直接写好算式计算的 ...
- 去掉没用的参数的warning
#define UNUSED(x) ((void)(x)) void fun(int noused_arg, int b){ UNUSED(noused_arg); printf("%d\n ...
- linux环境下的Oracle部署
一. 环境及相关软件 虚拟机:VMwore Workstation Linux系统:CentOS ORACLE:ORACLE_112030_Linux-x86-64 Xmanger软件 二. 安装 ...
- RAW数据格式解析
RAM数据格式解析 Raw格式是sensor的输出格式,是未经处理过的数据,表示sensor接受 到的各种光的强度. Raw数据在输出的时候是有一定的顺序的,一般为以下四种: 00: GR/BG 01 ...
- docker卷管理(四)
什么是数据卷 overlay2存储卷 docker镜像是由多个只读层叠加而成,启动容器时,docker会加载只读镜像层,并在镜像栈顶部添加一个可写层 如果运行中的容器修改了现有的一个已存在文件,那该文 ...
- Vue项目开发前的准备工作,node的安装,vue-cli的安装
一.安装node 1- 点击这里进入node官网下载 2- 3- 下载完成是这样的 4- 双击打开进行安装,一路next,安装完成是这样 5- 打开cmd进入安装node的文件夹,输入node ...
- jacoco统计自动化代码覆盖率
jacoco统计自动化代码覆盖率 1. 简介 1.1. 什么是Jacoco Jacoco是一个开源的代码覆盖率工具,可以嵌入到Ant .Maven中,并提供了EclEmma Eclipse插件,也可以 ...
- 【Spring AOP】AOP实现原理(六)
原文链接:https://my.oschina.net/guangshan/blog/1797461
- 物联网架构成长之路(35)-利用Netty解析物联网自定义协议
一.前言 前面博客大部分介绍了基于EMQ中间件,通信协议使用的是MQTT,而传输的数据为纯文本数据,采用JSON格式.这种方式,大部分一看就知道是熟悉Web开发.软件开发的人喜欢用的方式.由于我也是做 ...