-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongodb-data-insert-trap.html
executable file
·400 lines (360 loc) · 18.1 KB
/
mongodb-data-insert-trap.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
<!DOCTYPE html>
<html lang="en" prefix="og: http://ogp.me/ns# fb: https://www.facebook.com/2008/fbml">
<head>
<title>MongoDb数据导入踩坑记录 - TechLoveDeath</title>
<!-- Using the latest rendering mode for IE -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://brycemeng.com/images/favicon.png" rel="icon">
<link rel="canonical" href="https://brycemeng.com/mongodb-data-insert-trap.html">
<meta name="author" content="Bryce" />
<meta name="keywords" content="Quant,Mongodb" />
<meta name="description" content="MongoDb 总共14年的期货tick数据导入踩坑历程" />
<meta property="og:site_name" content="TechLoveDeath" />
<meta property="og:type" content="article"/>
<meta property="og:title" content="MongoDb数据导入踩坑记录"/>
<meta property="og:url" content="https://brycemeng.com/mongodb-data-insert-trap.html"/>
<meta property="og:description" content="MongoDb 总共14年的期货tick数据导入踩坑历程"/>
<meta property="article:published_time" content="2017-11-17" />
<meta property="article:section" content="Quant" />
<meta property="article:tag" content="Quant" />
<meta property="article:tag" content="Mongodb" />
<meta property="article:author" content="Bryce" />
<!-- Bootstrap -->
<link rel="stylesheet" href="https://brycemeng.com/theme/css/bootstrap.min.css" type="text/css"/>
<link href="https://brycemeng.com/theme/css/font-awesome.min.css" rel="stylesheet">
<link href="https://brycemeng.com/theme/css/pygments/native.css" rel="stylesheet">
<link rel="stylesheet" href="https://brycemeng.com/theme/css/style.css" type="text/css"/>
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="https://brycemeng.com/" class="navbar-brand">
<img class="img-responsive pull-left gap-right" src="https://brycemeng.com/images/favicon.png" width="24"/> TechLoveDeath </a>
</div>
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li >
<a href="https://brycemeng.com/category/android.html">Android</a>
</li>
<li >
<a href="https://brycemeng.com/category/misc.html">Misc</a>
</li>
<li >
<a href="https://brycemeng.com/category/music.html">Music</a>
</li>
<li >
<a href="https://brycemeng.com/category/programming.html">Programming</a>
</li>
<li class="active">
<a href="https://brycemeng.com/category/quant.html">Quant</a>
</li>
<li >
<a href="https://brycemeng.com/category/tech.html">Tech</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
</div> <!-- /.navbar -->
<!-- Banner -->
<!-- End Banner -->
<!-- Content Container -->
<div class="container">
<div class="row">
<div class="col-sm-9">
<section id="content">
<article>
<header class="page-header">
<h1>
<a href="https://brycemeng.com/mongodb-data-insert-trap.html"
rel="bookmark"
title="Permalink to MongoDb数据导入踩坑记录">
MongoDb数据导入踩坑记录
</a>
</h1>
</header>
<div class="entry-content">
<div class="panel">
<div class="panel-body">
<footer class="post-info">
<span class="label label-default">Date</span>
<span class="published">
<i class="fa fa-calendar"></i><time datetime="2017-11-17T16:14:00+08:00"> Fri 17 November 2017</time>
</span>
<span class="label label-default">Tags</span>
<a href="https://brycemeng.com/tag/quant.html">Quant</a>
/
<a href="https://brycemeng.com/tag/mongodb.html">Mongodb</a>
</footer><!-- /.post-info --> </div>
</div>
<ul>
<li>优化前:<strong>1天导入1个月左右的数据</strong></li>
<li>优化后:<strong>30分钟内导入1年的数据</strong></li>
</ul>
<h2>两个问题:</h2>
<ol>
<li>mongodb插入大数据的正确方法</li>
<li>mongodb如何把数据切换到外置SSD上面</li>
</ol>
<h2>插入方式</h2>
<p>大概在两个星期前,买了03年到现在的商品期货的tick数据,不过只有15年和以后的才是ctp格式,所以我还需要写两个导入程序,我是先写的15年前的,其实我走错了一步,应该先写15年后的,这样和我现在的数据(17年)能连接起来,不然我2004年的导好了,是用不了的。不过当时我确实没想到,我用错了数据插入方法后,速度会如此的慢,也是有点超出我的想像。</p>
<p>我先写的老数据(15年前)的导入,其实程序不复杂,就是把tick里面的细节处理好就行,然后在插入时我用了</p>
<div class="highlight"><pre><span></span><code><span class="n">flt</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'datetime'</span><span class="p">:</span> <span class="n">tick_data</span><span class="o">.</span><span class="n">datetime</span><span class="p">}</span>
<span class="n">DES_DB</span><span class="p">[</span><span class="n">collection_name</span><span class="p">]</span><span class="o">.</span><span class="n">update_one</span><span class="p">(</span><span class="n">flt</span><span class="p">,</span> <span class="p">{</span><span class="s1">'$set'</span><span class="p">:</span> <span class="n">tick_data</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">},</span> <span class="n">upsert</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</code></pre></div>
<p>这句话是从vnpy老版本的数据收集器里面扒下来的,当时的场景是收集实时数据,数据量不是很大,这样写是没有问题的,还可以防止日期重复的数据产生。但是我现在数据量压缩包都是50个G,所以还是不能直接用,我昨天实在受不了之后其实是先切换的SSD,发现还是慢,所以才想到了是不是插入方式的问题。</p>
<p>后来查了一些资料,我改用了insert_many,10000条一轮,但是该函数少了一个check主键datetime的问题,不过我在导入之前已经判断了相邻datetime重复的问题,所以出现datetime重复的概率应该不是很大。</p>
<p>所以现在是30分钟之内可以成功导入1年的数据,棒棒哒</p>
<h2>SSD切换</h2>
<p>再来讲讲我切换SSD的过程,也是一个坑。</p>
<p>其实原理非常简单,停止mongod服务,把/var/lib/mongodb下的文件移动到SSD上,再把/etc/mongodb.conf里面的dbPath对应的改掉就行。</p>
<p>但是我今天试了好几个小时都没启动起来,错误都是对目录没有访问权限,但是我明明chmod 777过啊,我擦。后来我用mongodb的用户登陆了bash,在里面做实验,确实没有权限,没有深究,估计是系统对这些服务类的用户限制比较大,还有一个原因应该是我的是USB SSD,所以是移动硬盘,我在home文件夹下是成功了的。</p>
<p>知道原因之后也很简单,把mongod的启动用户改成其他用户就行,我比较暴力,直接改成了root,反正把tick数据导完了再恢复就行。</p>
<p>遇到不科学的事情后,不要急,慢慢查原因,肯定能解决</p>
</div>
<!-- /.entry-content -->
<section class="well" id="related-posts">
<h4>Related Posts:</h4>
<ul>
<li><a href="https://brycemeng.com/some_exp_of_two_arg_strategy.html">最近的一些经验总结</a></li>
<li><a href="https://brycemeng.com/AdvFinML-Standard-Bars.html">AdvFinML - Standard Bars</a></li>
<li><a href="https://brycemeng.com/something-about-financial-analysis.html">财务分析相关</a></li>
<li><a href="https://brycemeng.com/hidden-markov-model.html">HMM 稳形马尔科夫</a></li>
<li><a href="https://brycemeng.com/numpy-version-pandas-ewm-mean-var.html">numpy pandas ewm mean/var</a></li>
</ul>
</section>
<hr/>
<section class="comments" id="comments">
<h2>Comments</h2>
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'brycemeng'; // required: replace example with your forum shortname
var disqus_config = function () {
this.language = "en";
this.page.identifier = '2017-11-17-mongodb-data-insert-trap';
this.page.url = 'https://brycemeng.com/mongodb-data-insert-trap.html';
};
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by
Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</section>
</article>
</section>
</div>
<div class="col-sm-3" id="sidebar">
<aside>
<!-- Sidebar -->
<section class="well well-sm">
<ul class="list-group list-group-flush">
<!-- Sidebar/Social -->
<li class="list-group-item">
<h4><i class="fa fa-home fa-lg"></i><span class="icon-label">Social</span></h4>
<ul class="list-group" id="social">
<li class="list-group-item"><a href="https://github.com/BryceMeng"><i class="fa fa-github-square fa-lg"></i> Github</a></li>
</ul>
</li>
<!-- End Sidebar/Social -->
<!-- Sidebar/Tag Cloud -->
<li class="list-group-item">
<a href="https://brycemeng.com/"><h4><i class="fa fa-tags fa-lg"></i><span class="icon-label">Tags</span></h4></a>
<ul class="list-group list-inline tagcloud" id="tags">
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/101alpha.html">101Alpha</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/adb.html">adb</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/advfinml.html">AdvFinML</a>
</li>
<li class="list-group-item tag-2">
<a href="https://brycemeng.com/tag/android.html">Android</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/async.html">async</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/cryptocurrency.html">Cryptocurrency</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/elite3.html">Elite3</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/english.html">English</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/fifthcircle.html">fifthcircle</a>
</li>
<li class="list-group-item tag-2">
<a href="https://brycemeng.com/tag/fun.html">Fun</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/futures.html">Futures</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/genetic-algorithm.html">Genetic Algorithm</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/hidden-markov.html">Hidden Markov</a>
</li>
<li class="list-group-item tag-2">
<a href="https://brycemeng.com/tag/hifi.html">Hifi</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/javascript.html">javascript</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/kalman-filter.html">Kalman Filter</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/life.html">Life</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/math.html">Math</a>
</li>
<li class="list-group-item tag-1">
<a href="https://brycemeng.com/tag/misc.html">Misc</a>
</li>
<li class="list-group-item tag-2">
<a href="https://brycemeng.com/tag/ml.html">ML</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/mongodb.html">Mongodb</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/monkey.html">monkey</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/multithread.html">multithread</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/music.html">Music</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/numpy.html">Numpy</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/optimization.html">Optimization</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/pandas.html">Pandas</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/programming.html">programming</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/python.html">Python</a>
</li>
<li class="list-group-item tag-1">
<a href="https://brycemeng.com/tag/quant.html">Quant</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/router.html">Router</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/stats.html">Stats</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/stock.html">Stock</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/system.html">System</a>
</li>
<li class="list-group-item tag-2">
<a href="https://brycemeng.com/tag/tech.html">Tech</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/tech-indicator.html">Tech Indicator</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/time-series.html">Time Series</a>
</li>
<li class="list-group-item tag-3">
<a href="https://brycemeng.com/tag/translation.html">Translation</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/trap.html">Trap</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/ubuntu.html">Ubuntu</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/vm.html">VM</a>
</li>
<li class="list-group-item tag-4">
<a href="https://brycemeng.com/tag/wu-du-quan.html">五度圈</a>
</li>
</ul>
</li>
<!-- End Sidebar/Tag Cloud -->
<!-- Sidebar/Links -->
<li class="list-group-item">
<h4><i class="fa fa-external-link-square fa-lg"></i><span class="icon-label">Links</span></h4>
<ul class="list-group" id="links">
<li class="list-group-item">
<a href="http://makerchen.com" target="_blank">MakerChen</a>
</li>
</ul>
</li>
<!-- End Sidebar/Links -->
</ul>
</section>
<!-- End Sidebar --> </aside>
</div>
</div>
</div>
<!-- End Content Container -->
<footer>
<div class="container">
<hr>
<div class="row">
<div class="col-xs-10">© 2024 Bryce
· Powered by <a href="https://github.com/getpelican/pelican-themes/tree/master/pelican-bootstrap3" target="_blank">pelican-bootstrap3</a>,
<a href="http://docs.getpelican.com/" target="_blank">Pelican</a>,
<a href="http://getbootstrap.com" target="_blank">Bootstrap</a> </div>
<div class="col-xs-2"><p class="pull-right"><i class="fa fa-arrow-up"></i> <a href="#">Back to top</a></p></div>
</div>
</div>
</footer>
<script src="https://brycemeng.com/theme/js/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://brycemeng.com/theme/js/bootstrap.min.js"></script>
<!-- Enable responsive features in IE8 with Respond.js (https://github.com/scottjehl/Respond) -->
<script src="https://brycemeng.com/theme/js/respond.min.js"></script>
<!-- Disqus -->
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'brycemeng'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var s = document.createElement('script');
s.async = true;
s.type = 'text/javascript';
s.src = '//' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
<!-- End Disqus Code -->
</body>
</html>