-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
2364 lines (1234 loc) · 88.1 KB
/
index.html
File metadata and controls
2364 lines (1234 loc) · 88.1 KB
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 pisces use-motion" lang="">
<head>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><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 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="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" 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.0" rel="stylesheet" type="text/css" />
<meta name="keywords" content="Hexo, NexT" />
<link rel="alternate" href="/atom.xml" title="Duncan's Blog" type="application/atom+xml" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />
<meta name="description" content="write something useful">
<meta property="og:type" content="website">
<meta property="og:title" content="Duncan's Blog">
<meta property="og:url" content="https://github.com/DuncanZhou/index.html">
<meta property="og:site_name" content="Duncan's Blog">
<meta property="og:description" content="write something useful">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Duncan's Blog">
<meta name="twitter:description" content="write something useful">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Pisces',
sidebar: {"position":"left","display":"post"},
fancybox: true,
motion: true,
duoshuo: {
userId: '0',
author: '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://github.com/DuncanZhou/"/>
<title> Duncan's Blog </title><!-- hexo-inject:begin --><!-- hexo-inject:end -->
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="">
<!-- hexo-inject:begin --><!-- hexo-inject:end --><div class="container one-collumn sidebar-position-left
page-home
">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><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">Duncan'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>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-主页">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br />
menu.主页
</a>
</li>
<li class="menu-item menu-item-分类">
<a href="/categories" rel="section">
<i class="menu-item-icon fa fa-fw fa-th"></i> <br />
menu.分类
</a>
</li>
<li class="menu-item menu-item-关于">
<a href="/about" rel="section">
<i class="menu-item-icon fa fa-fw fa-user"></i> <br />
menu.关于
</a>
</li>
<li class="menu-item menu-item-归档">
<a href="/archives" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
menu.归档
</a>
</li>
<li class="menu-item menu-item-标签">
<a href="/tags" rel="section">
<i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
menu.标签
</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="https://github.com/DuncanZhou/2019/09/23/Spark笔记/">
<span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="duncan">
<meta itemprop="description" content="">
<meta itemprop="image" content="https://raw.githubusercontent.com/DuncanZhou/images/master/webwxgetmsgimg.jpg">
</span>
<span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Duncan's Blog">
<span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
<img style="display:none;" itemprop="url image" alt="Duncan's Blog" src="">
</span>
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/09/23/Spark笔记/" itemprop="url">
Spark笔记
</a>
</h1>
<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">Veröffentlicht am</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2019-09-23T00:00:00+08:00">
2019-09-23
</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">in</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Learning/" itemprop="url" rel="index">
<span itemprop="name">Learning</span>
</a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="Spark笔记"><a href="#Spark笔记" class="headerlink" title="Spark笔记"></a>Spark笔记</h3><h3 id="1-数据结构方式"><a href="#1-数据结构方式" class="headerlink" title="1.数据结构方式"></a>1.数据结构方式</h3><p> RDD是Spark处理数据的数据结构,可以通过两种方式加载数据创建RDD</p>
<ul>
<li>从程序中parallelize一种现有的数据:如Array</li>
<li>从外部读取文件:CSV,Hive等</li>
</ul>
<h3 id="2-RDD操作类型"><a href="#2-RDD操作类型" class="headerlink" title="2.RDD操作类型"></a>2.RDD操作类型</h3><p>2.1 RDD的计算方式是lazy加载,即用的时候再计算。</p>
<p>2.2 如果一个变量需要经常使用,可以持久化persist</p>
<p>2.3 封装函数有多种方式:</p>
<ul>
<li>封装静态方法,创建<strong>object</strong></li>
<li>封装方法以及变量参数等等,创建<strong>class</strong></li>
</ul>
<p>2.3 常用转换方法</p>
<div class="table-container">
<table>
<thead>
<tr>
<th style="text-align:left">Transformation</th>
<th style="text-align:left">Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left"><strong>map</strong>(<em>func</em>)</td>
<td style="text-align:left">Return a new distributed dataset formed by passing each element of the source through a function <em>func</em>.</td>
</tr>
<tr>
<td style="text-align:left"><strong>filter</strong>(<em>func</em>)</td>
<td style="text-align:left">Return a new dataset formed by selecting those elements of the source on which <em>func</em>returns true.</td>
</tr>
<tr>
<td style="text-align:left"><strong>flatMap</strong>(<em>func</em>)</td>
<td style="text-align:left">Similar to map, but each input item can be mapped to 0 or more output items (so <em>func</em> should return a Seq rather than a single item).</td>
</tr>
<tr>
<td style="text-align:left"><strong>mapPartitions</strong>(<em>func</em>)</td>
<td style="text-align:left">Similar to map, but runs separately on each partition (block) of the RDD, so <em>func</em> must be of type Iterator<t> => Iterator<u> when running on an RDD of type T.</u></t></td>
</tr>
<tr>
<td style="text-align:left"><strong>mapPartitionsWithIndex</strong>(<em>func</em>)</td>
<td style="text-align:left">Similar to mapPartitions, but also provides <em>func</em> with an integer value representing the index of the partition, so <em>func</em> must be of type (Int, Iterator<t>) => Iterator<u> when running on an RDD of type T.</u></t></td>
</tr>
<tr>
<td style="text-align:left"><strong>sample</strong>(<em>withReplacement</em>, <em>fraction</em>, <em>seed</em>)</td>
<td style="text-align:left">Sample a fraction <em>fraction</em> of the data, with or without replacement, using a given random number generator seed.</td>
</tr>
<tr>
<td style="text-align:left"><strong>union</strong>(<em>otherDataset</em>)</td>
<td style="text-align:left">Return a new dataset that contains the union of the elements in the source dataset and the argument.</td>
</tr>
<tr>
<td style="text-align:left"><strong>intersection</strong>(<em>otherDataset</em>)</td>
<td style="text-align:left">Return a new RDD that contains the intersection of elements in the source dataset and the argument.</td>
</tr>
<tr>
<td style="text-align:left"><strong>distinct</strong>([<em>numPartitions</em>]))</td>
<td style="text-align:left">Return a new dataset that contains the distinct elements of the source dataset.</td>
</tr>
<tr>
<td style="text-align:left"><strong>groupByKey</strong>([<em>numPartitions</em>])</td>
<td style="text-align:left">When called on a dataset of (K, V) pairs, returns a dataset of (K, Iterable<v>) pairs. <strong>Note:</strong> If you are grouping in order to perform an aggregation (such as a sum or average) over each key, using <code>reduceByKey</code> or <code>aggregateByKey</code> will yield much better performance. <strong>Note:</strong> By default, the level of parallelism in the output depends on the number of partitions of the parent RDD. You can pass an optional <code>numPartitions</code> argument to set a different number of tasks.</v></td>
</tr>
<tr>
<td style="text-align:left"><strong>reduceByKey</strong>(<em>func</em>, [<em>numPartitions</em>])</td>
<td style="text-align:left">When called on a dataset of (K, V) pairs, returns a dataset of (K, V) pairs where the values for each key are aggregated using the given reduce function <em>func</em>, which must be of type (V,V) => V. Like in <code>groupByKey</code>, the number of reduce tasks is configurable through an optional second argument.</td>
</tr>
<tr>
<td style="text-align:left"><strong>aggregateByKey</strong>(<em>zeroValue</em>)(<em>seqOp</em>, <em>combOp</em>, [<em>numPartitions</em>])</td>
<td style="text-align:left">When called on a dataset of (K, V) pairs, returns a dataset of (K, U) pairs where the values for each key are aggregated using the given combine functions and a neutral “zero” value. Allows an aggregated value type that is different than the input value type, while avoiding unnecessary allocations. Like in <code>groupByKey</code>, the number of reduce tasks is configurable through an optional second argument.</td>
</tr>
<tr>
<td style="text-align:left"><strong>sortByKey</strong>([<em>ascending</em>], [<em>numPartitions</em>])</td>
<td style="text-align:left">When called on a dataset of (K, V) pairs where K implements Ordered, returns a dataset of (K, V) pairs sorted by keys in ascending or descending order, as specified in the boolean <code>ascending</code> argument.</td>
</tr>
<tr>
<td style="text-align:left"><strong>join</strong>(<em>otherDataset</em>, [<em>numPartitions</em>])</td>
<td style="text-align:left">When called on datasets of type (K, V) and (K, W), returns a dataset of (K, (V, W)) pairs with all pairs of elements for each key. Outer joins are supported through <code>leftOuterJoin</code>, <code>rightOuterJoin</code>, and <code>fullOuterJoin</code>.</td>
</tr>
<tr>
<td style="text-align:left"><strong>cogroup</strong>(<em>otherDataset</em>, [<em>numPartitions</em>])</td>
<td style="text-align:left">When called on datasets of type (K, V) and (K, W), returns a dataset of (K, (Iterable<v>, Iterable<w>)) tuples. This operation is also called <code>groupWith</code>.</w></v></td>
</tr>
<tr>
<td style="text-align:left"><strong>cartesian</strong>(<em>otherDataset</em>)</td>
<td style="text-align:left">When called on datasets of types T and U, returns a dataset of (T, U) pairs (all pairs of elements).</td>
</tr>
<tr>
<td style="text-align:left"><strong>pipe</strong>(<em>command</em>, <em>[envVars]</em>)</td>
<td style="text-align:left">Pipe each partition of the RDD through a shell command, e.g. a Perl or bash script. RDD elements are written to the process’s stdin and lines output to its stdout are returned as an RDD of strings.</td>
</tr>
<tr>
<td style="text-align:left"><strong>coalesce</strong>(<em>numPartitions</em>)</td>
<td style="text-align:left">Decrease the number of partitions in the RDD to numPartitions. Useful for running operations more efficiently after filtering down a large dataset.</td>
</tr>
<tr>
<td style="text-align:left"><strong>repartition</strong>(<em>numPartitions</em>)</td>
<td style="text-align:left">Reshuffle the data in the RDD randomly to create either more or fewer partitions and balance it across them. This always shuffles all data over the network.</td>
</tr>
<tr>
<td style="text-align:left"><strong>repartitionAndSortWithinPartitions</strong>(<em>partitioner</em>)</td>
<td style="text-align:left">Repartition the RDD according to the given partitioner and, within each resulting partition, sort records by their keys. This is more efficient than calling <code>repartition</code> and then sorting within each partition because it can push the sorting down into the shuffle machinery.</td>
</tr>
</tbody>
</table>
</div>
<h3 id="3-创建DataFrame的三种方式"><a href="#3-创建DataFrame的三种方式" class="headerlink" title="3.创建DataFrame的三种方式"></a>3.创建DataFrame的三种方式</h3><ul>
<li><p>使用toDF函数</p>
</li>
<li><p>使用createDataFrame函数</p>
</li>
<li>通过文件直接创建</li>
</ul>
<h3 id="4-scala的vector和spark包中vector不一样"><a href="#4-scala的vector和spark包中vector不一样" class="headerlink" title="4.scala的vector和spark包中vector不一样"></a>4.scala的vector和spark包中vector不一样</h3><h3 id="5-Spark优化:(美团Spark)"><a href="#5-Spark优化:(美团Spark)" class="headerlink" title="5.Spark优化:(美团Spark)"></a>5.Spark优化:(美团Spark)</h3><p>基础版:<a href="https://tech.meituan.com/2016/04/29/spark-tuning-basic.html" target="_blank" rel="external">https://tech.meituan.com/2016/04/29/spark-tuning-basic.html</a></p>
<p>高级版:<a href="https://tech.meituan.com/2016/05/12/spark-tuning-pro.html" target="_blank" rel="external">https://tech.meituan.com/2016/05/12/spark-tuning-pro.html</a></p>
<h3 id="6-Spark保留运行环境(用于查错)"><a href="#6-Spark保留运行环境(用于查错)" class="headerlink" title="6.Spark保留运行环境(用于查错)"></a>6.Spark保留运行环境(用于查错)</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">conf.spark.yarn.preserve.staging.files=true</div></pre></td></tr></table></figure>
<h3 id="7-宽依赖和窄依赖"><a href="#7-宽依赖和窄依赖" class="headerlink" title="7.宽依赖和窄依赖"></a>7.宽依赖和窄依赖</h3><ul>
<li><strong>窄依赖</strong>:指父RDD的每个分区只被一个子RDD分区使用,子RDD分区通常只对应常数个父RDD分区。(map、filter、union操作)。</li>
<li><strong>宽依赖</strong>:指父RDD的每个分区都有可能被多个子RDD分区使用,子RDD分区通常对应父RDD所有分区。(groupByKey、partitionBy等操作)</li>
<li>比较:<strong>宽依赖通常对应着shuffle操作</strong>,需要在运行的过程中将同一个RDD分区传入到不同的RDD分区中,中间可能涉及多个节点之间数据的传输。</li>
</ul>
<h3 id="8-ORC格式和PARQUET格式文件对比"><a href="#8-ORC格式和PARQUET格式文件对比" class="headerlink" title="8.ORC格式和PARQUET格式文件对比"></a>8.ORC格式和PARQUET格式文件对比</h3><blockquote>
<p>impala暂时不支持orc格式的表查询</p>
</blockquote>
<h3 id="9-left-anti-join(某个字段过滤用)"><a href="#9-left-anti-join(某个字段过滤用)" class="headerlink" title="9.left anti join(某个字段过滤用)"></a>9.left anti join(某个字段过滤用)</h3><ul>
<li>left semi join —> exists</li>
<li>left anti join —> not exists</li>
</ul>
<h3 id="10-Shuffle过程数据倾斜"><a href="#10-Shuffle过程数据倾斜" class="headerlink" title="10.Shuffle过程数据倾斜"></a>10.Shuffle过程数据倾斜</h3><blockquote>
<p>和Hive中类似,数据的倾斜都发生在shuffle过程中,下面以hive的shuffle进行总结。发生倾斜的根本原因在于,shuffle之后,key的分布不均匀,使得大量的key集中在某个reduce节点,导致此节点过于“忙碌”,在其他节点都处理完之后,任务的结整需要等待此节点处理完,使得整个任务被此节点堵塞。</p>
<p>要解决此问题,主要可以分为两大块:</p>
<ul>
<li>一是尽量不shuffle;</li>
<li>二是shuffle之后,在reduce节点上的key分布尽量均匀。</li>
</ul>
</blockquote>
<p>方案总结如下:</p>
<hr>
<p>解决方案:MapJoin,添加随机前缀,使用列桶表</p>
<ul>
<li>mapjoin</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">-- mapjoin配置</div><div class="line">set hive.auto.convert.join = true;</div><div class="line">set hive.mapjoin.smalltable.filesize=25000000;</div></pre></td></tr></table></figure>
<ul>
<li>手动分割成两部分进行join</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">select t1.* </div><div class="line">from t1 join t2 on t1.key=t2.key</div><div class="line"></div><div class="line">拆成以下SQL:</div><div class="line"></div><div class="line">select t1.* </div><div class="line">from t1 join t2 on t1.key=t2.key</div><div class="line">where t1.key=A</div><div class="line">union all </div><div class="line">select t1.*</div><div class="line">from t1 join t2 on t1.key=t2.key</div><div class="line">where t1.key<>A</div></pre></td></tr></table></figure>
<ul>
<li>当小表不是很小,不太方便用mapjoin,大表添加N中随机前缀,小表膨胀N倍数据</li>
<li>使用Skewed Table 或者 List Bucketing Table</li>
</ul>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="https://github.com/DuncanZhou/2019/09/23/Scala笔记/">
<span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="duncan">
<meta itemprop="description" content="">
<meta itemprop="image" content="https://raw.githubusercontent.com/DuncanZhou/images/master/webwxgetmsgimg.jpg">
</span>
<span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Duncan's Blog">
<span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
<img style="display:none;" itemprop="url image" alt="Duncan's Blog" src="">
</span>
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/09/23/Scala笔记/" itemprop="url">
Scala笔记
</a>
</h1>
<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">Veröffentlicht am</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2019-09-23T00:00:00+08:00">
2019-09-23
</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">in</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Learning/" itemprop="url" rel="index">
<span itemprop="name">Learning</span>
</a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="Scala笔记"><a href="#Scala笔记" class="headerlink" title="Scala笔记"></a>Scala笔记</h2><h3 id="1-四种操作符的区别和联系"><a href="#1-四种操作符的区别和联系" class="headerlink" title="1.四种操作符的区别和联系"></a>1.四种操作符的区别和联系</h3><ul>
<li><p>:: 该方法成为cons,表时构造,向队列头部加入元素。x::list表示向list头部加入元素。(列表构造:</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="number">2</span>::<span class="number">1</span>::<span class="number">2</span>::<span class="string">"bar"</span>::<span class="string">"foo"</span> 表示<span class="type">List</span>[<span class="type">Any</span>]= (<span class="number">2</span>,<span class="number">1</span>,<span class="number">2</span>,bar,foo)</div></pre></td></tr></table></figure>
</li>
<li><p>:+和+:表示分别在尾部加入元素和在头部加入元素。</p>
</li>
<li><p>++ 表示连接两个集合</p>
</li>
<li><p>::: 该方法只能用于连接两个list类型的集合</p>
</li>
</ul>
<h3 id="2-日期操作-经常用到,所以记录下"><a href="#2-日期操作-经常用到,所以记录下" class="headerlink" title="2.日期操作(经常用到,所以记录下)"></a>2.日期操作(经常用到,所以记录下)</h3><ul>
<li><p>获取今天0点时间戳</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">val</span> dateFormat = <span class="keyword">new</span> <span class="type">SimpleDateFormat</span>(<span class="string">"yyyy-MM-dd"</span>)</div><div class="line"><span class="keyword">val</span> cur = dateFormat.parse(dateFormat.format(<span class="keyword">new</span> <span class="type">Date</span>())).getTime</div></pre></td></tr></table></figure>
</li>
<li><p>日期格式转时间戳</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">val</span> dateFormat = <span class="keyword">new</span> <span class="type">SimpleDateFormat</span>(<span class="string">"yyyy-MM-dd"</span>)</div><div class="line"><span class="comment">//val dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss")</span></div><div class="line"><span class="keyword">val</span> timestamp = dateFormat.parse(dateFormat.format(<span class="keyword">new</span> <span class="type">Date</span>())).getTime</div></pre></td></tr></table></figure>
</li>
<li><p>时间戳转日期</p>
<figure class="highlight scala"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">val</span> dateFormat = <span class="keyword">new</span> <span class="type">SimpleDateFormat</span>(<span class="string">"yyyy-MM-dd"</span>)</div><div class="line"><span class="keyword">val</span> date = dateFormat.format(<span class="keyword">new</span> <span class="type">Date</span>())</div></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="3-删除目录或文件"><a href="#3-删除目录或文件" class="headerlink" title="3.删除目录或文件"></a>3.删除目录或文件</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> java.io.<span class="type">File</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">dirDel</span></span>(path: <span class="type">File</span>) {</div><div class="line"> <span class="keyword">if</span> (!path.exists())</div><div class="line"> <span class="keyword">return</span></div><div class="line"> <span class="keyword">else</span> <span class="keyword">if</span> (path.isFile()) {</div><div class="line"> path.delete()</div><div class="line"> <span class="keyword">return</span></div><div class="line"> }</div><div class="line"> <span class="keyword">val</span> file: <span class="type">Array</span>[<span class="type">File</span>] = path.listFiles()</div><div class="line"> <span class="keyword">for</span> (d <- file) {</div><div class="line"> dirDel(d)</div><div class="line"> }</div><div class="line"> path.delete()</div><div class="line"> }</div><div class="line"> <span class="keyword">if</span>(<span class="type">Files</span>.exists(<span class="type">Paths</span>.get(path))) {</div><div class="line"> <span class="comment">// 先删除目录里的文件</span></div><div class="line"> dirDel(<span class="type">Paths</span>.get(path).toFile)</div><div class="line"> }</div></pre></td></tr></table></figure>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="https://github.com/DuncanZhou/2019/09/23/Redis笔记/">
<span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="duncan">
<meta itemprop="description" content="">
<meta itemprop="image" content="https://raw.githubusercontent.com/DuncanZhou/images/master/webwxgetmsgimg.jpg">
</span>
<span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Duncan's Blog">
<span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
<img style="display:none;" itemprop="url image" alt="Duncan's Blog" src="">
</span>
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/09/23/Redis笔记/" itemprop="url">
Redis学习
</a>
</h1>
<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">Veröffentlicht am</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2019-09-23T00:00:00+08:00">
2019-09-23
</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">in</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Learning/" itemprop="url" rel="index">
<span itemprop="name">Learning</span>
</a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="Redis"><a href="#Redis" class="headerlink" title="Redis"></a>Redis</h3><h3 id="1、为什么使用Redis数据库"><a href="#1、为什么使用Redis数据库" class="headerlink" title="1、为什么使用Redis数据库"></a>1、为什么使用Redis数据库</h3><ul>
<li><strong>性能极高</strong> – Redis能读的速度是110000次/s,写的速度是81000次/s 。</li>
<li>丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。</li>
<li>原子 – Redis的所有操作都是原子性的,意思就是要么成功执行要么失败完全不执行。单个操作是原子性的。多个操作也支持事务,即原子性,通过MULTI和EXEC指令包起来。</li>
<li>丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性。</li>
</ul>
<h3 id="2-数据类型"><a href="#2-数据类型" class="headerlink" title="2.数据类型"></a>2.数据类型</h3><ul>
<li><p>string</p>
</li>
<li><p>hash:键值对的集合 </p>
<figure class="highlight plain"><figcaption><span>hashset1 key1 val1 key2 val2```</span></figcaption><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">```HGET hashset1 key1</div></pre></td></tr></table></figure>
</li>
<li><p>list</p>
<figure class="highlight plain"><figcaption><span>list1 val1```</span></figcaption><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">* set</div><div class="line"></div><div class="line"> ```sadd set1 val1</div></pre></td></tr></table></figure>
<figure class="highlight plain"><figcaption><span>set1```</span></figcaption><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">* zset(有序集合,带分数)</div><div class="line"></div><div class="line"> ```zadd key score memeber</div></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="3-set和hset区别"><a href="#3-set和hset区别" class="headerlink" title="3.set和hset区别"></a>3.set和hset区别</h3><ul>
<li>set 就是普通的已key-value 方式存储数据,可以设置过期时间。时间复杂度为 O(1)</li>
<li>hset 则是以hash 散列表的形式存储。超时时间只能设置在 大 key 上,单个 filed 则不可以设置超时</li>
</ul>
<hr>
<p>使用场景对比:<strong>set 存储单个大文本非结构化数据,hset 则存储结构化数据</strong>,一个 hash 存储一条数据,一个 filed 则存储 一条数据中的一个属性,value 则是属性对应的值。</p>
<h3 id="4-Scan操作,keys-操作,线上谨慎使用"><a href="#4-Scan操作,keys-操作,线上谨慎使用" class="headerlink" title="4.Scan操作,keys()操作,线上谨慎使用"></a>4.Scan操作,keys()操作,线上谨慎使用</h3><p>scan操作取N条出来进行scan,并保持prefix</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> HashSet<String> <span class="title">getKeysByScan</span><span class="params">(String pattern)</span> </span>{</div><div class="line"> ScanParams scanParams = <span class="keyword">new</span> ScanParams().count(<span class="number">1000</span>).match(pattern);</div><div class="line"> HashSet<String> allKeys = <span class="keyword">new</span> HashSet<>();</div><div class="line"> cluster.getClusterNodes().values().forEach((pool) -> {</div><div class="line"> String cur = ScanParams.SCAN_POINTER_START;</div><div class="line"> <span class="keyword">do</span> {</div><div class="line"> <span class="keyword">try</span> (Jedis jedis = pool.getResource()) {</div><div class="line"> ScanResult<String> scanResult = jedis.scan(cur, scanParams);</div><div class="line"> allKeys.addAll(scanResult.getResult());</div><div class="line"> cur = scanResult.getStringCursor();</div><div class="line"> }</div><div class="line"> } <span class="keyword">while</span> (!cur.equals(ScanParams.SCAN_POINTER_START));</div><div class="line"> });</div><div class="line"> <span class="keyword">return</span> allKeys;</div><div class="line"> }</div></pre></td></tr></table></figure>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="https://github.com/DuncanZhou/2019/09/05/Flink笔记/">
<span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="duncan">
<meta itemprop="description" content="">
<meta itemprop="image" content="https://raw.githubusercontent.com/DuncanZhou/images/master/webwxgetmsgimg.jpg">
</span>
<span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Duncan's Blog">
<span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
<img style="display:none;" itemprop="url image" alt="Duncan's Blog" src="">
</span>
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/09/05/Flink笔记/" itemprop="url">
Flink学习记录
</a>
</h1>
<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">Veröffentlicht am</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2019-09-05T11:10:23+08:00">
2019-09-05
</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">in</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Streaming-Data/" itemprop="url" rel="index">
<span itemprop="name">Streaming Data</span>
</a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="Flink笔记"><a href="#Flink笔记" class="headerlink" title="Flink笔记"></a>Flink笔记</h2><h3 id="1-数据集类型"><a href="#1-数据集类型" class="headerlink" title="1.数据集类型"></a>1.数据集类型</h3><ul>
<li>有界数据集:具有时间边界,在处理过程中数据一定会在某个时间范围内起始和结束。提供DataSet API</li>
<li>无界数据集: 数据从一开始就一直持续产生的。提供DataStream API</li>
</ul>
<h3 id="2-Flink编程接口"><a href="#2-Flink编程接口" class="headerlink" title="2.Flink编程接口"></a>2.Flink编程接口</h3><ul>
<li>Flink SQL</li>
<li>Table API:在内存中的DataSet和DataStream基础上加上Schema信息,将数据类型<strong>抽象成表结构</strong></li>
<li>DataStream API和DataSet API</li>
<li>Stateful Stream Process API</li>
</ul>
<h3 id="3-程序结构"><a href="#3-程序结构" class="headerlink" title="3.程序结构"></a>3.程序结构</h3><ul>
<li><p>设定运行环境:</p>
<ul>
<li><figure class="highlight plain"><figcaption><span>env = StreamExecutionEnvironment.getExecutionEnvironment```</span></figcaption><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">* ```/*设置并行度为5*/val env = StreamExecutionEnvironment.createLocalEnvironment(5)</div></pre></td></tr></table></figure>
</li>
<li><figure class="highlight scala"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">val</span> env = <span class="type">StreamExecutionEnvironment</span>.createRemoteEnvironment(<span class="string">"JobManagerHost"</span>,<span class="number">6021</span>,<span class="number">5</span>,<span class="string">"/user/application.jar"</span>)</div></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>初始化数据:</p>
<p>将外部数据转换成DataStream<t>或者DataSet<t></t></t></p>
</li>
<li><p>执行转换逻辑:</p>
<ul>
<li>复杂的逻辑通过实现MapFunction接口,然后调用map()方法将实现类传入</li>
<li>匿名函数</li>
<li>RichFunction接口</li>
</ul>
</li>
<li><p>分区key指定</p>
</li>
<li><p>根据第一个字段分区,根据第二个字段求和</p>
<p><code>val result = DataStream.keyBy(0).sum(1)</code></p>