加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Apache Flink 零基础入门(一):基础概念解析.html 79.34 KB
一键复制 编辑 原始数据 按行查看 历史
杜德福 提交于 2021-04-22 17:41 . Site updated: 2021-04-22 17:41:18
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014
<!DOCTYPE html>
<html class="theme-next pisces use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic|Roboto Slab:300,300italic,400,400italic,700,700italic|Lobster Two:300,300italic,400,400italic,700,700italic|PT Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">
<link rel="stylesheet" href="/css/main.css?v=7.1.2">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.1.2">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.1.2">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon.ico?v=7.1.2">
<link rel="mask-icon" href="/images/logo.svg?v=7.1.2" color="#222">
<script id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Pisces',
version: '7.1.2',
sidebar: {"position":"left","display":"hide","offset":12,"onmobile":false,"dimmer":false},
back2top: true,
back2top_sidebar: false,
fancybox: false,
fastclick: false,
lazyload: false,
tabs: true,
motion: {"enable":true,"async":true,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<meta name="description" content="一、Apache Flink 的定义、架构及原理Apache Flink 是一个分布式大数据处理引擎,可对有限数据流和无限数据流进行有状态或无状态的计算,能够部署在各种集群环境,对各种规模大小的数据进行快速计算。">
<meta name="keywords" content="Flink">
<meta property="og:type" content="article">
<meta property="og:title" content="Apache Flink 零基础入门(一):基础概念解析">
<meta property="og:url" content="https://www.dudefu.tk/Apache Flink 零基础入门(一):基础概念解析.html">
<meta property="og:site_name" content="The Future">
<meta property="og:description" content="一、Apache Flink 的定义、架构及原理Apache Flink 是一个分布式大数据处理引擎,可对有限数据流和无限数据流进行有状态或无状态的计算,能够部署在各种集群环境,对各种规模大小的数据进行快速计算。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://yqfile.alicdn.com/f273ad0225d6b77b7e1e0151ed1964f12970cd25.jpeg">
<meta property="og:image" content="https://yqfile.alicdn.com/a16e76642ea95d0d71843f29ad5da71b2bbf7196.jpeg">
<meta property="og:image" content="https://yqfile.alicdn.com/c12e75424fe6c6058b895b614262f61c8d2f1baf.jpeg">
<meta property="og:image" content="https://yqfile.alicdn.com/334b4162776f26b2bcc85017f719687a05fbc40d.jpeg">
<meta property="og:image" content="https://yqfile.alicdn.com/3b266c8f0ffc568f8d85703679581f20a7f8362a.png">
<meta property="og:image" content="https://yqfile.alicdn.com/a62d3ce6051eb3a9249855653fc6d953a41a6549.png">
<meta property="og:image" content="https://yqfile.alicdn.com/f05cbff1caf39e9117ada3272484975fd28b4f2c.png">
<meta property="og:image" content="https://yqfile.alicdn.com/55a8733d374d7481323d03577386fa6d819e9032.png">
<meta property="og:image" content="https://yqfile.alicdn.com/e9e371cc65f42705137953ccb9dd870216c0ce29.png">
<meta property="og:image" content="https://yqfile.alicdn.com/0d497c688f0a640d518c71aa5cae341caa5aa638.png">
<meta property="og:image" content="https://yqfile.alicdn.com/7ece51897efca57d6a6c0095d06e5409648f77fe.png">
<meta property="og:image" content="https://yqfile.alicdn.com/108d0ac0398874ebafb0bc2318f3837e4fe789e1.png">
<meta property="og:image" content="https://yqfile.alicdn.com/f641b074b2334dd3fc0ef3a4db88fcaa511a29eb.png">
<meta property="og:image" content="https://yqfile.alicdn.com/23a231fa509ef45a572bf036c51ee58355742fa0.png">
<meta property="og:image" content="https://yqfile.alicdn.com/d064e1275ec4b0825a4311b85b4a8060a5a2e2bd.png">
<meta property="og:image" content="https://yqfile.alicdn.com/b350e67de1c612366deb7d573d64733214cbfbfe.png">
<meta property="og:image" content="https://yqfile.alicdn.com/942976c94cf14c11c3b7151d5f5205d8aa7359f0.png">
<meta property="og:image" content="https://yqfile.alicdn.com/39233b6ec882c80d2001c2fe1015dfd0c3ba2c5a.png">
<meta property="og:image" content="https://yqfile.alicdn.com/5c2e8be0390d3ff995b6c7b29233f3caf5bfcb54.png">
<meta property="og:image" content="https://yqfile.alicdn.com/2d0a56ba91f27c143db0cab6f5276e8d2d33834b.png">
<meta property="og:image" content="https://yqfile.alicdn.com/fb9c08413e705dc0e36d70beb44da682673b00a0.png">
<meta property="og:image" content="https://yqfile.alicdn.com/c51c6bad979faf3a0eb66f49dc025036c00f0f3a.png">
<meta property="og:image" content="https://yqfile.alicdn.com/460bbcae0f2a7366e24ca937c5c7b0cf7f738af9.png">
<meta property="og:image" content="https://yqfile.alicdn.com/b3a404ef8a36871d98ba6485ede1e799d95d6e3e.png">
<meta property="og:updated_time" content="2021-01-20T03:16:52.731Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Apache Flink 零基础入门(一):基础概念解析">
<meta name="twitter:description" content="一、Apache Flink 的定义、架构及原理Apache Flink 是一个分布式大数据处理引擎,可对有限数据流和无限数据流进行有状态或无状态的计算,能够部署在各种集群环境,对各种规模大小的数据进行快速计算。">
<meta name="twitter:image" content="https://yqfile.alicdn.com/f273ad0225d6b77b7e1e0151ed1964f12970cd25.jpeg">
<link rel="canonical" href="https://www.dudefu.tk/Apache Flink 零基础入门(一):基础概念解析">
<script id="page.configurations">
CONFIG.page = {
sidebar: "",
};
</script>
<title>Apache Flink 零基础入门(一):基础概念解析 | The Future</title>
<noscript>
<style>
.use-motion .motion-element,
.use-motion .brand,
.use-motion .menu-item,
.sidebar-inner,
.use-motion .post-block,
.use-motion .pagination,
.use-motion .comments,
.use-motion .post-header,
.use-motion .post-body,
.use-motion .collection-title { opacity: initial; }
.use-motion .logo,
.use-motion .site-title,
.use-motion .site-subtitle {
opacity: initial;
top: initial;
}
.use-motion .logo-line-before i { left: initial; }
.use-motion .logo-line-after i { right: initial; }
</style>
</noscript>
</head>
<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-CN">
<div class="container sidebar-position-left page-post-detail">
<div class="headband"></div>
<header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">The Future</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<h1 class="site-subtitle" itemprop="description">Stay hungry,stay foolish.</h1>
</div>
<div class="site-nav-toggle">
<button aria-label="切换导航栏">
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档<span class="badge">125</span></a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类<span class="badge">15</span></a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签<span class="badge">63</span></a>
</li>
<li class="menu-item menu-item-something">
<a href="/something" rel="section"><i class="menu-item-icon fa fa-fw fa-paper-plane"></i> <br>干货</a>
</li>
<li class="menu-item menu-item-about">
<a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>
</li>
<li class="menu-item menu-item-search">
<a href="javascript:;" class="popup-trigger">
<i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
</li>
</ul>
<div class="site-search">
<div class="popup search-popup local-search-popup">
<div class="local-search-header clearfix">
<span class="search-icon">
<i class="fa fa-search"></i>
</span>
<span class="popup-btn-close">
<i class="fa fa-times-circle"></i>
</span>
<div class="local-search-input-wrapper">
<input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
</div>
</div>
<div id="local-search-result"></div>
</div>
</div>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<div id="posts" class="posts-expand">
<div class="reading-progress-bar"></div>
<article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="https://www.dudefu.tk/Apache Flink 零基础入门(一):基础概念解析.html">
<span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
<meta itemprop="name" content="Daniel X">
<meta itemprop="description" content="專注于大数据技術,分享干货">
<meta itemprop="image" content="https://hexoblog-1254111960.cos.ap-guangzhou.myqcloud.com/HexoBlog-tou.jpg">
</span>
<span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
<meta itemprop="name" content="The Future">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">Apache Flink 零基础入门(一):基础概念解析
</h2>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-01-19 11:04:08" itemprop="dateCreated datePublished" datetime="2021-01-19T11:04:08+08:00">2021-01-19</time>
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-calendar-check-o"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2021-01-20 11:16:52" itemprop="dateModified" datetime="2021-01-20T11:16:52+08:00">2021-01-20</time>
</span>
<span class="post-category">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope="" itemtype="http://schema.org/Thing"><a href="/categories/大数据/" itemprop="url" rel="index"><span itemprop="name">大数据</span></a></span>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<span class="post-meta-item-text">评论数:</span>
<a href="/Apache Flink 零基础入门(一):基础概念解析.html#comments" itemprop="discussionUrl">
<span class="post-comments-count valine-comment-count" data-xid="/Apache Flink 零基础入门(一):基础概念解析.html" itemprop="commentCount"></span>
</a>
</span>
<span id="/Apache Flink 零基础入门(一):基础概念解析.html" class="leancloud_visitors" data-flag-title="Apache Flink 零基础入门(一):基础概念解析">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读次数:</span>
<span class="leancloud-visitors-count"></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="一、Apache-Flink-的定义、架构及原理"><a href="#一、Apache-Flink-的定义、架构及原理" class="headerlink" title="一、Apache Flink 的定义、架构及原理"></a>一、Apache Flink 的定义、架构及原理</h3><p>Apache Flink 是一个分布式大数据处理引擎,可对有限数据流和无限数据流进行有状态或无状态的计算,能够部署在各种集群环境,对各种规模大小的数据进行快速计算。</p>
<a id="more"></a>
<h4 id="1-Flink-Application"><a href="#1-Flink-Application" class="headerlink" title="1. Flink Application"></a><strong>1. Flink Application</strong></h4><p>了解 Flink 应用开发需要先理解 Flink 的 Streams、State、Time 等基础处理语义以及 Flink 兼顾灵活性和方便性的多层次 API。</p>
<ul>
<li><strong>Streams:</strong>流,分为有限数据流与无限数据流,unbounded stream 是有始无终的数据流,即无限数据流;而 bounded stream 是限定大小的有始有终的数据集合,即有限数据流,二者的区别在于无限数据流的数据会随时间的推演而持续增加,计算持续进行且不存在结束的状态,相对的有限数据流数据大小固定,计算最终会完成并处于结束的状态。</li>
<li><strong>State</strong>,状态是计算过程中的数据信息,在容错恢复和 Checkpoint 中有重要的作用,流计算在本质上是 Incremental Processing,因此需要不断查询保持状态;另外,为了确保 Exactly- once 语义,需要数据能够写入到状态中;而持久化存储,能够保证在整个分布式系统运行失败或者挂掉的情况下做到 Exactly- once,这是状态的另外一个价值。</li>
<li><strong>Time</strong>,分为 Event time、Ingestion time、Processing time,Flink 的无限数据流是一个持续的过程,时间是我们判断业务状态是否滞后,数据处理是否及时的重要依据。</li>
<li><strong>API</strong>,API 通常分为三层,由上而下可分为 SQL / Table API、DataStream API、ProcessFunction 三层,API 的表达能力及业务抽象能力都非常强大,但越接近 SQL 层,表达能力会逐步减弱,抽象能力会增强,反之,ProcessFunction 层 API 的表达能力非常强,可以进行多种灵活方便的操作,但抽象能力也相对越小。</li>
</ul>
<h4 id="2-Flink-Architecture"><a href="#2-Flink-Architecture" class="headerlink" title="2.Flink Architecture"></a><strong>2.Flink Architecture</strong></h4><p>在架构部分,主要分为以下四点:</p>
<p><img src="https://yqfile.alicdn.com/f273ad0225d6b77b7e1e0151ed1964f12970cd25.jpeg" alt="1_16_001_jpeg"></p>
<p><strong>第一</strong>, Flink 具备统一的框架处理有界和无界两种数据流的能力</p>
<p><strong>第二</strong>, 部署灵活,Flink 底层支持多种资源调度器,包括 Yarn、Kubernetes 等。Flink 自身带的 Standalone 的调度器,在部署上也十分灵活。</p>
<p><strong>第三</strong>, 极高的可伸缩性,可伸缩性对于分布式系统十分重要,阿里巴巴双11大屏采用 Flink 处理海量数据,使用过程中测得 Flink 峰值可达 17 亿/秒。</p>
<p><strong>第四</strong>, 极致的流式处理性能。Flink 相对于 Storm 最大的特点是将状态语义完全抽象到框架中,支持本地状态读取,避免了大量网络 IO,可以极大提升状态存取的性能。</p>
<h4 id="3-Flink-Operation"><a href="#3-Flink-Operation" class="headerlink" title="3.Flink Operation"></a><strong>3.Flink Operation</strong></h4><p>后面会有专门课程讲解,此处简单分享 Flink 关于运维及业务监控的内容:</p>
<ul>
<li>Flink 具备 7 X 24 小时高可用的 SOA(面向服务架构),原因是在实现上 Flink 提供了一致性的 Checkpoint。Checkpoint 是 Flink 实现容错机制的核心,它周期性的记录计算过程中 Operator 的状态,并生成快照持久化存储。当 Flink 作业发生故障崩溃时,可以有选择的从 Checkpoint 中恢复,保证了计算的一致性。</li>
<li>Flink 本身提供监控、运维等功能或接口,并有内置的 WebUI,对运行的作业提供 DAG 图以及各种 Metric 等,协助用户管理作业状态。</li>
</ul>
<h4 id="4-Flink-的应用场景"><a href="#4-Flink-的应用场景" class="headerlink" title="4.Flink 的应用场景"></a>4.Flink 的应用场景</h4><h5 id="4-1-Flink-的应用场景:Data-Pipeline"><a href="#4-1-Flink-的应用场景:Data-Pipeline" class="headerlink" title="4.1 Flink 的应用场景:Data Pipeline"></a>4.1 Flink 的应用场景:Data Pipeline</h5><p><img src="https://yqfile.alicdn.com/a16e76642ea95d0d71843f29ad5da71b2bbf7196.jpeg" alt="2_16_004_jpeg"></p>
<p>Data Pipeline 的核心场景类似于数据搬运并在搬运的过程中进行部分数据清洗或者处理,而整个业务架构图的左边是 Periodic ETL,它提供了流式 ETL 或者实时 ETL,能够订阅消息队列的消息并进行处理,清洗完成后实时写入到下游的 Database 或 File system 中。场景举例:</p>
<ul>
<li><strong>实时数仓</strong></li>
</ul>
<p>当下游要构建实时数仓时,上游则可能需要实时的 Stream ETL。这个过程会进行实时清洗或扩展数据,清洗完成后写入到下游的实时数仓的整个链路中,可保证数据查询的时效性,形成实时数据采集、实时数据处理以及下游的实时 Query。</p>
<ul>
<li><strong>搜索引擎推荐</strong></li>
</ul>
<p>搜索引擎这块以淘宝为例,当卖家上线新商品时,后台会实时产生消息流,该消息流经过 Flink 系统时会进行数据的处理、扩展。然后将处理及扩展后的数据生成实时索引,写入到搜索引擎中。这样当淘宝卖家上线新商品时,能在秒级或者分钟级实现搜索引擎的搜索。</p>
<h5 id="4-2-Flink-应用场景:Data-Analytics"><a href="#4-2-Flink-应用场景:Data-Analytics" class="headerlink" title="4.2 Flink 应用场景:Data Analytics"></a><strong>4.2 Flink</strong> <strong>应用场景:Data Analytics</strong></h5><p><img src="https://yqfile.alicdn.com/c12e75424fe6c6058b895b614262f61c8d2f1baf.jpeg" alt="3_005_jpeg"></p>
<p>Data Analytics,如图,左边是 Batch Analytics,右边是 Streaming Analytics。Batch Analysis 就是传统意义上使用类似于 Map Reduce、Hive、Spark Batch 等,对作业进行分析、处理、生成离线报表,Streaming Analytics 使用流式分析引擎如 Storm,Flink 实时处理分析数据,应用较多的场景如实时大屏、实时报表。</p>
<h5 id="4-3-Flink-应用场景:Data-Driven"><a href="#4-3-Flink-应用场景:Data-Driven" class="headerlink" title="4.3 Flink 应用场景:Data Driven"></a><strong>4.3 Flink</strong> <strong>应用场景:Data Driven</strong></h5><p><img src="https://yqfile.alicdn.com/334b4162776f26b2bcc85017f719687a05fbc40d.jpeg" alt="4_006_jpeg"></p>
<p>从某种程度上来说,所有的实时的数据处理或者是流式数据处理都是属于 Data Driven,流计算本质上是 Data Driven 计算。应用较多的如风控系统,当风控系统需要处理各种各样复杂的规则时,Data Driven 就会把处理的规则和逻辑写入到Datastream 的 API 或者是 ProcessFunction 的 API 中,然后将逻辑抽象到整个 Flink 引擎中,当外面的数据流或者是事件进入就会触发相应的规则,这就是 Data Driven 的原理。在触发某些规则后,Data Driven 会进行处理或者是进行预警,这些预警会发到下游产生业务通知,这是 Data Driven 的应用场景,Data Driven 在应用上更多应用于复杂事件的处理。</p>
<h3 id="二、「有状态的流式处理」概念解析"><a href="#二、「有状态的流式处理」概念解析" class="headerlink" title="二、「有状态的流式处理」概念解析"></a><strong>二、「有状态的流式处理」概念解析</strong></h3><h4 id="1-传统批处理"><a href="#1-传统批处理" class="headerlink" title="1.传统批处理"></a>1.传统批处理</h4><p><img src="https://yqfile.alicdn.com/3b266c8f0ffc568f8d85703679581f20a7f8362a.png" alt="5_04"></p>
<p>传统批处理方法是持续收取数据,以时间作为划分多个批次的依据,再周期性地执行批次运算。但假设需要计算每小时出现事件转换的次数,如果事件转换跨越了所定义的时间划分,传统批处理会将中介运算结果带到下一个批次进行计算;除此之外,当出现接收到的事件顺序颠倒情况下,传统批处理仍会将中介状态带到下一批次的运算结果中,这种处理方式也不尽如人意。</p>
<h4 id="2-理想方法"><a href="#2-理想方法" class="headerlink" title="2.理想方法"></a>2.理想方法</h4><p><img src="https://yqfile.alicdn.com/a62d3ce6051eb3a9249855653fc6d953a41a6549.png" alt="6_07"></p>
<p><strong>第一点</strong>,要有理想方法,这个理想方法是引擎必须要有能力可以累积状态和维护状态,累积状态代表着过去历史中接收过的所有事件,会影响到输出。</p>
<p><strong>第二点</strong>,时间,时间意味着引擎对于数据完整性有机制可以操控,当所有数据都完全接受到后,输出计算结果。</p>
<p><strong>第三点</strong>,理想方法模型需要实时产生结果,但更重要的是采用新的持续性数据处理模型来处理实时数据,这样才最符合 continuous data 的特性。</p>
<h4 id="3-流式处理"><a href="#3-流式处理" class="headerlink" title="3.流式处理"></a>3.流式处理</h4><p><img src="https://yqfile.alicdn.com/f05cbff1caf39e9117ada3272484975fd28b4f2c.png" alt="7_08"></p>
<p>流式处理简单来讲即有一个无穷无尽的数据源在持续收取数据,以代码作为数据处理的基础逻辑,数据源的数据经过代码处理后产生出结果,然后输出,这就是流式处理的基本原理。</p>
<h4 id="4-分布式流式处理"><a href="#4-分布式流式处理" class="headerlink" title="4.分布式流式处理"></a>4.分布式流式处理</h4><p><img src="https://yqfile.alicdn.com/55a8733d374d7481323d03577386fa6d819e9032.png" alt="8_09"></p>
<p>假设 Input Streams 有很多个使用者,每个使用者都有自己的 ID,如果计算每个使用者出现的次数,我们需要让同一个使用者的出现事件流到同一运算代码,这跟其他批次需要做 group by 是同样的概念,所以跟 Stream 一样需要做分区,设定相应的 key,然后让同样的 key 流到同一个 computation instance 做同样的运算。</p>
<h4 id="5-有状态分布式流式处理"><a href="#5-有状态分布式流式处理" class="headerlink" title="5.有状态分布式流式处理"></a>5.有状态分布式流式处理</h4><p><img src="https://yqfile.alicdn.com/e9e371cc65f42705137953ccb9dd870216c0ce29.png" alt="9_10"></p>
<p>如图,上述代码中定义了变数 X,X 在数据处理过程中会进行读和写,在最后输出结果时,可以依据变数 X 决定输出的内容,即状态 X 会影响最终的输出结果。这个过程中,第一个重点是先进行了状态 co-partitioned key by,同样的 key 都会流到 computation instance,与使用者出现次数的原理相同,次数即所谓的状态,这个状态一定会跟同一个 key 的事件累积在同一个 computation instance。</p>
<p><img src="https://yqfile.alicdn.com/0d497c688f0a640d518c71aa5cae341caa5aa638.png" alt="10_11"></p>
<p>相当于根据输入流的 key 重新分区的 状态,当分区进入 stream 之后,这个 stream 会累积起来的状态也变成 copartiton 了。第二个重点是 embeded local state backend。有状态分散式流式处理的引擎,状态可能会累积到非常大,当 key 非常多时,状态可能就会超出单一节点的 memory 的负荷量,这时候状态必须有状态后端去维护它;在这个状态后端在正常状况下,用 in-memory 维护即可。</p>
<h3 id="三、Apache-Flink-的优势"><a href="#三、Apache-Flink-的优势" class="headerlink" title="三、Apache Flink 的优势"></a><strong>三、Apache Flink 的优势</strong></h3><h4 id="1-状态容错"><a href="#1-状态容错" class="headerlink" title="1.状态容错"></a>1.状态容错</h4><p>当我们考虑状态容错时难免会想到精确一次的状态容错,应用在运算时累积的状态,每笔输入的事件反映到状态,更改状态都是精确一次,如果修改超过一次的话也意味着数据引擎产生的结果是不可靠的。</p>
<ul>
<li>如何确保状态拥有精确一次(Exactly-once guarantee)的容错保证?</li>
<li>如何在分散式场景下替多个拥有本地状态的运算子产生一个全域一致的快照(Global consistent snapshot)?</li>
<li>更重要的是,如何在不中断运算的前提下产生快照?</li>
</ul>
<h5 id="1-1-简单场景的精确一次容错方法"><a href="#1-1-简单场景的精确一次容错方法" class="headerlink" title="1.1 简单场景的精确一次容错方法"></a>1.1 简单场景的精确一次容错方法</h5><p>还是以使用者出现次数来看,如果某个使用者出现的次数计算不准确,不是精确一次,那么产生的结果是无法作为参考的。在考虑精确的容错保证前,我们先考虑最简单的使用场景,如无限流的数据进入,后面单一的 Process 进行运算,每处理完一笔计算即会累积一次状态,这种情况下如果要确保 Process 产生精确一次的状态容错,每处理完一笔数据,更改完状态后进行一次快照,快照包含在队列中并与相应的状态进行对比,完成一致的快照,就能确保精确一次。</p>
<h5 id="1-2-分布式状态容错"><a href="#1-2-分布式状态容错" class="headerlink" title="1.2 分布式状态容错"></a>1.2 分布式状态容错</h5><p>Flink 作为分布式的处理引擎,在分布式的场景下,进行多个本地状态的运算,只产生一个全域一致的快照,如需要在不中断运算值的前提下产生全域一致的快照,就涉及到分散式状态容错。</p>
<ul>
<li><strong>Global consistent snapshot</strong></li>
</ul>
<p><img src="https://yqfile.alicdn.com/7ece51897efca57d6a6c0095d06e5409648f77fe.png" alt="11_21"></p>
<p>关于 Global consistent snapshot,当 Operator 在分布式的环境中,在各个节点做运算,首先产生 Global consistent snapshot 的方式就是处理每一笔数据的快照点是连续的,这笔运算流过所有的运算值,更改完所有的运算值后,能够看到每一个运算值的状态与该笔运算的位置,即可称为 consistent snapshot,当然,Global consistent snapshot 也是简易场景的延伸。</p>
<ul>
<li><strong>容错恢复</strong></li>
</ul>
<p><img src="https://yqfile.alicdn.com/108d0ac0398874ebafb0bc2318f3837e4fe789e1.png" alt="12_22"></p>
<p>首先了解一下 Checkpoint,上面提到连续性快照每个 Operator 运算值本地的状态后端都要维护状态,也就是每次将产生检查点时会将它们传入共享的 DFS 中。当任何一个 Process 挂掉后,可以直接从三个完整的 Checkpoint 将所有的运算值的状态恢复,重新设定到相应位置。Checkpoint 的存在使整个 Process 能够实现分散式环境中的 Exactly-once。</p>
<h5 id="1-3-分散式快照(Distributed-Snapshots)方法"><a href="#1-3-分散式快照(Distributed-Snapshots)方法" class="headerlink" title="1.3 分散式快照(Distributed Snapshots)方法"></a><strong>1.3</strong> <strong>分散式快照(Distributed Snapshots)方法</strong></h5><p><img src="https://yqfile.alicdn.com/f641b074b2334dd3fc0ef3a4db88fcaa511a29eb.png" alt="13_23"></p>
<p>关于 Flink 如何在不中断运算的状况下持续产生 Global consistent snapshot,其方式是基于用 simple lamport 演算法机制下延伸的。已知的一个点 Checkpoint barrier, Flink 在某个 Datastream 中会一直安插 Checkpoint barrier,Checkpoint barrier 也会 N — 1等等,Checkpoint barrier N 代表着所有在这个范围里面的数据都是Checkpoint barrier N。</p>
<p><img src="https://yqfile.alicdn.com/23a231fa509ef45a572bf036c51ee58355742fa0.png" alt="14_25"></p>
<p>举例:假设现在需要产生 Checkpoint barrier N,但实际上在 Flink 中是由 job manager 触发 Checkpoint,Checkpoint 被触发后开始从数据源产生 Checkpoint barrier。当 job 开始做 Checkpoint barrier N 的时候,可以理解为 Checkpoint barrier N 需要逐步填充左下角的表格。</p>
<p><img src="https://yqfile.alicdn.com/d064e1275ec4b0825a4311b85b4a8060a5a2e2bd.png" alt="15_26"></p>
<p>如图,当部分事件标为红色,Checkpoint barrier N 也是红色时,代表着这些数据或事件都由 Checkpoint barrier N 负责。Checkpoint barrier N 后面白色部分的数据或事件则不属于 Checkpoint barrier N。</p>
<p>在以上的基础上,当数据源收到 Checkpoint barrier N 之后会先将自己的状态保存,以读取 Kafka 资料为例,数据源的状态就是目前它在 Kafka 分区的位置,这个状态也会写入到上面提到的表格中。下游的 Operator 1 会开始运算属于 Checkpoint barrier N 的数据,当 Checkpoint barrier N 跟着这些数据流动到 Operator 1 之后,Operator 1 也将属于 Checkpoint barrier N 的所有数据都反映在状态中,当收到 Checkpoint barrier N 时也会直接对 Checkpoint 去做快照。</p>
<p><img src="https://yqfile.alicdn.com/b350e67de1c612366deb7d573d64733214cbfbfe.png" alt="16_27"></p>
<p>当快照完成后继续往下游走,Operator 2 也会接收到所有数据,然后搜索 Checkpoint barrier N 的数据并直接反映到状态,当状态收到 Checkpoint barrier N 之后也会直接写入到 Checkpoint N 中。以上过程到此可以看到 Checkpoint barrier N 已经完成了一个完整的表格,这个表格叫做 Distributed Snapshots,即分布式快照。分布式快照可以用来做状态容错,任何一个节点挂掉的时候可以在之前的 Checkpoint 中将其恢复。继续以上 Process,当多个 Checkpoint 同时进行,Checkpoint barrier N 已经流到 job manager 2,Flink job manager 可以触发其他的 Checkpoint,比如 Checkpoint N + 1,Checkpoint N + 2 等等也同步进行,利用这种机制,可以在不阻挡运算的状况下持续地产生 Checkpoint。</p>
<h4 id="2-状态维护"><a href="#2-状态维护" class="headerlink" title="2.状态维护"></a>2.状态维护</h4><p>状态维护即用一段代码在本地维护状态值,当状态值非常大时需要本地的状态后端来支持。</p>
<p><img src="https://yqfile.alicdn.com/942976c94cf14c11c3b7151d5f5205d8aa7359f0.png" alt="17_34"></p>
<p>如图,在 Flink 程序中,可以采用 getRuntimeContext().getState(desc); 这组 API 去注册状态。Flink 有多种状态后端,采用 API 注册状态后,读取状态时都是通过状态后端来读取的。Flink 有两种不同的状态值,也有两种不同的状态后端:</p>
<p><img src="https://yqfile.alicdn.com/39233b6ec882c80d2001c2fe1015dfd0c3ba2c5a.png" alt="18_37"></p>
<ul>
<li><strong>JVM Heap状态后端</strong>,适合数量较小的状态,当状态量不大时就可以采用 JVM Heap 的状态后端。JVM Heap 状态后端会在每一次运算值需要读取状态时,用 Java object read / writes 进行读或写,不会产生较大代价,但当 Checkpoint 需要将每一个运算值的本地状态放入 Distributed Snapshots 的时候,就需要进行序列化了。</li>
</ul>
<p><img src="https://yqfile.alicdn.com/5c2e8be0390d3ff995b6c7b29233f3caf5bfcb54.png" alt="19_40"></p>
<ul>
<li><strong>RocksDB 状态后端</strong>,它是一种 out of core 的状态后端。在 Runtime 的本地状态后端让使用者去读取状态的时候会经过磁盘,相当于将状态维护在磁盘里,与之对应的代价可能就是每次读取状态时,都需要经过序列化和反序列化的过程。当需要进行快照时只将应用序列化即可,序列化后的数据直接传输到中央的共享 DFS 中。</li>
</ul>
<p>Flink 目前支持以上两种状态后端,一种是纯 memory 的状态后端,另一种是有资源磁盘的状态后端,在维护状态时可以根据状态的数量选择相应的状态后端。</p>
<h4 id="3-Event-Time"><a href="#3-Event-Time" class="headerlink" title="3.Event - Time"></a>3.Event - Time</h4><p><strong>3.1</strong> <strong>不同时间种类</strong></p>
<p>在 Flink 及其他进阶的流式处理引擎出现之前,大数据处理引擎一直只支持 Processing-time 的处理。假设定义一个运算 windows 的窗口,windows 运算设定每小时进行结算。以 Processing-time 进行运算时可以发现数据引擎将 3 点至 4 点间收到的数据做结算。实际上在做报表或者分析结果时是想了解真实世界中 3 点至 4 点之间实际产生数据的输出结果,了解实际数据的输出结果就必须采用 Event – Time 了。</p>
<p><img src="https://yqfile.alicdn.com/2d0a56ba91f27c143db0cab6f5276e8d2d33834b.png" alt="20_42"></p>
<p>如图,Event - Time 相当于事件,它在数据最源头产生时带有时间戳,后面都需要用时间戳来进行运算。用图来表示,最开始的队列收到数据,每小时对数据划分一个批次,这就是 Event - Time Process 在做的事情。</p>
<p><strong>3.2</strong> <strong>Event - Time 处理</strong></p>
<p><img src="https://yqfile.alicdn.com/fb9c08413e705dc0e36d70beb44da682673b00a0.png" alt="21_44"></p>
<p>Event - Time 是用事件真实产生的时间戳去做 Re-bucketing,把对应时间 3 点到 4 点的数据放在 3 点到 4 点的 Bucket,然后 Bucket 产生结果。所以 Event - Time 跟 Processing - time 的概念是这样对比的存在。</p>
<p><img src="https://yqfile.alicdn.com/c51c6bad979faf3a0eb66f49dc025036c00f0f3a.png" alt="22_45"></p>
<p>Event - Time 的重要性在于记录引擎输出运算结果的时间。简单来说,流式引擎连续 24 小时在运行、搜集资料,假设 Pipeline 里有一个 windows Operator 正在做运算,每小时能产生结果,何时输出 windows 的运算值,这个时间点就是 Event - Time 处理的精髓,用来表示该收的数据已经收到。</p>
<p><strong>3.3</strong> <strong>Watermarks</strong></p>
<p><img src="https://yqfile.alicdn.com/460bbcae0f2a7366e24ca937c5c7b0cf7f738af9.png" alt="23_46"></p>
<p>Flink 实际上是用 watermarks 来实现 Event - Time 的功能。Watermarks 在 Flink 中也属于特殊事件,其精髓在于当某个运算值收到带有时间戳“ T ”的 watermarks 时就意味着它不会接收到新的数据了。使用 watermarks 的好处在于可以准确预估收到数据的截止时间。举例,假设预期收到数据时间与输出结果时间的时间差延迟 5 分钟,那么 Flink 中所有的 windows Operator 搜索 3 点至 4 点的数据,但因为存在延迟需要再多等5分钟直至收集完 4:05 分的数据,此时方能判定 4 点钟的资料收集完成了,然后才会产出 3 点至 4 点的数据结果。这个时间段的结果对应的就是 watermarks 的部分。</p>
<h4 id="4-状态保存与迁移"><a href="#4-状态保存与迁移" class="headerlink" title="4.状态保存与迁移"></a>4.状态保存与迁移</h4><p>流式处理应用无时无刻不在运行,运维上有几个重要考量:</p>
<ul>
<li>更改应用逻辑/修 bug 等,如何将前一执行的状态迁移到新的执行?</li>
<li>如何重新定义运行的平行化程度?</li>
<li>如何升级运算丛集的版本号?</li>
</ul>
<p>Checkpoint 完美符合以上需求,不过 Flink 中还有另外一个名词保存点(Savepoint),当手动产生一个 Checkpoint 的时候,就叫做一个 Savepoint。Savepoint 跟 Checkpoint 的差别在于检查点是 Flink 对于一个有状态应用在运行中利用分布式快照持续周期性的产生 Checkpoint,而 Savepoint 则是手动产生的 Checkpoint,Savepoint 记录着流式应用中所有运算元的状态。</p>
<p><img src="https://yqfile.alicdn.com/b3a404ef8a36871d98ba6485ede1e799d95d6e3e.png" alt="24_49"></p>
<p>如图,Savepoint A 和 Savepoint B,无论是变更底层代码逻辑、修 bug 或是升级 Flink 版本,重新定义应用、计算的平行化程度等,最先需要做的事情就是产生 Savepoint。</p>
<p>Savepoint 产生的原理是在 Checkpoint barrier 流动到所有的 Pipeline 中手动插入从而产生分布式快照,这些分布式快照点即 Savepoint。Savepoint 可以放在任何位置保存,当完成变更时,可以直接从 Savepoint 恢复、执行。</p>
<p>从 Savepoint 的恢复执行需要注意,在变更应用的过程中时间在持续,如 Kafka 在持续收集资料,当从 Savepoint 恢复时,Savepoint 保存着 Checkpoint 产生的时间以及 Kafka 的相应位置,因此它需要恢复到最新的数据。无论是任何运算,Event - Time 都可以确保产生的结果完全一致。</p>
<p>假设恢复后的重新运算用 Process Event - Time,将 windows 窗口设为 1 小时,重新运算能够在 10 分钟内将所有的运算结果都包含到单一的 windows 中。而如果使用 Event – Time,则类似于做 Bucketing。在 Bucketing 的状况下,无论重新运算的数量多大,最终重新运算的时间以及 windows 产生的结果都一定能保证完全一致。</p>
<h3 id="四、总结"><a href="#四、总结" class="headerlink" title="四、总结"></a>四、总结</h3><p>本文首先从 Apache Flink 的定义、架构、基本原理入手,对大数据流计算相关的基本概念进行辨析,在此基础上简单回顾了大数据处理方式的历史演进以及有状态的流式数据处理的原理,最后从目前有状态的流式处理面临的挑战分析 Apache Flink 作为业界公认为最好的流计算引擎之一所具备的天然优势。希望有助于大家厘清大数据流式处理引擎涉及的基本概念,能够更加得心应手的使用 Flink。</p>
<p><span class="exturl" data-url="aHR0cHM6Ly9kZXZlbG9wZXIuYWxpeXVuLmNvbS9hcnRpY2xlLzcxMjcwMT9zcG09YTJjNmguMTI4NzM2MzkuMC4wLjUyMDFmNTUzR3p0aE9hJmFtcDtncm91cENvZGU9c2M=" title="https://developer.aliyun.com/article/712701?spm=a2c6h.12873639.0.0.5201f553GzthOa&amp;groupCode=sc">原文地址<i class="fa fa-external-link"></i></span></p>
</div>
<footer class="post-footer">
<div class="post-tags">
<a href="/tags/Flink/" rel="tag"><i class="fa fa-tag"></i> Flink</a>
</div>
<div class="post-widgets">
<div class="social_share">
<div>
<script src="//cdn.jsdelivr.net/npm/ilyabirman-likely@2/release/likely.js"></script>
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/ilyabirman-likely@2/release/likely.css">
<div class="likely likely-light">
<div class="twitter">Tweet</div>
<div class="facebook">Share</div>
<div class="linkedin">Link</div>
<div class="gplus">Plus</div>
<div class="vkontakte">Share</div>
<div class="odnoklassniki">Class</div>
<div class="telegram">Send</div>
<div class="whatsapp">Send</div>
<div class="pinterest">Pin</div>
</div>
</div>
<div>
<div class="bdsharebuttonbox">
<a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
<a href="#" class="bds_douban" data-cmd="douban" title="分享到豆瓣网"></a>
<a href="#" class="bds_sqq" data-cmd="sqq" title="分享到QQ好友"></a>
<a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"></a>
<a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a>
<a href="#" class="bds_tieba" data-cmd="tieba" title="分享到百度贴吧"></a>
<a href="#" class="bds_twi" data-cmd="twi" title="分享到Twitter"></a>
<a href="#" class="bds_fbook" data-cmd="fbook" title="分享到Facebook"></a>
<a href="#" class="bds_more" data-cmd="more"></a>
<a class="bds_count" data-cmd="count"></a>
</div>
<script>
window._bd_share_config = {
"common": {
"bdText": "",
"bdMini": "2",
"bdMiniList": false,
"bdPic": ""
},
"share": {
"bdSize": "16",
"bdStyle": "0"
},
"image": {
"viewList": ["tsina", "douban", "sqq", "qzone", "weixin", "twi", "fbook"],
"viewText": "分享到:",
"viewSize": "16"
}
}
</script>
<script>
with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='//bdimg.share.baidu.com/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
</script>
</div>
</div>
</div>
<div class="post-nav">
<div class="post-nav-next post-nav-item">
<a href="/图数据库洞察数据间的关联价值.html" rel="next" title="图数据库洞察数据间的关联价值">
<i class="fa fa-chevron-left"></i> 图数据库洞察数据间的关联价值
</a>
</div>
<span class="post-nav-divider"></span>
<div class="post-nav-prev post-nav-item">
<a href="/Apache Flink 零基础入门(二):开发环境搭建和应用的配置、部署及运行.html" rel="prev" title="Apache Flink 零基础入门(二):开发环境搭建和应用的配置、部署及运行">
Apache Flink 零基础入门(二):开发环境搭建和应用的配置、部署及运行 <i class="fa fa-chevron-right"></i>
</a>
</div>
</div>
</footer>
</div>
</article>
</div>
</div>
<div class="comments" id="comments">
<div id="lv-container" data-id="city" data-uid="MTAyMC8yOTk3My82NTM4"></div>
</div>
</div>
<div class="sidebar-toggle">
<div class="sidebar-toggle-line-wrap">
<span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
<span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
<span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
</div>
</div>
<aside id="sidebar" class="sidebar">
<div class="sidebar-inner">
<ul class="sidebar-nav motion-element">
<li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
文章目录
</li>
<li class="sidebar-nav-overview" data-target="site-overview-wrap">
站点概览
</li>
</ul>
<div class="site-overview-wrap sidebar-panel">
<div class="site-overview">
<div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
<img class="site-author-image" itemprop="image" src="https://hexoblog-1254111960.cos.ap-guangzhou.myqcloud.com/HexoBlog-tou.jpg" alt="Daniel X">
<p class="site-author-name" itemprop="name">Daniel X</p>
<div class="site-description motion-element" itemprop="description">專注于大数据技術,分享干货</div>
</div>
<nav class="site-state motion-element">
<div class="site-state-item site-state-posts">
<a href="/archives/">
<span class="site-state-item-count">125</span>
<span class="site-state-item-name">日志</span>
</a>
</div>
<div class="site-state-item site-state-categories">
<a href="/categories">
<span class="site-state-item-count">15</span>
<span class="site-state-item-name">分类</span>
</a>
</div>
<div class="site-state-item site-state-tags">
<a href="/tags">
<span class="site-state-item-count">63</span>
<span class="site-state-item-name">标签</span>
</a>
</div>
</nav>
<div class="links-of-author motion-element">
<span class="links-of-author-item">
<span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2R1ZGVmdQ==" title="GitHub &rarr; https://github.com/dudefu"><i class="fa fa-fw fa-github"></i>GitHub</span>
</span>
<span class="links-of-author-item">
<span class="exturl" data-url="bWFpbHRvOmR1ZGVmdUBmb3htYWlsLmNvbT9zdWJqZWN0PUhlbGxvJTIwYWdhaW4=" title="E-mail &rarr; mailto:dudefu@foxmail.com?subject=Hello%20again"><i class="fa fa-fw fa-envelope"></i>E-mail</span>
</span>
<span class="links-of-author-item">
<span class="exturl" data-url="aHR0cHM6Ly93ZWliby5jb20vZHVkZWZ1" title="Weibo &rarr; https://weibo.com/dudefu"><i class="fa fa-fw fa-weibo"></i>Weibo</span>
</span>
<span class="links-of-author-item">
<span class="exturl" data-url="aHR0cHM6Ly93cGEucXEuY29tL21zZ3JkP3Y9MyZ1aW49MTU3NzU3MTk1OSZzaXRlPWR1ZGVmdS5pbmZvJm1lbnU9eWVz" title="QQ &rarr; https://wpa.qq.com/msgrd?v=3&uin=1577571959&site=dudefu.info&menu=yes"><i class="fa fa-fw fa-qq"></i>QQ</span>
</span>
</div>
</div>
</div>
<!--noindex-->
<div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
<div class="post-toc">
<div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#一、Apache-Flink-的定义、架构及原理"><span class="nav-number">1.</span> <span class="nav-text">一、Apache Flink 的定义、架构及原理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-Flink-Application"><span class="nav-number">1.1.</span> <span class="nav-text">1. Flink Application</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-Flink-Architecture"><span class="nav-number">1.2.</span> <span class="nav-text">2.Flink Architecture</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-Flink-Operation"><span class="nav-number">1.3.</span> <span class="nav-text">3.Flink Operation</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-Flink-的应用场景"><span class="nav-number">1.4.</span> <span class="nav-text">4.Flink 的应用场景</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#4-1-Flink-的应用场景:Data-Pipeline"><span class="nav-number">1.4.1.</span> <span class="nav-text">4.1 Flink 的应用场景:Data Pipeline</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#4-2-Flink-应用场景:Data-Analytics"><span class="nav-number">1.4.2.</span> <span class="nav-text">4.2 Flink 应用场景:Data Analytics</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#4-3-Flink-应用场景:Data-Driven"><span class="nav-number">1.4.3.</span> <span class="nav-text">4.3 Flink 应用场景:Data Driven</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二、「有状态的流式处理」概念解析"><span class="nav-number">2.</span> <span class="nav-text">二、「有状态的流式处理」概念解析</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-传统批处理"><span class="nav-number">2.1.</span> <span class="nav-text">1.传统批处理</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-理想方法"><span class="nav-number">2.2.</span> <span class="nav-text">2.理想方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-流式处理"><span class="nav-number">2.3.</span> <span class="nav-text">3.流式处理</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-分布式流式处理"><span class="nav-number">2.4.</span> <span class="nav-text">4.分布式流式处理</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-有状态分布式流式处理"><span class="nav-number">2.5.</span> <span class="nav-text">5.有状态分布式流式处理</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#三、Apache-Flink-的优势"><span class="nav-number">3.</span> <span class="nav-text">三、Apache Flink 的优势</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-状态容错"><span class="nav-number">3.1.</span> <span class="nav-text">1.状态容错</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1-1-简单场景的精确一次容错方法"><span class="nav-number">3.1.1.</span> <span class="nav-text">1.1 简单场景的精确一次容错方法</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#1-2-分布式状态容错"><span class="nav-number">3.1.2.</span> <span class="nav-text">1.2 分布式状态容错</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#1-3-分散式快照(Distributed-Snapshots)方法"><span class="nav-number">3.1.3.</span> <span class="nav-text">1.3 分散式快照(Distributed Snapshots)方法</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-状态维护"><span class="nav-number">3.2.</span> <span class="nav-text">2.状态维护</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-Event-Time"><span class="nav-number">3.3.</span> <span class="nav-text">3.Event - Time</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-状态保存与迁移"><span class="nav-number">3.4.</span> <span class="nav-text">4.状态保存与迁移</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#四、总结"><span class="nav-number">4.</span> <span class="nav-text">四、总结</span></a></li></ol></div>
</div>
</div>
<!--/noindex-->
</div>
</aside>
</div>
</main>
<footer id="footer" class="footer">
<div class="footer-inner">
<div class="copyright"> <span class="exturl" data-url="aHR0cDovL3d3dy5iZWlhbi5taWl0Lmdvdi5jbg==">粤ICP备18110871号 </span>&copy; 2017 – <span itemprop="copyrightYear">2021</span>
<span class="with-love" id="animate">
<i class="fa fa-spinner"></i>
</span>
<span class="author" itemprop="copyrightHolder">dudefu</span>
</div>
<!--
<div class="powered-by">由 <span class="exturl theme-link" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> 强力驱动 v3.8.0</div>
<span class="post-meta-divider">|</span>
<div class="theme-info">主题 – <span class="exturl theme-link" data-url="aHR0cHM6Ly90aGVtZS1uZXh0Lm9yZw==">NexT.Pisces</span> v7.1.2</div>
-->
</div>
</footer>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
<span id="scrollpercent"><span>0</span>%</span>
</div>
</div>
<script>
if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
window.Promise = null;
}
</script>
<script color="26,26,26" opacity="0.5" zindex="-1" count="99" src="//cdn.jsdelivr.net/gh/theme-next/theme-next-canvas-nest@1/canvas-nest.min.js"></script>
<script id="ribbon" size="300" alpha="0.6" zindex="-1" src="/lib/canvas-ribbon/canvas-ribbon.js"></script>
<script src="/lib/jquery/index.js?v=3.4.1"></script>
<script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
<script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="/lib/reading_progress/reading_progress.js"></script>
<script src="/js/utils.js?v=7.1.2"></script>
<script src="/js/motion.js?v=7.1.2"></script>
<script src="/js/affix.js?v=7.1.2"></script>
<script src="/js/schemes/pisces.js?v=7.1.2"></script>
<script src="/js/scrollspy.js?v=7.1.2"></script>
<script src="/js/post-details.js?v=7.1.2"></script>
<script src="/js/next-boot.js?v=7.1.2"></script>
<script src="/js/js.cookie.js?v=7.1.2"></script>
<script src="/js/scroll-cookie.js?v=7.1.2"></script>
<script src="/js/exturl.js?v=7.1.2"></script>
<script src="//cdn1.lncld.net/static/js/3.11.1/av-min.js"></script>
<script src="//unpkg.com/valine/dist/Valine.min.js"></script>
<script>
var GUEST = ['nick', 'mail', 'link'];
var guest = 'nick,mail,link';
guest = guest.split(',').filter(function(item) {
return GUEST.indexOf(item) > -1;
});
new Valine({
el: '#comments',
verify: true,
notify: true,
appId: '1N5rpk874DGudJw2wCL9J011-gzGzoHsz',
appKey: '9Y83e6suJgx567wtxhKy45IN',
placeholder: 'Just go go',
avatar: 'mm',
meta: guest,
pageSize: '10' || 10,
visitor: true,
lang: 'zk-cn' || 'zh-cn'
});
</script>
<script>
window.livereOptions = {
refer: 'Apache Flink 零基础入门(一):基础概念解析.html'
};
(function(d, s) {
var j, e = d.getElementsByTagName(s)[0];
if (typeof LivereTower === 'function') { return; }
j = d.createElement(s);
j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
j.async = true;
e.parentNode.insertBefore(j, e);
})(document, 'script');
</script>
<script>
// Popup Window;
var isfetched = false;
var isXml = true;
// Search DB path;
var search_path = "search.xml";
if (search_path.length === 0) {
search_path = "search.xml";
} else if (/json$/i.test(search_path)) {
isXml = false;
}
var path = "/" + search_path;
// monitor main search box;
var onPopupClose = function (e) {
$('.popup').hide();
$('#local-search-input').val('');
$('.search-result-list').remove();
$('#no-result').remove();
$(".local-search-pop-overlay").remove();
$('body').css('overflow', '');
}
function proceedsearch() {
$("body")
.append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
.css('overflow', 'hidden');
$('.search-popup-overlay').click(onPopupClose);
$('.popup').toggle();
var $localSearchInput = $('#local-search-input');
$localSearchInput.attr("autocapitalize", "none");
$localSearchInput.attr("autocorrect", "off");
$localSearchInput.focus();
}
// search function;
var searchFunc = function(path, search_id, content_id) {
'use strict';
// start loading animation
$("body")
.append('<div class="search-popup-overlay local-search-pop-overlay">' +
'<div id="search-loading-icon">' +
'<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
'</div>' +
'</div>')
.css('overflow', 'hidden');
$("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');
$.ajax({
url: path,
dataType: isXml ? "xml" : "json",
async: true,
success: function(res) {
// get the contents from search data
isfetched = true;
$('.popup').detach().appendTo('.header-inner');
var datas = isXml ? $("entry", res).map(function() {
return {
title: $("title", this).text(),
content: $("content",this).text(),
url: $("url" , this).text()
};
}).get() : res;
var input = document.getElementById(search_id);
var resultContent = document.getElementById(content_id);
var inputEventFunction = function() {
var searchText = input.value.trim().toLowerCase();
var keywords = searchText.split(/[\s\-]+/);
if (keywords.length > 1) {
keywords.push(searchText);
}
var resultItems = [];
if (searchText.length > 0) {
// perform local searching
datas.forEach(function(data) {
var isMatch = false;
var hitCount = 0;
var searchTextCount = 0;
var title = data.title.trim();
var titleInLowerCase = title.toLowerCase();
var content = data.content.trim().replace(/<[^>]+>/g,"");
var contentInLowerCase = content.toLowerCase();
var articleUrl = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
var indexOfTitle = [];
var indexOfContent = [];
// only match articles with not empty titles
if(title != '') {
keywords.forEach(function(keyword) {
function getIndexByWord(word, text, caseSensitive) {
var wordLen = word.length;
if (wordLen === 0) {
return [];
}
var startPosition = 0, position = [], index = [];
if (!caseSensitive) {
text = text.toLowerCase();
word = word.toLowerCase();
}
while ((position = text.indexOf(word, startPosition)) > -1) {
index.push({position: position, word: word});
startPosition = position + wordLen;
}
return index;
}
indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
});
if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
isMatch = true;
hitCount = indexOfTitle.length + indexOfContent.length;
}
}
// show search results
if (isMatch) {
// sort index by position of keyword
[indexOfTitle, indexOfContent].forEach(function (index) {
index.sort(function (itemLeft, itemRight) {
if (itemRight.position !== itemLeft.position) {
return itemRight.position - itemLeft.position;
} else {
return itemLeft.word.length - itemRight.word.length;
}
});
});
// merge hits into slices
function mergeIntoSlice(text, start, end, index) {
var item = index[index.length - 1];
var position = item.position;
var word = item.word;
var hits = [];
var searchTextCountInSlice = 0;
while (position + word.length <= end && index.length != 0) {
if (word === searchText) {
searchTextCountInSlice++;
}
hits.push({position: position, length: word.length});
var wordEnd = position + word.length;
// move to next position of hit
index.pop();
while (index.length != 0) {
item = index[index.length - 1];
position = item.position;
word = item.word;
if (wordEnd > position) {
index.pop();
} else {
break;
}
}
}
searchTextCount += searchTextCountInSlice;
return {
hits: hits,
start: start,
end: end,
searchTextCount: searchTextCountInSlice
};
}
var slicesOfTitle = [];
if (indexOfTitle.length != 0) {
slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
}
var slicesOfContent = [];
while (indexOfContent.length != 0) {
var item = indexOfContent[indexOfContent.length - 1];
var position = item.position;
var word = item.word;
// cut out 100 characters
var start = position - 20;
var end = position + 80;
if(start < 0){
start = 0;
}
if (end < position + word.length) {
end = position + word.length;
}
if(end > content.length){
end = content.length;
}
slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
}
// sort slices in content by search text's count and hits' count
slicesOfContent.sort(function (sliceLeft, sliceRight) {
if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
return sliceRight.searchTextCount - sliceLeft.searchTextCount;
} else if (sliceLeft.hits.length !== sliceRight.hits.length) {
return sliceRight.hits.length - sliceLeft.hits.length;
} else {
return sliceLeft.start - sliceRight.start;
}
});
// select top N slices in content
var upperBound = parseInt('1');
if (upperBound >= 0) {
slicesOfContent = slicesOfContent.slice(0, upperBound);
}
// highlight title and content
function highlightKeyword(text, slice) {
var result = '';
var prevEnd = slice.start;
slice.hits.forEach(function (hit) {
result += text.substring(prevEnd, hit.position);
var end = hit.position + hit.length;
result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
prevEnd = end;
});
result += text.substring(prevEnd, slice.end);
return result;
}
var resultItem = '';
if (slicesOfTitle.length != 0) {
resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
} else {
resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
}
slicesOfContent.forEach(function (slice) {
resultItem += "<a href='" + articleUrl + "'>" +
"<p class=\"search-result\">" + highlightKeyword(content, slice) +
"...</p>" + "</a>";
});
resultItem += "</li>";
resultItems.push({
item: resultItem,
searchTextCount: searchTextCount,
hitCount: hitCount,
id: resultItems.length
});
}
})
};
if (keywords.length === 1 && keywords[0] === "") {
resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x"></i></div>'
} else if (resultItems.length === 0) {
resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x"></i></div>'
} else {
resultItems.sort(function (resultLeft, resultRight) {
if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
return resultRight.searchTextCount - resultLeft.searchTextCount;
} else if (resultLeft.hitCount !== resultRight.hitCount) {
return resultRight.hitCount - resultLeft.hitCount;
} else {
return resultRight.id - resultLeft.id;
}
});
var searchResultList = '<ul class=\"search-result-list\">';
resultItems.forEach(function (result) {
searchResultList += result.item;
})
searchResultList += "</ul>";
resultContent.innerHTML = searchResultList;
}
}
if ('auto' === 'auto') {
input.addEventListener('input', inputEventFunction);
} else {
$('.search-icon').click(inputEventFunction);
input.addEventListener('keypress', function (event) {
if (event.keyCode === 13) {
inputEventFunction();
}
});
}
// remove loading animation
$(".local-search-pop-overlay").remove();
$('body').css('overflow', '');
proceedsearch();
}
});
}
// handle and trigger popup window;
$('.popup-trigger').click(function(e) {
e.stopPropagation();
if (isfetched === false) {
searchFunc(path, 'local-search-input', 'local-search-result');
} else {
proceedsearch();
};
});
$('.popup-btn-close').click(onPopupClose);
$('.popup').click(function(e){
e.stopPropagation();
});
$(document).on('keyup', function (event) {
var shouldDismissSearchPopup = event.which === 27 &&
$('.search-popup').is(':visible');
if (shouldDismissSearchPopup) {
onPopupClose();
}
});
</script>
<script src="https://www.gstatic.com/firebasejs/4.6.0/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.6.0/firebase-firestore.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.5.1/bluebird.core.min.js"></script>
<script>
(function () {
firebase.initializeApp({
apiKey: '',
projectId: ''
})
function getCount(doc, increaseCount) {
//increaseCount will be false when not in article page
return doc.get().then(function (d) {
var count
if (!d.exists) { //has no data, initialize count
if (increaseCount) {
doc.set({
count: 1
})
count = 1
}
else {
count = 0
}
}
else { //has data
count = d.data().count
if (increaseCount) {
if (!(window.localStorage && window.localStorage.getItem(title))) { //if first view this article
doc.set({ //increase count
count: count + 1
})
count++
}
}
}
if (window.localStorage && increaseCount) { //mark as visited
localStorage.setItem(title, true)
}
return count
})
}
function appendCountTo(el) {
return function (count) {
$(el).append(
$('<span>').addClass('post-visitors-count').append(
$('<span>').addClass('post-meta-divider').text('|')
).append(
$('<span>').addClass('post-meta-item-icon').append(
$('<i>').addClass('fa fa-users')
)
).append($('<span>').text('阅读次数 ' + count))
)
}
}
var db = firebase.firestore()
var articles = db.collection('articles')
//https://hexo.io/docs/variables.html
var isPost = 'Apache Flink 零基础入门(一):基础概念解析'.length > 0
var isArchive = '' === 'true'
var isCategory = ''.length > 0
var isTag = ''.length > 0
if (isPost) { //is article page
var title = 'Apache Flink 零基础入门(一):基础概念解析'
var doc = articles.doc(title)
getCount(doc, true).then(appendCountTo($('.post-meta')))
}
else if (!isArchive && !isCategory && !isTag) { //is index page
var titles = [] //array to titles
var postsstr = '' //if you have a better way to get titles of posts, please change it
eval(postsstr)
var promises = titles.map(function (title) {
return articles.doc(title)
}).map(function (doc) {
return getCount(doc)
})
Promise.all(promises).then(function (counts) {
var metas = $('.post-meta')
counts.forEach(function (val, idx) {
appendCountTo(metas[idx])(val)
})
})
}
})()
</script>
<script>
if ($('body').find('div.pdf').length) {
$.ajax({
type: 'GET',
url: '//cdn.jsdelivr.net/npm/pdfobject@2/pdfobject.min.js',
dataType: 'script',
cache: true,
success: function() {
$('body').find('div.pdf').each(function(i, o) {
PDFObject.embed($(o).attr('target'), $(o), {
pdfOpenParams: {
navpanes: 0,
toolbar: 0,
statusbar: 0,
pagemode: 'thumbs',
view: 'FitH'
},
PDFJS_URL: '/lib/pdf/web/viewer.html',
height: $(o).attr('height') || '500px'
});
});
},
});
}
</script>
<script>
if ($('body').find('pre.mermaid').length) {
$.ajax({
type: 'GET',
url: '//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js',
dataType: 'script',
cache: true,
success: function() {
mermaid.initialize({
theme: 'dark',
logLevel: 3,
flowchart: { curve: 'linear' },
gantt: { axisFormat: '%m/%d/%Y' },
sequence: { actorMargin: 50 }
});
}
});
}
</script>
<script>
(function(){
var bp = document.createElement('script');
var curProtocol = window.location.protocol.split(':')[0];
bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(bp, s);
})();
</script>
<script src="/lib/bookmark/bookmark.min.js?v=1.0"></script>
<script>
bookmark.scrollToMark('auto', "#更多");
</script>
<script>
$('.highlight').not('.gist .highlight').each(function(i, e) {
var $wrap = $('<div>').addClass('highlight-wrap');
$(e).after($wrap);
$wrap.append($('<button>').addClass('copy-btn').append('复制').on('click', function(e) {
var code = $(this).parent().find('.code').find('.line').map(function(i, e) {
return $(e).text();
}).toArray().join('\n');
var ta = document.createElement('textarea');
var yPosition = window.pageYOffset || document.documentElement.scrollTop;
ta.style.top = yPosition + 'px'; // Prevent page scroll
ta.style.position = 'absolute';
ta.style.opacity = '0';
ta.readOnly = true;
ta.value = code;
document.body.appendChild(ta);
const selection = document.getSelection();
const selected = selection.rangeCount > 0 ? selection.getRangeAt(0) : false;
ta.select();
ta.setSelectionRange(0, code.length);
ta.readOnly = false;
var result = document.execCommand('copy');
if (result) $(this).text('复制成功');
else $(this).text('复制失败');
ta.blur(); // For iOS
$(this).blur();
if (selected) {
selection.removeAllRanges();
selection.addRange(selected);
}
})).on('mouseleave', function(e) {
var $b = $(this).find('.copy-btn');
setTimeout(function() {
$b.text('复制');
}, 300);
}).append(e);
})
</script>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化