-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapache-flink:realtime-pressure.html
1180 lines (731 loc) · 51.2 KB
/
apache-flink:realtime-pressure.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
<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=1"/>
<meta name="theme-color" content="#222">
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">
<link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">
<meta name="keywords" content="," />
<link rel="alternate" href="/atom.xml" title="antigeneral's blog" type="application/atom+xml" />
<meta name="description" content="本站&作者">
<meta property="og:type" content="website">
<meta property="og:title" content="生产实践 | 实时压测方案沉淀">
<meta property="og:url" content="apache-flink:realtime-pressure.html">
<meta property="og:site_name" content="antigeneral's blog">
<meta property="og:description" content="本站&作者">
<meta property="og:locale">
<meta property="og:image" content="apache-flink:/blog-img/apache-flink:realtime-live-stream-3/tec-arc.png">
<meta property="og:image" content="apache-flink:/blog-img/apache-flink:realtime-live-stream-3/metric-prod-tec-arc.png">
<meta property="og:image" content="apache-flink:/blog-img/apache-flink:realtime-live-stream-3/metric-current-live-live-stream-number-life-cycle.png">
<meta property="article:published_time" content="2020-11-11T06:21:53.000Z">
<meta property="article:modified_time" content="2020-11-17T13:28:30.671Z">
<meta property="article:author" content="yangyichao-mango">
<meta property="article:tag" content="Apache Flink">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="apache-flink:/blog-img/apache-flink:realtime-live-stream-3/tec-arc.png">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '',
scheme: 'Muse',
version: '5.1.4',
sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
fancybox: true,
tabs: true,
motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
duoshuo: {
userId: '0',
author: '博主'
},
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>
<link rel="canonical" href="https://yangyichao-mango.github.io/apache-flink:realtime-pressure.html"/>
<title>生产实践 | 实时压测方案沉淀 | antigeneral's blog</title>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-150061865-1', 'auto');
ga('send', 'pageview');
</script>
<script type="text/javascript">
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?c013b4f82e10e4f302d6f461f20bdf63";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<meta name="generator" content="Hexo 5.4.0"></head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<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">antigeneral's blog</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle"></p>
</div>
<div class="site-nav-toggle">
<button>
<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-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-tags">
<a href="/tags" rel="section">
<i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
标签
</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 />
分类
</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 />
归档
</a>
</li>
<li class="menu-item menu-item-links">
<a href="/links" rel="section">
<i class="menu-item-icon fa fa-fw fa-link"></i> <br />
links
</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="post-block page">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">生产实践 | 实时压测方案沉淀</h1>
<div class="post-meta">
<div class="post-description">本站&作者</div>
</div>
</header>
<div class="post-body">
<h1 id="生产实践-Flink-直播(三)-如何建设当前正在直播-xx-数?"><a href="#生产实践-Flink-直播(三)-如何建设当前正在直播-xx-数?" class="headerlink" title="生产实践 | Flink + 直播(三)| 如何建设当前正在直播 xx 数?"></a>生产实践 | Flink + 直播(三)| 如何建设当前正在直播 xx 数?</h1><blockquote>
<p>本系列每篇文章都是从一些实际的 case 出发,分析一些生产环境中经常会遇到的问题,抛砖引玉,以帮助小伙伴们解决一些实际问题。本篇文章主要介绍直播间生产侧指标的建设过程,如果对小伙伴有帮助的话,欢迎点赞 + 再看~</p>
</blockquote>
<h2 id="整体架构"><a href="#整体架构" class="headerlink" title="整体架构"></a>整体架构</h2><p>本文主要介绍<strong>生产侧指标的建设</strong>,比如当前正在直播直播间数,或者主播数等。在介绍生产侧指标的建设过程之前,我们先回顾下上一节的<strong>架构</strong>图。</p>
<p><img src="/blog-img/apache-flink:realtime-live-stream-3/tec-arc.png" alt="架构"></p>
<p>而本篇要介绍的<strong>生产侧指标</strong>的数据链路主要对应以下几个模块。</p>
<ul>
<li>数据源:读取直播生产,比如开播,关播等 kafka 数据源日志;</li>
<li>数据处理:使用生产侧数据源 + 实时画像维表 + flink 建设生产侧实时指标;</li>
<li>数据汇:将处理完成的指标数据写入到 kafka 中。</li>
</ul>
<p>我用另一张图进行了标注,图中<strong>标红</strong>模块为生产侧指标的数据链路涉及到的模块。</p>
<p><img src="/blog-img/apache-flink:realtime-live-stream-3/metric-prod-tec-arc.png" alt="生产侧架构"></p>
<p>其中直播间实时画像维表的介绍已经在上节进行了介绍,感兴趣的话可以点击以下链接,跳转到上节进行阅读~</p>
<p>本小节就不针对<strong>生产侧指标的建设</strong>中所有涉及指标的建设过程进行详细介绍了,我们主要以<strong>当前分钟正在开播直播间数</strong>作为<strong>生产侧指标建设</strong>的一个代表性案例,介绍这个指标的整个建设过程。<br>来为大家还原生产侧指标的业务过程以及技术方案。</p>
<h2 id="Question"><a href="#Question" class="headerlink" title="Question"></a>Question</h2><p>仍然从几个问题入手,介绍<strong>当前分钟正在开播直播间数</strong>的建设过程。</p>
<ul>
<li><strong>当前分钟正在开播直播间数</strong>的定义什么?业务过程是怎么样的?举例?</li>
<li>怎样去建设这个指标?整体的指标计算流程?</li>
</ul>
<h2 id="1-聊聊定义?"><a href="#1-聊聊定义?" class="headerlink" title="1.聊聊定义?"></a>1.聊聊定义?</h2><p>当前分钟正在开播直播间数,其定义就是整个平台中,当前分钟正在开播的直播间数 + 单层维度下钻的当前分钟正在开播的直播间数。</p>
<p>举例:</p>
<p>现在的时间点是 2020-11-11 12:42,真实直播的直播间数为 3000 个(平台维度下钻:IOS 平台为 1500,安卓平台为 1500)</p>
<p>到了 12:43 时,有 200 个直播间进行了关播(其中 100 个为 IOS,100 个为安卓),有 100 个直播间开播(全部为 IOS),则当前正在直播的直播间数为 2900(平台维度下钻:IOS 平台为 1500,安卓平台为 1400)。</p>
<p>其中 2020-11-11 12:42 的 3000 以及 2020-11-11 12:43 的 2900 以及按照平台下钻的数值就为当前时间正在开播的直播间数。</p>
<p>因此根据上述定义和分析,我们可以直接将数据源和数据汇的 schema 定义下来,主体信息如下。</p>
<h3 id="数据源-schema"><a href="#数据源-schema" class="headerlink" title="数据源 schema"></a>数据源 schema</h3><table>
<thead>
<tr>
<th>字段</th>
<th>备注</th>
</tr>
</thead>
<tbody><tr>
<td>live_stream_id</td>
<td>直播间 id</td>
</tr>
<tr>
<td>author_id</td>
<td>主播 id</td>
</tr>
<tr>
<td>start_or_end</td>
<td>开播还是关播</td>
</tr>
<tr>
<td>timestamp</td>
<td>时间戳</td>
</tr>
<tr>
<td>…</td>
<td>…</td>
</tr>
</tbody></table>
<h3 id="数据汇-schema"><a href="#数据汇-schema" class="headerlink" title="数据汇 schema"></a>数据汇 schema</h3><table>
<thead>
<tr>
<th>字段</th>
<th>备注</th>
</tr>
</thead>
<tbody><tr>
<td>timestamp</td>
<td>时间戳,汇总到分钟粒度</td>
</tr>
<tr>
<td>metric_name</td>
<td>指标名,举例:开播直播间数</td>
</tr>
<tr>
<td>metric_value</td>
<td>指标值,举例:3000(开播直播间数)</td>
</tr>
<tr>
<td>dim_name</td>
<td>维度名,举例:平台,版本</td>
</tr>
<tr>
<td>dim_value</td>
<td>维度值,举例:IOS,8.1</td>
</tr>
<tr>
<td>…</td>
<td>…</td>
</tr>
</tbody></table>
<blockquote>
<p>Notes:</p>
<p><strong>metric_name 和 metric_value</strong>:</p>
<p>这两个字段是为了之后进行指标扩充时进行的设计。比如后续如果需要加入开播主播数,开播时长等指标,不用修改数据汇 schema,只需要加一种 metric_name,就可以使用原有 schema 进行数据产出。</p>
<p><strong>dim_name 和 dim_value</strong>:</p>
<p>目前我们建设的指标只提供了进行单维度下钻的能力,所以设计了 dim_name 和 dim_value 两个字段,可满足用户查看平台为 IOS 的当前开播直播间数或者使用开播软件版本为 8.1 的当前开播直播间数。<br>如果后续业务场景需要多维下钻能力,可以在字段上面进行扩充。或者也可以提供明细数据在 OLAP 中进行多维下钻。</p>
</blockquote>
<h2 id="2-怎样建设?"><a href="#2-怎样建设?" class="headerlink" title="2.怎样建设?"></a>2.怎样建设?</h2><p>对于当前分钟正在开播直播间数来说,其计算方式很简单,就是下面这个数学公式:</p>
<p><strong>当前分钟正在开播直播间数</strong> = <strong>上一分钟正在开播直播间数</strong> + <strong>当前分钟开播直播间数</strong> - <strong>当前分钟关播直播间数</strong></p>
<p>可以从上面的公式可以看出,对于当前分钟正在开播直播间数的计算来说,是依赖上下文信息的,即<strong>上一分钟正在开播直播间数</strong>,这也就是我们所说的<strong>状态</strong>。</p>
<h3 id="指标处理逻辑"><a href="#指标处理逻辑" class="headerlink" title="指标处理逻辑"></a>指标处理逻辑</h3><p>从获取到数据源,到产出指标的整体处理逻辑如下图所示。这里就不进行赘述了。</p>
<p><img src="/blog-img/apache-flink:realtime-live-stream-3/metric-current-live-live-stream-number-life-cycle.png" alt="技术架构"></p>
<p>其中标为<strong>粉色</strong>的模块为任务中的<strong>状态</strong>,即任务中一直存储的当前分钟正在开播直播间数。</p>
<h3 id="状态"><a href="#状态" class="headerlink" title="状态"></a>状态</h3><p>上述指标涉及到了,状态,那么我这里讲一下我对<strong>状态</strong>的理解。如有错误,请在文末讨论中进行指出,我会和大家讨论。</p>
<p>状态其实就是一个记录上下文信息的东西,如果当前的计算过程依赖到上次计算的结果,那么上次计算的结果就是状态。举几个🌰;</p>
<ul>
<li><p><strong>流处理</strong>:如本节介绍的<strong>当前分钟正在开播直播间数</strong>的计算,就是依赖上一分钟的正在开播直播间数(状态)进行的计算。<br>可能有小伙伴会说,我不依赖上一分钟,我从头开始计算可以不?答案是可以的,但是从头开始计算,也需要将所有历史数据进行存储,这些历史数据其实也就是状态,只不过我们将其优化为了上一分钟开播直播间数。</p>
</li>
<li><p><strong>批处理</strong>:今天的全量表 = 昨天全量表(状态) + 今天的增量表。</p>
</li>
<li><p><strong>数据库存储</strong>:最常见的 mysql 主键自增,unique key 等。<br>为什么新插入一条数据主键会自增?因为 mysql 存储了主键的上一个值(状态)。<br>为什么插入相同数据时,由于 unique key 会导致报错,就是因为 mysql 存储了所有 unique key 的字段的数据(状态)。</p>
</li>
<li><p><strong>生活</strong>:当前的手机电量 = 上一分钟的手机电量(状态) + (充电/用电量)。<br>为什么你越来越喜欢你的另一半?因为你对她的感觉 = 前一秒你对她的感觉(状态) + 当前这一秒她亲了你一下。</p>
</li>
</ul>
<p>生活中随处可见状态,即使你不是程序员,我相信也都可以理解状态的概念。</p>
<h3 id="指标计算代码示例"><a href="#指标计算代码示例" class="headerlink" title="指标计算代码示例"></a>指标计算代码示例</h3><p>按照最简单的实现方式举例如下。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LiveStreamRealtimeMetricProdProcessorJob</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line"> <span class="keyword">final</span> StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();</span><br><span class="line"></span><br><span class="line"> DataStream<SourceModel> source = SourceFactory.getSourceDataStream(...);</span><br><span class="line"></span><br><span class="line"> DataStream<SinkModel> result = source</span><br><span class="line"> .keyBy(<span class="keyword">new</span> KeySelector<SourceModel, Long>() {</span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> Long <span class="title">getKey</span><span class="params">(SourceModel commonModel)</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line"> <span class="keyword">return</span> commonModel.getLiveStreamId() % <span class="number">1000</span>;</span><br><span class="line"> }</span><br><span class="line"> })</span><br><span class="line"> .timeWindow(Time.seconds(<span class="number">60</span>))</span><br><span class="line"> .process(<span class="keyword">new</span> ProcessWindowFunction<SourceModel, SinkModel, Long, TimeWindow>() {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> ValueState<Long> playingLiveStreamNumberValueState;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">open</span><span class="params">(Configuration parameters)</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line"> <span class="keyword">super</span>.open(parameters);</span><br><span class="line"> <span class="keyword">this</span>.playingLiveStreamNumberValueState = getRuntimeContext().getState(...);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">process</span><span class="params">(Long bucket, Context context, Iterable<SourceModel> iterable,</span></span></span><br><span class="line"><span class="function"><span class="params"> Collector<SinkModel> collector)</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line"> Long playingLiveStreamNumber = <span class="keyword">this</span>.playingLiveStreamNumberValueState.value();</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (<span class="keyword">null</span> == playingLiveStreamNumber) {</span><br><span class="line"> playingLiveStreamNumber = <span class="number">0L</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> List<SourceModel> sourceModels = (List<SourceModel>) iterable;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span> (SourceModel sourceModel : sourceModels) {</span><br><span class="line"> <span class="keyword">if</span> (BizType.I == sourceModel.getBizType()) {</span><br><span class="line"> playingLiveStreamNumber++;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> playingLiveStreamNumber--;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">this</span>.playingLiveStreamNumberValueState.update(playingLiveStreamNumber);</span><br><span class="line"></span><br><span class="line"> collector.collect(</span><br><span class="line"> SinkModel.builder().build()</span><br><span class="line"> );</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> SinkFactory.setSinkDataStream(...);</span><br><span class="line"></span><br><span class="line"> env.execute();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Data</span></span><br><span class="line"> <span class="meta">@Builder</span></span><br><span class="line"> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">SourceModel</span> </span>{</span><br><span class="line"> <span class="comment">// 直播间id</span></span><br><span class="line"> <span class="keyword">private</span> Long liveStreamId;</span><br><span class="line"> <span class="comment">// 开播时间,关播时间</span></span><br><span class="line"> <span class="keyword">private</span> Long time;</span><br><span class="line"> <span class="comment">// 主播id</span></span><br><span class="line"> <span class="keyword">private</span> Long authorId;</span><br><span class="line"> <span class="comment">// binlog 时间戳</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">long</span> binlogTimestamp;</span><br><span class="line"> <span class="comment">// 开播,关播</span></span><br><span class="line"> <span class="keyword">private</span> BizType bizType;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="class"><span class="keyword">enum</span> <span class="title">BizType</span> </span>{</span><br><span class="line"> I, <span class="comment">// 开播</span></span><br><span class="line"> D, <span class="comment">// 关播</span></span><br><span class="line"> ;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Data</span></span><br><span class="line"> <span class="meta">@Builder</span></span><br><span class="line"> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">SinkModel</span> </span>{</span><br><span class="line"> <span class="comment">// 时间戳,汇总到分钟粒度</span></span><br><span class="line"> <span class="keyword">private</span> Long timestamp;</span><br><span class="line"> <span class="comment">// 指标名</span></span><br><span class="line"> <span class="keyword">private</span> String metricName;</span><br><span class="line"> <span class="comment">// 指标值</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">double</span> metricValue;</span><br><span class="line"> <span class="comment">// 维度名</span></span><br><span class="line"> <span class="keyword">private</span> String dimName;</span><br><span class="line"> <span class="comment">// 维度值</span></span><br><span class="line"> <span class="keyword">private</span> String dimValue;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>本文衔接上文,主要介绍直播间<strong>生产侧指标的建设</strong>,以<strong>当前分钟正在开播直播间数</strong>为代表举例。提出定义以及建设过程相关的问题,以这两个个问题出发,引出了以下两小节。</p>
<p>第一节简单介绍了当前分钟正在开播直播间数的定义。</p>
<p>第二节主要介绍了当前分钟正在开播直播间数的建设逻辑以及过程,并对<strong>状态</strong>这个概念进行了一个拓展介绍。</p>
<p>最后一节对本文进行了总结。</p>
<p>如果你也有相同的指标建设需求,或者存在一些指标建设过程中的问题,欢迎关注博主公众号,或者添加博主微信,互相交流~</p>
<p>记得点赞 + 再看喔~</p>
</div>
</div>
</div>
</div>
<div class="comments" id="comments">
<div id="disqus_thread">
<noscript>
Please enable JavaScript to view the
<a target="_blank" rel="noopener" href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
</noscript>
</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>
<section 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="/blog-img/avatar.jpeg"
alt="" />
<p class="site-author-name" itemprop="name"></p>
<p class="site-description motion-element" itemprop="description"></p>
</div>
<nav class="site-state motion-element">
<div class="site-state-item site-state-posts">
<a href="/archives%7C%7C%20archive">
<span class="site-state-item-count">115</span>
<span class="site-state-item-name">日志</span>
</a>
</div>
<div class="site-state-item site-state-categories">
<a href="/categories/index.html">
<span class="site-state-item-count">16</span>
<span class="site-state-item-name">分类</span>
</a>
</div>
<div class="site-state-item site-state-tags">
<a href="/tags/index.html">
<span class="site-state-item-count">20</span>
<span class="site-state-item-name">标签</span>
</a>
</div>
</nav>
<div class="feed-link motion-element">
<a href="/atom.xml" rel="alternate">
<i class="fa fa-rss"></i>
RSS
</a>
</div>
<div class="links-of-author motion-element">
<span class="links-of-author-item">
<a href="https://github.com/yangyichao-mango" target="_blank" title="GitHub">
<i class="fa fa-fw fa-github"></i>GitHub</a>
</span>
<span class="links-of-author-item">
<a href="mailto:1048262223@qq.com" target="_blank" title="E-Mail">
<i class="fa fa-fw fa-envelope"></i>E-Mail</a>
</span>
</div>
<div class="cc-license motion-element" itemprop="license">
<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" target="_blank">
<img src="/images/cc-by-nc-sa.svg" alt="Creative Commons" />
</a>
</div>
</div>
</section>
<!--noindex-->
<section 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-1"><a class="nav-link" href="#%E7%94%9F%E4%BA%A7%E5%AE%9E%E8%B7%B5-Flink-%E7%9B%B4%E6%92%AD%EF%BC%88%E4%B8%89%EF%BC%89-%E5%A6%82%E4%BD%95%E5%BB%BA%E8%AE%BE%E5%BD%93%E5%89%8D%E6%AD%A3%E5%9C%A8%E7%9B%B4%E6%92%AD-xx-%E6%95%B0%EF%BC%9F"><span class="nav-number">1.</span> <span class="nav-text">生产实践 | Flink + 直播(三)| 如何建设当前正在直播 xx 数?</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%95%B4%E4%BD%93%E6%9E%B6%E6%9E%84"><span class="nav-number">1.1.</span> <span class="nav-text">整体架构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Question"><span class="nav-number">1.2.</span> <span class="nav-text">Question</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E8%81%8A%E8%81%8A%E5%AE%9A%E4%B9%89%EF%BC%9F"><span class="nav-number">1.3.</span> <span class="nav-text">1.聊聊定义?</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E6%BA%90-schema"><span class="nav-number">1.3.1.</span> <span class="nav-text">数据源 schema</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E6%B1%87-schema"><span class="nav-number">1.3.2.</span> <span class="nav-text">数据汇 schema</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%80%8E%E6%A0%B7%E5%BB%BA%E8%AE%BE%EF%BC%9F"><span class="nav-number">1.4.</span> <span class="nav-text">2.怎样建设?</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8C%87%E6%A0%87%E5%A4%84%E7%90%86%E9%80%BB%E8%BE%91"><span class="nav-number">1.4.1.</span> <span class="nav-text">指标处理逻辑</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%8A%B6%E6%80%81"><span class="nav-number">1.4.2.</span> <span class="nav-text">状态</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8C%87%E6%A0%87%E8%AE%A1%E7%AE%97%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B"><span class="nav-number">1.4.3.</span> <span class="nav-text">指标计算代码示例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-number">1.5.</span> <span class="nav-text">总结</span></a></li></ol></li></ol></div>
</div>
</section>
<!--/noindex-->
</div>
</aside>
</div>
</main>
<footer id="footer" class="footer">
<div class="footer-inner">
<div class="copyright">© 2019 — <span itemprop="copyrightYear">2022</span>
<span class="with-love">
<i class="fa fa-user"></i>
</span>
<span class="author" itemprop="copyrightHolder">yangyichao-mango</span>
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-area-chart"></i>
</span>
<span class="post-meta-item-text">Site words total count:</span>
<span title="Site words total count">244.8k</span>
</div>
<div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>
<span class="post-meta-divider">|</span>
<div class="theme-info">主题 — <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>
</div>
</footer>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
</div>
</div>
<script type="text/javascript">
if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
window.Promise = null;
}
</script>
<script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
<script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
<script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>
<script id="dsq-count-scr" src="https://true.disqus.com/count.js" async></script>
<script type="text/javascript">
var disqus_config = function () {
this.page.url = 'apache-flink:realtime-pressure.html';
this.page.identifier = 'apache-flink:realtime-pressure.html';
this.page.title = '生产实践 | 实时压测方案沉淀';
};
var d = document, s = d.createElement('script');
s.src = 'https://true.disqus.com/embed.js';
s.setAttribute('data-timestamp', '' + +new Date());
(d.head || d.body).appendChild(s);
</script>
<script type="text/javascript">
// 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);
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;
}
}