-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
577 lines (342 loc) · 22.6 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" >
<title>hongwei's blog</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="description" content="关注ML,NLP,内容推荐">
<meta property="og:type" content="website">
<meta property="og:title" content="hongwei's blog">
<meta property="og:url" content="http://hongwei.org/index.html">
<meta property="og:site_name" content="hongwei's blog">
<meta property="og:description" content="关注ML,NLP,内容推荐">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="hongwei's blog">
<meta name="twitter:description" content="关注ML,NLP,内容推荐">
<link rel="alternative" href="/atom.xml" title="hongwei's blog" type="application/atom+xml">
<link rel="icon" href="/favicon.png">
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<div id="container">
<div class="left-col">
<div class="overlay"></div>
<div class="intrude-less">
<header id="header" class="inner">
<a href="/" class="profilepic">
<img lazy-src="/img/me.jpg" class="js-avatar">
</a>
<hgroup>
<h1 class="header-author"><a href="/">hongwei</a></h1>
</hgroup>
<p class="header-subtitle">关注机器学习,自然语言处理,内容推荐</p>
<div class="switch-btn">
<div class="icon">
<div class="icon-ctn">
<div class="icon-wrap icon-house" data-idx="0">
<div class="birdhouse"></div>
<div class="birdhouse_holes"></div>
</div>
<div class="icon-wrap icon-ribbon hide" data-idx="1">
<div class="ribbon"></div>
</div>
</div>
</div>
<div class="tips-box hide">
<div class="tips-arrow"></div>
<ul class="tips-inner">
<li>Menu</li>
<li>Tags</li>
</ul>
</div>
</div>
<div class="switch-area">
<div class="switch-wrap">
<section class="switch-part switch-part1">
<nav class="header-menu">
<ul>
<li><a href="/">主页</a></li>
<li><a href="/archives">所有文章</a></li>
</ul>
</nav>
<nav class="header-nav">
<div class="social">
<a class="github" target="_blank" href="#" title="github">github</a>
<a class="weibo" target="_blank" href="#" title="weibo">weibo</a>
<a class="rss" target="_blank" href="#" title="rss">rss</a>
<a class="zhihu" target="_blank" href="#" title="zhihu">zhihu</a>
</div>
</nav>
</section>
<section class="switch-part switch-part2">
<div class="widget tagcloud" id="js-tagcloud">
<a href="/tags/TimeCacheMap/" style="font-size: 10px;">TimeCacheMap</a> <a href="/tags/acker/" style="font-size: 10px;">acker</a> <a href="/tags/python/" style="font-size: 20px;">python</a> <a href="/tags/storm/" style="font-size: 20px;">storm</a> <a href="/tags/unicode/" style="font-size: 10px;">unicode</a> <a href="/tags/异或/" style="font-size: 10px;">异或</a> <a href="/tags/算法/" style="font-size: 10px;">算法</a> <a href="/tags/读文件/" style="font-size: 10px;">读文件</a>
</div>
</section>
</div>
</div>
</header>
</div>
</div>
<div class="mid-col">
<nav id="mobile-nav">
<div class="overlay">
<div class="slider-trigger"></div>
<h1 class="header-author js-mobile-header hide">hongwei</h1>
</div>
<div class="intrude-less">
<header id="header" class="inner">
<div class="profilepic">
<img lazy-src="/img/me.jpg" class="js-avatar">
</div>
<hgroup>
<h1 class="header-author">hongwei</h1>
</hgroup>
<p class="header-subtitle">关注机器学习,自然语言处理,内容推荐</p>
<nav class="header-menu">
<ul>
<li><a href="/">主页</a></li>
<li><a href="/archives">所有文章</a></li>
<div class="clearfix"></div>
</ul>
</nav>
<nav class="header-nav">
<div class="social">
<a class="github" target="_blank" href="#" title="github">github</a>
<a class="weibo" target="_blank" href="#" title="weibo">weibo</a>
<a class="rss" target="_blank" href="#" title="rss">rss</a>
<a class="zhihu" target="_blank" href="#" title="zhihu">zhihu</a>
</div>
</nav>
</header>
</div>
</nav>
<div class="body-wrap">
<article id="post-python-unicode-decode-encode" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/python-unicode-decode-encode/" class="article-date">
<time datetime="2016-07-20T01:10:44.000Z" itemprop="datePublished">2016-07-20</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/python-unicode-decode-encode/">python的decode与encode</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>用python处理中文时,总会遇到编码错误。傻傻的分不清什么时候该用decode,什么时候该用encode。</p>
<h1 id="unicode"><a href="#unicode" class="headerlink" title="unicode"></a>unicode</h1><p>首先,明确一个基本的概念:计算机用数字代表每一个字符,unicode就是这样一种通用的编码规范.</p>
<h1 id="utf8"><a href="#utf8" class="headerlink" title="utf8"></a>utf8</h1><p>现在,unicode将每个字符表示成一个唯一的,无歧义的数字了.在计算机中如何存储这些数字呢?</p>
<p>有两种解决方案:</p>
<ul>
<li><p>定长的: 因为unicode最大的编码数字是0x7FFFFFFF,于是为了能用一个长度固定的字节组合来表示所有可能的字符,每个字符需要8个字节.这样解析一个文本就只需要从头开始,把每八个字节转换成一个数字,查一下这个数字对应是什么字符.</p>
</li>
<li><p>可变长的:显然,定长的虽然解析起来简单,但是有很大的空间浪费问题.编码较小的字符是完全可以用较少的字节数来存储的,不需要为了能表示可能的编码极大的字符,就让每个字符都用最大的字节数来存储.</p>
</li>
</ul>
<p>UTF-8就是这样一种字节可变长的存储方案.一个重要的问题就是,如何无歧义的知道连续几个字节存储了一个字符呢?</p>
<p>UTF-8的编码规则很简单,只有二条:</p>
<ul>
<li>对于单字节的符号,字节的第一位设为0,后面7位为这个符号的unicode码。因此对于英语字母,UTF-8编码和ASCII码是相同的。</li>
<li>对于n字节的符号(n>1),第一个字节的前n位都设为1,第n+1位设为0,后面字节的前两位一律设为10。这些字节中剩下的没有提及的二进制位,全部为这个符号的unicode码。</li>
</ul>
<h1 id="decode解码"><a href="#decode解码" class="headerlink" title="decode解码"></a>decode解码</h1><p>因为有很多编码方案,于是用python处理非ASCII码表示外的字符(如中文字符)时,需要告诉python我们要处理的文本用的是哪个编码方案.这样python读取文本时,才知道每个字符的编码是定长的还是变长的,如果是定长的,用了几个字节;如果是变长的,怎么识别哪几个字节代表了一个字符.</p>
<p>“中文”.decode(“utf8”)</p>
<h1 id="encode编码"><a href="#encode编码" class="headerlink" title="encode编码"></a>encode编码</h1><p>同样的,用python写文件时,也需要告诉python,我们希望用哪个编码方案存储字符.</p>
<p>u”中文”.encode(“utf8”)</p>
</div>
<div class="article-info article-info-index">
<div class="article-tag tagcloud">
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/python/">python</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/unicode/">unicode</a></li></ul>
</div>
<div class="article-category tagcloud">
<a class="article-category-link" href="/categories/编程语言/">编程语言</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-python-read-file-efficiently" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/python-read-file-efficiently/" class="article-date">
<time datetime="2016-07-20T00:53:49.000Z" itemprop="datePublished">2016-07-20</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/python-read-file-efficiently/">用Python高效的读取文件</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>参考:<a href="http://stackoverflow.com/questions/8009882/how-to-read-large-file-line-by-line-in-python" target="_blank" rel="external">http://stackoverflow.com/questions/8009882/how-to-read-large-file-line-by-line-in-python</a></p>
<p>用python读文件时,python提供了三个基本的函数: read, readline, readlines.</p>
<p>通常,处理普通大小的文件时,我会无脑的使用 readlines 读文件. 实际上,调用readlines会一次加载全部文件内容到内存中,并按行保存为列表. 因此,如果文件大到内存放不下时,readlines就不适用了.</p>
<p>为此,我选择用readline逐行的将文本加载到内存,代码如下:</p>
<figure class="highlight python"><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"><span class="keyword">while</span> <span class="keyword">True</span>: </div><div class="line"> line = f.readline()</div><div class="line"> <span class="keyword">if</span> <span class="keyword">not</span> line : <span class="keyword">break</span></div><div class="line"> do something <span class="keyword">with</span> line</div></pre></td></tr></table></figure>
<p>这样做虽然可以处理大文本了,但是读取效率会被磁盘io的速度拖累.</p>
<p>进而,也许我们可以使用read(size)方法一次加载size长度的数据到内存,但是我们不得不手动将读取到的数据切分成行.并且要处理某一行被截断的情况.</p>
<p>其实,依靠python强大的功能,我们可以用如下的更优雅的方法:</p>
<figure class="highlight python"><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">with</span> open(...) <span class="keyword">as</span> f:</div><div class="line"> <span class="keyword">for</span> line <span class="keyword">in</span> f:</div><div class="line"> <do something <span class="keyword">with</span> line></div></pre></td></tr></table></figure>
<p>python自动帮我们处理io缓存,内存管理等问题.经过我实践操作,效率至少比用readline高一倍以上.</p>
</div>
<div class="article-info article-info-index">
<div class="article-tag tagcloud">
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/python/">python</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/读文件/">读文件</a></li></ul>
</div>
<div class="article-category tagcloud">
<a class="article-category-link" href="/categories/编程语言/">编程语言</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-storm-SingleJoin" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/storm-SingleJoin/" class="article-date">
<time datetime="2016-07-20T00:43:10.000Z" itemprop="datePublished">2016-07-20</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/storm-SingleJoin/">storm实例分析小结—SingleJoin</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>流聚合是storm的一个常见模式,storm官方给出了相关的实例:<a href="https://github.com/apache/storm/blob/master/examples/storm-starter/src/jvm/storm/starter/SingleJoinExample.java" target="_blank" rel="external">SingleJoinExample</a>。</p>
<h2 id="锚点anchor"><a href="#锚点anchor" class="headerlink" title="锚点anchor"></a>锚点anchor</h2><p>在网上找到了一个对该实例讲解的博文:<a href="http://www.cnblogs.com/panfeng412/archive/2012/06/04/storm-common-patterns-of-stream-join.html" target="_blank" rel="external">Storm常见模式——流聚合</a>。讲解的还算清楚,不过最后有一个错误:</p>
<blockquote>
<p>最后通过_collector将parts中存放的tuple和聚合后的输出结果发射出去,并ack这些tuple已经处理成功。<br>_collector.emit(new ArrayList<tuple>(parts.values()), joinResult);</tuple></p>
</blockquote>
<p>问题在于,<code>parts</code>中存放的<code>tuple:new ArrayList<Tuple>(parts.values())</code>不是被发射出去了,而是作为<code>joinResult</code>设置为锚点(<code>anchor</code>)了。锚点的概念作用见:<a href="http://storm.apache.org/releases/current/Guaranteeing-message-processing.html" target="_blank" rel="external">Guaranteeing-message-processing</a>。</p>
<h2 id="TimeCacheMap"><a href="#TimeCacheMap" class="headerlink" title="TimeCacheMap"></a>TimeCacheMap</h2><p>在这个实例中还提到了storm中一个很高效的数据结构:<code>TimeCacheMap</code>。如果你需要一个带有过期删除功能的Map作为缓存数据结构,TimeCacheMap是一个很高效的解决方案。这篇博文<a href="http://www.cnblogs.com/mushroom/p/4805413.html" target="_blank" rel="external">探索c#之storm的TimeCacheMap</a>对TimeCacheMap讲解的非常赞!相比来说,徐明明的<a href="http://xumingming.sinaapp.com/395/twitter-storm-code-analysis-timecahcemap/" target="_blank" rel="external">Twitter Storm源代码分析之TimeCacheMap</a>讲解就逊色了些,他对该数据结构的效率的解释有误,讲解的例子也不够到位:</p>
<blockquote>
<p>那为什么说时间是expirationSecs到expirationSecs * (1 + 1 / (numBuckets-1))之间呢?因为线程调度的不确定性。</p>
</blockquote>
<p>这个解释有点想当然了。我觉得延迟是由于数据随机到达,而删除操作是定时的.<br>TimeCacheMap优点是删除时间是O(1)的;缺点是对于大部分数据,删除操作会有延迟。</p>
</div>
<div class="article-info article-info-index">
<div class="article-tag tagcloud">
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/TimeCacheMap/">TimeCacheMap</a></li></ul>
</div>
<div class="article-category tagcloud">
<a class="article-category-link" href="/categories/分布式/">分布式</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-storm-topology-jar" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/storm-topology-jar/" class="article-date">
<time datetime="2016-07-18T02:47:22.000Z" itemprop="datePublished">2016-07-18</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/storm-topology-jar/">在storm上提交jar包不生效的问题</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>前一阵连续遇到好几次在Storm上提交Topology不生效的问题。</p>
<h2 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h2><p>问题是这样的:<br>首先将具有<code>A.java</code>的jar包<code>1.jar</code>传到<code>storm/lib</code>下,用<code>storm jar lib/1.jar A</code>命令提交Topology。<br>不久后更新了<code>A.java</code>文件里的代码,并将工程重新打包成jar包<code>2.jar</code>,同样传到<code>storm/lib</code>下(并没有删掉<code>1.jar</code>),此时kill掉旧Topology后,用<code>storm jar lib/2.jar A</code>命令提交Topology。这时发现运行的Topology仍然是更新前的,更新的代码并没有生效。<br>这个问题困惑了我好久,我明明指定了jar包名了啊!最后突然想起:storm/lib目录作为storm的classpath,当storm在调用任何需要的类时,会先在storm/lib下查找,因此便出现这样的情况:我本意想运行lib/2.jar中的类A,但是storm实际会找到lib/1.jar中的类A。</p>
<h2 id="解决方法"><a href="#解决方法" class="headerlink" title="解决方法"></a>解决方法</h2><p>因此,我的解决方法是:把打好的jar包放到新目录myTopology下。</p>
</div>
<div class="article-info article-info-index">
<div class="article-tag tagcloud">
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/storm/">storm</a></li></ul>
</div>
<div class="article-category tagcloud">
<a class="article-category-link" href="/categories/分布式/">分布式</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</article>
<article id="post-storm-acker-xor" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-meta">
<a href="/storm-acker-xor/" class="article-date">
<time datetime="2015-08-09T00:59:10.000Z" itemprop="datePublished">2015-08-09</time>
</a>
</div>
<div class="article-inner">
<input type="hidden" class="isFancy" />
<header class="article-header">
<h1 itemprop="name">
<a class="article-title" href="/storm-acker-xor/">学storm的acker机制有感</a>
</h1>
</header>
<div class="article-entry" itemprop="articleBody">
<p>storm是一个分布式实时计算系统。它最大的一个特点就是可靠性,即:保证每一个节点至少被处理一次。</p>
<h2 id="acker机制"><a href="#acker机制" class="headerlink" title="acker机制"></a>acker机制</h2><p>在可靠的处理过程中,storm使用acker机制维护着从spout发出的Tuple节点为根的整个Tuple树。在任何处理节点发射出的tuple都要与其父节点建立锚点,当一个tuple处理完毕时也要被确认。基于这样有保证的处理,storm需要判断在超时时间内(默认是30秒),tuple树的节点是否都被确认了。如果没有,则要从spout出发,重新发射根节点tuple。 </p>
<h2 id="xor操作"><a href="#xor操作" class="headerlink" title="xor操作"></a>xor操作</h2><p>比较有意思的是,storm的acker机制利用了非常高效的方法来跟踪整个tuple树的确认情况。首先,storm会为每个tuple分配一个64位的数值型id;在此基础之上,当一个tuple树每发射一个tuple时,storm会异或这个tuple id;每个tuple被确认后,同样异或下这个tuple的id值。当发现异或的结果为零时,storm就认为整个tuple树的所有tuple都被确认了;否则,就从根节点进行重发操作,从而保证了可靠性。<br>学习storm的acker机制后,我想到了校招时遇到的一个算法题:如何判断一个整数数组中的所有元素均是成对出现的。那时看了答案才知道怎么解这道题:就是将数组中所有元素都异或到一起,看结果就否为0。因为一个数和自己异或的值为0,因此如果结果为0,那么就说明数组中的所有数都是成对出现的了。当时觉得解法很巧妙,很高效;同时也觉得然并卵,会这种算法题有什么意义呢?现在才恍然大悟,原来这些算法题都是从实际工作中抽象、总结出来的精华啊!<br>以后要多做算法题!</p>
</div>
<div class="article-info article-info-index">
<div class="article-tag tagcloud">
<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/acker/">acker</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/storm/">storm</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/异或/">异或</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/算法/">算法</a></li></ul>
</div>
<div class="article-category tagcloud">
<a class="article-category-link" href="/categories/分布式/">分布式</a>
</div>
<div class="clearfix"></div>
</div>
</div>
</article>
</div>
<footer id="footer">
<div class="outer">
<div id="footer-info">
<div class="footer-left">
© 2016 hongwei
</div>
<div class="footer-right">
<a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/litten/hexo-theme-yilia" target="_blank">Yilia</a> by Litten
</div>
</div>
</div>
</footer>
</div>
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
<script>
var yiliaConfig = {
fancybox: true,
mathjax: true,
animate: true,
isHome: true,
isPost: false,
isArchive: false,
isTag: false,
isCategory: false,
open_in_new: false
}
</script>
<script src="http://7.url.cn/edu/jslib/comb/require-2.1.6,jquery-1.9.1.min.js"></script>
<script src="/js/main.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
processEscapes: true,
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
}
});
MathJax.Hub.Queue(function() {
var all = MathJax.Hub.getAllJax(), i;
for(i=0; i < all.length; i += 1) {
all[i].SourceElement().parentNode.className += ' has-jax';
}
});
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
</div>
</body>
</html>