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性能调优的第一步,就是为任务分配合适的资源,在一定范围内,增加资源的分配与性能的提升是成正比的,实现了最优的资源配置后,在此基础上再考虑进行后面论述的性能调优策略. ...
随机推荐
- TypeScript 学习笔记(一)
TypeScript: 1.是 JavaScript 的一个超集,支持 ES6 标准 2.由微软开发的自由和开源的编程语言 3.设计目标是开发大型应用,它可编译成纯 JavaScript,编译出来的 ...
- bayaim_linux_install_oracle_11g - 20181102
-- 2018-11-2 15:12:12— baipingyang -- bayaim-- bayaim_linux_install_oracle_11g: -------------------- ...
- 更改docker默认网段
#本文档旨在说明创建docker时注意的事项:我们在局域网中使用Docker,最常遇到的一个困惑,就是有时候跨网段结果出现网络不通.原因是因为Docker默认生成的网关和我们的局域网网段有时候是冲突的 ...
- Java 种15种锁的介绍:公平锁,可重入锁,独享锁,互斥锁等等…
Java 中15种锁的介绍 1,在读很多并发文章中,会提及各种各样的锁,如公平锁,乐观锁,下面是对各种锁的总结归纳: 公平锁/非公平锁 可重入锁/不可重入锁 独享锁/共享锁 互斥锁/读写锁 乐观锁/悲 ...
- 201871010116-祁英红《面向对象程序设计(java)》第6-7周学习总结
项目 内容 <面向对象程序设计(java)> https://home.cnblogs.com/u/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.c ...
- session:
内容回顾: 多对多 class Pulisher(models.Model): name = models.CharField(max_length=32) class Book(models ...
- (day55)七、查询优化、MTV和MCV、choices、AJAX、序列化
目录 一.ORM查询优化 (一)only与defer (1)only (2)defer (二)select_related与prefatch_related (1)select_related (2) ...
- 【LGR-060】洛谷10月月赛 I div.1&div.2
Preface 一边打一边写作文打的像shit,T2失智严重特判错了233 Orz Div1 Rank2的foreverlastnig聚聚,顺便说一句显然Luogu的比赛质量比以往显著提高了啊 以下题 ...
- Unity 2018 Cookbook (Matt Smith 著)
1. Displaying Data with Core UI Elements (已看) 2. Responding to User Events for Interactive UIs (已看) ...
- webpack与vue环境搭建(转载)
原文:https://www.cnblogs.com/lgx5/p/10732016.html npm安装教程 一.使用之前,我们先来掌握3个东西是用来干什么的. npm: Nodejs下的包管理 ...