-
Notifications
You must be signed in to change notification settings - Fork 11
/
easyNSQ.dos
715 lines (643 loc) · 30 KB
/
easyNSQ.dos
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
module DolphinDBModules::easyNSQ
/* *
* @ Function name: easyNsqLog
* @ Brief: print message to frontend and to log file
* @ Param:
* funcName: the name of function that sends the message
* msg: the message to print
* isError: whether it is an error msg
*/
def easyNsqLog(funcName, msg, isError=false) {
if(!isError) {
formattedMsg = stringFormat("[DolphinDBModules::easyNSQ] %W: %W", funcName, msg)
} else {
formattedMsg = stringFormat("[ERROR][DolphinDBModules::easyNSQ] %W: %W", funcName, msg)
}
print(formattedMsg)
writeLog(formattedMsg)
}
/* *
* @ Function name: validateDataSourceParam
* @ Brief: check whether the dataSource argument is valid and standardize it
* @ Param:
* dataSource: represents the nsq data type of interest
* @ Return: NULL if the dataSource argument is invalid,
* otherwise return a string scalar which is `orders, `trade or `snapshot
*/
def validateDataSourceParam(dataSource) {
try {
if(dataSource.size() == 1 && !isVoid(dataSource)) {
ds = dataSource.lower()
if(ds in [`orders, `trade, `snapshot]) {
return ds
}
}
} catch (ex) {
easyNsqLog(`validateDataSourceParam, string(ex[0]) + ":" + string(ex[1]), true)
return NULL
}
easyNsqLog(`validateDataSourceParam, "Invalid dataSource argument", true)
return NULL
}
/* *
* @ Function name: validateMarketsParam
* @ Brief: check whether the markets argument is valid and standardize it
* @ Param:
* markets: represents the markets of interest
* @ Return: NULL if the markets argument is invalid,
* otherwise return a string vector which is [`sz], [`sh] or [`sz, `sh]
*/
def validateMarketsParam(markets) {
try {
if(markets.size() in 1:2) {
marketList = markets.lower()
if(all(marketList in ["sz", "sh"])) {
if(marketList.size() == 1 || marketList[0] != marketList[1])
return [marketList].flatten()
}
}
} catch (ex) {
easyNsqLog(`validateMarketsParam, string(ex[0]) + ":" + string(ex[1]), true)
return NULL
}
easyNsqLog(`validateMarketsParam, "Invalid markets argument", true)
return NULL
}
/* *
* @ Function name: validateStreamTableNamesParam
* @ Brief: check whether the streamTableNames argument is valid and standardize it
* @ Param:
* streamTableNames: the name of stream tables where the real-time nsq data is going to store
* @ Return: NULL if the streamTableNames argument is invalid,
* nsqStickOrderStream/nsqStockTradeStream/nsqStockSnapshotStream if streamTableNames=NULL and merge=true,
* nsqStockOrdersSHStream/nsqStockOrdersSZStream/nsqStockTradeSHStream/nsqStockTradeSZStream/nsqStockSnapshotSHStream/nsqStockSnapshotSZStream
* if streamTableNames=NULL and merge=false, otherwise return user intput without modification
*/
def validateStreamTableNamesParam(ds, marketList, merge, streamTableNames) {
try {
if(isVoid(streamTableNames)) {
ds_capitalized = upper(ds[0]) + ds[1:]
if(merge && marketList.size() == 2) {
return ["nsqStock" + ds_capitalized + "Stream"]
} else if(!merge) {
return ["nsqStock" + ds_capitalized + upper(marketList) + "Stream"].flatten()
}
} else if(streamTableNames.size() in 1:2) {
if((merge == true && streamTableNames.size() == 1) ||
(merge == false && streamTableNames.size() == marketList.size())) {
return [streamTableNames].flatten()
}
}
} catch (ex) {
easyNsqLog(`validateStreamTableNamesParam, string(ex[0]) + ":" + string(ex[1]), true)
return NULL
}
easyNsqLog(`validateStreamTableNamesParam, "Invalid streamTableNames argument", true)
return NULL
}
/* *
* @ Function name: validateDbPathParam
* @ Brief: check whether the dbPath argument is valid and standardize it
* @ Param:
* dbPath: the path of database where the real-time nsq data is going to store if saveToDfs option is on
* @ Return: NULL if the dbPath argument is invalid,
* dfs://nsqStockOrders, dfs://nsqStockTrade or dfs://nsqStockSnapshot if dbPath=NULL,
* otherwise return user intput without modification
*/
def validateDbPathParam(ds, dbPath) {
try {
if(dbPath.size() == 1) {
if(isVoid(dbPath)) {
ds_capitalized = upper(ds[0]) + ds[1:]
return "dfs://nsqStock" + ds_capitalized
} else if(regexFind(dbPath, "^dfs://") == 0) {
return dbPath
}
}
} catch (ex) {
easyNsqLog(`validateDbPathParam, string(ex[0]) + ":" + string(ex[1]), true)
return NULL
}
easyNsqLog(`validateDbPathParam, "Invalid dbPath argument", true)
return NULL
}
/* *
* @ Function name: validateTableNamesParam
* @ Brief: check whether the tableNames argument is valid and standardize it
* @ Param:
* tableNames: the name of dfs tables where the real-time nsq data is going to store if saveToDfs option is on
* @ Return: NULL if the tableNames argument is invalid,
* orders/trade/snapshot if tableNames=NULL and merge=true,
* ordersSH/ordersSZ/tradeSH/tradeSZ/snapshotSH/snapshotSZ if tableNames=NULL and merge=false,
* otherwise return user intput without modification
*/
def validateTableNamesParam(ds, marketList, merge, tableNames) {
try {
if(isVoid(tableNames)) {
if(merge) {
return [ds]
} else {
return [ds + upper(marketList)].flatten()
}
} else if(tableNames.size() in 1:2) {
if((merge == true && tableNames.size() == 1) ||
(merge == false && tableNames.size() == marketList.size())) {
return [tableNames].flatten()
}
}
} catch (ex) {
easyNsqLog(`validateTableNamesParam, string(ex[0]) + ":" + string(ex[1]), true)
return NULL
}
easyNsqLog(`validateTableNamesParam, "Invalid tableNames argument", true)
return NULL
}
def subscribeNsqOrders(markets, merge, saveToDfs, streamTableNames, dbPath, tableNames) {
try {
ordersSchema = nsq::getSchema(`orders)
// sz,sh合并存储
if(merge) {
// 持久化共享流表
enableTableShareAndPersistence(
table=streamTable(1:0, ordersSchema.name, ordersSchema.type),
tableName=streamTableNames[0],
cacheSize=500000,
preCache=100000
)
easyNsqLog(`subscribeNsqOrders, "initialize shared and persistence streamTable "+streamTableNames[0])
if(saveToDfs) {
// 分布式表
if(existsDatabase(dbPath)) {
easyNsqLog(`subscribeNsqOrders, "find existing database "+ dbPath)
} else {
dbDate = database("", VALUE, 2023.01.01..2023.12.01)
dbSymbol = database("", HASH, [SYMBOL, 50])
database(dbPath, COMPO, [dbDate, dbSymbol], , "TSDB")
easyNsqLog(`subscribeNsqOrders, "create database "+ dbPath)
}
if(existsTable(dbPath, tableNames[0])) {
easyNsqLog(`subscribeNsqOrders, "find existing dfsTable ("+ dbPath + "," + tableNames[0]+")")
} else {
createPartitionedTable(
dbHandle=database(dbPath),
table=table(1:0, ordersSchema.name, ordersSchema.type),
tableName=tableNames[0],
partitionColumns=`TradeDate`InstrumentID,
sortColumns=`InstrumentID`TransactTime,
keepDuplicates=ALL
)
easyNsqLog(`subscribeNsqOrders, "create dfsTable ("+ dbPath + "," + tableNames[0]+")")
}
// 流表入库的订阅
subscribeTable(
tableName=streamTableNames[0],
actionName="easyNSQ_saveToDfsTable",
offset=-1,
handler=loadTable(dbPath, tableNames[0]),
msgAsTable=true,
batchSize=10000,
throttle=1,
reconnect = true
)
easyNsqLog(`subscribeNsqOrders, stringFormat("dfs table (%W,%W) is listening to stream table %W", dbPath, tableNames[0], streamTableNames[0]))
}
// nsq订阅实时数据
nsq::subscribe(`orders, `sz, objByName(streamTableNames[0], true))
nsq::subscribe(`orders, `sh, objByName(streamTableNames[0], true))
easyNsqLog(`subscribeNsqOrders, "successfully subscribe nsq sh and sz orders data")
} else {
// sz,sh分开存储
for(i in 0:markets.size()) {
// 持久化共享流表
enableTableShareAndPersistence(
table=streamTable(1:0, ordersSchema.name, ordersSchema.type),
tableName=streamTableNames[i],
cacheSize=500000,
preCache=100000
)
easyNsqLog(`subscribeNsqOrders, "initialize shared and persistence streamTable "+streamTableNames[i])
}
if(saveToDfs) {
// 分布式表
if(existsDatabase(dbPath)) {
easyNsqLog(`subscribeNsqOrders, "find existing database "+ dbPath)
} else {
dbDate = database("", VALUE, 2023.01.01..2023.12.01)
dbSymbol = database("", HASH, [SYMBOL, 25])
database(dbPath, COMPO, [dbDate, dbSymbol], , "TSDB")
easyNsqLog(`subscribeNsqOrders, "create database "+ dbPath)
}
for(i in 0:markets.size()) {
if(existsTable(dbPath, tableNames[i])) {
easyNsqLog(`subscribeNsqOrders, "find existing dfsTable ("+ dbPath + "," + tableNames[i]+")")
} else {
createPartitionedTable(
dbHandle=database(dbPath),
table=table(1:0, ordersSchema.name, ordersSchema.type),
tableName=tableNames[i],
partitionColumns=`TradeDate`InstrumentID,
sortColumns=`InstrumentID`TransactTime,
keepDuplicates=ALL
)
easyNsqLog(`subscribeNsqOrders, "create dfsTable ("+ dbPath + "," + tableNames[i]+")")
}
// 流表入库的订阅
subscribeTable(
tableName=streamTableNames[i],
actionName="easyNSQ_saveToDfsTable",
offset=-1,
handler=loadTable(dbPath, tableNames[i]),
msgAsTable=true,
batchSize=10000,
throttle=1,
reconnect = true
)
easyNsqLog(`subscribeNsqOrders, stringFormat("dfs table (%W,%W) is listening to stream table %W", dbPath, tableNames[i], streamTableNames[i]))
}
}
for(i in 0:markets.size()) {
// nsq订阅实时数据
nsq::subscribe(`orders, markets[i], objByName(streamTableNames[i], true))
easyNsqLog(`subscribeNsqOrders, "successfully subscribe nsq " + markets[i] + " orders data")
}
}
} catch(ex) {
easyNsqLog("subscribeNsqOrders", string(ex[0])+":"+string(ex[1]), true)
return NULL
}
if(saveToDfs) {
return streamTableNames, dbPath, tableNames
} else {
return streamTableNames
}
}
def subscribeNsqTrade(markets, merge, saveToDfs, streamTableNames, dbPath, tableNames) {
try {
tradeSchema = nsq::getSchema(`trade)
// sz,sh合并存储
if(merge) {
// 持久化共享流表
enableTableShareAndPersistence(
table=streamTable(1:0, tradeSchema.name, tradeSchema.type),
tableName=streamTableNames[0],
cacheSize=500000,
preCache=100000
)
easyNsqLog(`subscribeNsqTrade, "initialize shared and persistence streamTable "+streamTableNames[0])
if(saveToDfs) {
// 分布式表
if(existsDatabase(dbPath)) {
easyNsqLog(`subscribeNsqTrade, "find existing database "+ dbPath)
} else {
dbDate = database("", VALUE, 2023.01.01..2023.12.01)
dbSymbol = database("", HASH, [SYMBOL, 50])
database(dbPath, COMPO, [dbDate, dbSymbol], , "TSDB")
easyNsqLog(`subscribeNsqTrade, "create database "+ dbPath)
}
if(existsTable(dbPath, tableNames[0])) {
easyNsqLog(`subscribeNsqTrade, "find existing dfsTable ("+ dbPath + "," + tableNames[0] + ")")
} else {
createPartitionedTable(
dbHandle=database(dbPath),
table=table(1:0, tradeSchema.name, tradeSchema.type),
tableName=tableNames[0],
partitionColumns=`TradeDate`InstrumentID,
sortColumns=`InstrumentID`TransactTime,
keepDuplicates=ALL
)
easyNsqLog(`subscribeNsqTrade, "create dfsTable ("+ dbPath + "," + tableNames[0]+")")
}
// 流表入库的订阅
subscribeTable(
tableName=streamTableNames[0],
actionName="easyNSQ_saveToDfsTable",
offset=-1,
handler=loadTable(dbPath, tableNames[0]),
msgAsTable=true,
batchSize=10000,
throttle=1,
reconnect = true
)
easyNsqLog(`subscribeNsqOrders, stringFormat("dfs table (%W,%W) is listening to stream table %W", dbPath, tableNames[0], streamTableNames[0]))
}
// nsq订阅实时数据
nsq::subscribe(`trade, `sz, objByName(streamTableNames[0], true))
nsq::subscribe(`trade, `sh, objByName(streamTableNames[0], true))
easyNsqLog(`subscribeNsqOrders, "successfully subscribe nsq sh and sz trade data")
} else {
// sz,sh分开存储
for(i in 0:markets.size()) {
// 持久化共享流表
enableTableShareAndPersistence(
table=streamTable(1:0, tradeSchema.name, tradeSchema.type),
tableName=streamTableNames[i],
cacheSize=500000,
preCache=100000
)
easyNsqLog(`subscribeNsqTrade, "initialize shared and persistence streamTable "+streamTableNames[i])
}
if(saveToDfs) {
// 分布式表
if(existsDatabase(dbPath)) {
easyNsqLog(`subscribeNsqTrade, "find existing database "+ dbPath)
} else {
dbDate = database("", VALUE, 2023.01.01..2023.12.01)
dbSymbol = database("", HASH, [SYMBOL, 25])
database(dbPath, COMPO, [dbDate, dbSymbol], , "TSDB")
easyNsqLog(`subscribeNsqTrade, "create database "+ dbPath)
}
for(i in 0:markets.size()) {
if(existsTable(dbPath, tableNames[i])) {
easyNsqLog(`subscribeNsqTrade, "find existing dfsTable ("+ dbPath + "," + tableNames[i]+")")
} else {
createPartitionedTable(
dbHandle=database(dbPath),
table=table(1:0, tradeSchema.name, tradeSchema.type),
tableName=tableNames[i],
partitionColumns=`TradeDate`InstrumentID,
sortColumns=`InstrumentID`TransactTime,
keepDuplicates=ALL
)
easyNsqLog(`subscribeNsqTrade, "create dfsTable ("+ dbPath + "," + tableNames[i]+")")
}
// 流表入库的订阅
subscribeTable(
tableName=streamTableNames[i],
actionName="easyNSQ_saveToDfsTable",
offset=-1,
handler=loadTable(dbPath, tableNames[i]),
msgAsTable=true,
batchSize=10000,
throttle=1,
reconnect = true
)
easyNsqLog(`subscribeNsqOrders, stringFormat("dfs table (%W,%W) is listening to stream table %W", dbPath, tableNames[i], streamTableNames[i]))
}
}
for(i in 0:markets.size()) {
// nsq订阅实时数据
nsq::subscribe(`trade, markets[i], objByName(streamTableNames[i], true))
easyNsqLog(`subscribeNsqOrders, "successfully subscribe nsq " + markets[i] + " trade data")
}
}
} catch(ex) {
easyNsqLog("subscribeNsqTrade", string(ex[0]) + ":" + string(ex[1]), true)
return NULL
}
if(saveToDfs) {
return streamTableNames, dbPath, tableNames
} else {
return streamTableNames
}
}
def subscribeNsqSnapshot(markets, merge, saveToDfs, streamTableNames, dbPath, tableNames) {
try {
snapshotSchema = nsq::getSchema(`snapshot)
// sz,sh合并存储
if(merge) {
// 持久化共享流表
enableTableShareAndPersistence(
table=streamTable(1:0, snapshotSchema.name, snapshotSchema.type),
tableName=streamTableNames[0],
cacheSize=500000,
preCache=100000
)
easyNsqLog(`subscribeNsqSnapshot, "initialize shared and persistence streamTable "+streamTableNames[0])
if(saveToDfs) {
// 分布式表
if(existsDatabase(dbPath)) {
easyNsqLog(`subscribeNsqSnapshot, "find existing database "+ dbPath)
} else {
dbDate = database("", VALUE, 2023.01.01..2023.12.01)
dbSymbol = database("", HASH, [SYMBOL, 50])
database(dbPath, COMPO, [dbDate, dbSymbol], , "TSDB")
easyNsqLog(`subscribeNsqSnapshot, "create database "+ dbPath)
}
if(existsTable(dbPath, tableNames[0])) {
easyNsqLog(`subscribeNsqSnapshot, "find existing dfsTable ("+ dbPath + "," + tableNames[0] + ")")
} else {
createPartitionedTable(
dbHandle=database(dbPath),
table=table(1:0, snapshotSchema.name, snapshotSchema.type),
tableName=tableNames[0],
partitionColumns=`TradeDate`InstrumentID,
sortColumns=`InstrumentID`UpdateTime,
keepDuplicates=ALL
)
easyNsqLog(`subscribeNsqSnapshot, "create dfsTable ("+ dbPath + "," + tableNames[0]+")")
}
// 流表入库的订阅
subscribeTable(
tableName=streamTableNames[0],
actionName="easyNSQ_saveToDfsTable",
offset=-1,
handler=loadTable(dbPath, tableNames[0]),
msgAsTable=true,
batchSize=10000,
throttle=1,
reconnect = true
)
easyNsqLog(`subscribeNsqOrders, stringFormat("dfs table (%W,%W) is listening to stream table %W", dbPath, tableNames[0], streamTableNames[0]))
}
// nsq订阅实时数据
nsq::subscribe(`snapshot, `sz, objByName(streamTableNames[0], true))
nsq::subscribe(`snapshot, `sh, objByName(streamTableNames[0], true))
easyNsqLog(`subscribeNsqOrders, "successfully subscribe nsq sz and sh snapshot data")
} else {
// sz,sh分开存储
for(i in 0:markets.size()) {
// 持久化共享流表
enableTableShareAndPersistence(
table=streamTable(1:0, snapshotSchema.name, snapshotSchema.type),
tableName=streamTableNames[i],
cacheSize=500000,
preCache=100000
)
easyNsqLog(`subscribeNsqSnapshot, "initialize shared and persistence streamTable "+streamTableNames[i])
}
if(saveToDfs) {
// 分布式表
if(existsDatabase(dbPath)) {
easyNsqLog(`subscribeNsqSnapshot, "find existing database "+ dbPath)
} else {
dbDate = database("", VALUE, 2023.01.01..2023.12.01)
dbSymbol = database("", HASH, [SYMBOL, 25])
database(dbPath, COMPO, [dbDate, dbSymbol], , "TSDB")
easyNsqLog(`subscribeNsqSnapshot, "create database "+ dbPath)
}
for(i in 0:markets.size()) {
if(existsTable(dbPath, tableNames[i])) {
easyNsqLog(`subscribeNsqSnapshot, "find existing dfsTable ("+ dbPath + "," + tableNames[i]+")")
} else {
createPartitionedTable(
dbHandle=database(dbPath),
table=table(1:0, snapshotSchema.name, snapshotSchema.type),
tableName=tableNames[i],
partitionColumns=`TradeDate`InstrumentID,
sortColumns=`InstrumentID`UpdateTime,
keepDuplicates=ALL
)
easyNsqLog(`subscribeNsqSnapshot, "create dfsTable ("+ dbPath + "," + tableNames[i]+")")
}
// 流表入库的订阅
subscribeTable(
tableName=streamTableNames[i],
actionName="easyNSQ_saveToDfsTable",
offset=-1,
handler=loadTable(dbPath, tableNames[i]),
msgAsTable=true,
batchSize=10000,
throttle=1,
reconnect = true
)
easyNsqLog(`subscribeNsqOrders, stringFormat("dfs table (%W,%W) is listening to stream table %W", dbPath, tableNames[i], streamTableNames[i]))
}
}
for(i in 0:markets.size()) {
// nsq订阅实时数据
nsq::subscribe(`snapshot, markets[i], objByName(streamTableNames[i], true))
easyNsqLog(`subscribeNsqOrders, "start to subscribe nsq " + markets[i] + " snapshot data")
}
}
} catch(ex) {
easyNsqLog("subscribeNsqSnapshot", string(ex[0]) + ":" + string(ex[1]), true)
return NULL
}
if(saveToDfs) {
return streamTableNames, dbPath, tableNames
} else {
return streamTableNames
}
}
/* *
* @ Function name: iniNsqEnv
* @ Brief: clean out existing stream tables and their subscriptions
* @ Param:
* streamTableNames: the name of stream tables to clean
* @ Sample usage: iniNsqEnv() to clean all the default stream tables,
* iniNsqEnv(["msqNsqOrdersSHStream", "msqNsqOrdersSZStream"]) to clean stream table msqNsqOrdersSHStream and msqNsqOrdersSZStream
*/
def iniNsqEnv(streamTableNames=NULL) {
streamTBs = streamTableNames
if(isVoid(streamTBs)) {
streamTBs = ['nsqStockOrdersStream', 'nsqStockOrdersSHStream', 'nsqStockOrdersSZStream', 'nsqStockTradeStream',
'nsqStockTradeSHStream', 'nsqStockTradeSZStream', 'nsqStockSnapshotStream', 'nsqStockSnapshotSHStream', 'nsqStockSnapshotSZStream']
}
try {
// cancel stream table's subscription
subRecords = select tableName, actions from getStreamingStat().pubTables where tableName in streamTBs
for(record in subRecords) {
tbName = record["tableName"]
actionList = record["actions"]
actionList = actionList.strReplace("[", "").strReplace("]", "").split(',')
for(actName in actionList) {
try {
unsubscribeTable(tableName=tbName, actionName=actName)
easyNsqLog(`iniNsqEnv, "successfully unsubscribe (" + tbName + ", " + actName + ")")
sleep(10)
}
catch(ex) {
easyNsqLog(`iniNsqEnv, stringFormat("exception when unsubscribing (%W, %W): %W:%W", tbName, actName, string(ex[0]), string(ex[1])), true)
}
}
}
// drop stream table
existTables = exec name from objs(true) where form="TABLE" and type="REALTIME" and shared=true and name in streamTBs
for(tbName in existTables) {
try {
dropStreamTable(tbName)
easyNsqLog(`iniNsqEnv, "successfully drop stream table: " + tbName)
} catch (ex) {
easyNsqLog(`iniNsqEnv, stringFormat("exception when droping %W: %W:%W", tbName, string(ex[0]), string(ex[1])), true)
}
}
} catch (ex) {
easyNsqLog(`iniNsqEnv, string(ex[0])+":"+string(ex[1]), true)
}
}
/* *
* @ Function name: iniNsqDfs
* @ Brief: clear out the existing dfs table
* @ Param:
* dbName: the database name of dfs tables to clean
* tbNames: the name of dfs tables to clean
* @ Sample usage: iniNsqDfs() to clean all the default dfs tables,
* iniNsqDfs("dfs://myNsqTrade", ["myNsqTradeSH", "myNsqTradeSZ"]) to clean dfs table (dfs://myNsqTrade,myNsqTradeSH) and ("dfs://myNsqTrade", "myNsqTradeSZ")
*/
def iniNsqDfs(dbName=NULL, tbNames=NULL) {
dropTableIfExists = def (x,y) {
if(existsTable(x,y)) {
try {
dropTable(database(x), y)
easyNsqLog(`iniNsqDfs, stringFormat("successfully drop dfs table (%W,%W)", x, y))
} catch (ex) {
easyNsqLog(`iniNsqDfs, string(ex[0])+":"+string(ex[1]), true)
}
}
}
try {
if(isVoid(dbName) && isVoid(tbNames)) {
loop(dropTableIfExists{"dfs://nsqStockOrders"}, `ordersSH`ordersSZ`orders)
loop(dropTableIfExists{"dfs://nsqStockTrade"}, `tradeSH`tradeSZ`trade)
loop(dropTableIfExists{"dfs://nsqStockSnapshot"}, `snapshotSH`snapshotSZ`snapshot)
} else if(!isVoid(dbName) && !isVoid(tbNames)) {
loop(dropTableIfExists{dbName}, tbNames)
} else {
easyNsqLog(`iniNsqDfs, "invalid arguments, dbName and tbNames should be both null, or both non-null", true)
}
} catch (ex) {
easyNsqLog(`iniNsqDfs, string(ex[0])+":"+string(ex[1]), true)
}
}
/* *
* @ Function name: subscribeNsq
* @ Brief: subscribe nsq real-time data, store data to stream tables, or dfs tables
* @ Param:
* configFilePath: the configuration file for nsq data server
* dataSource: the nsq data type of interest
* markets: the market of interest, default value is ["sz", "sh"]
* merge: whether sh and sz data are stored together
* saveToDfs: whether data is stored to dfs tables
* streamTableNames: the name of stream tables where the real-time nsq data is going to store
* dbPath: the path of database where the real-time nsq data is going to store if saveToDfs option is on
* tableNames: the name of dfs tables where the real-time nsq data is going to store if saveToDfs option is on
* @ Return: NULL if there is any exception,
* return streamTableNames if saveToDfs=false,
* return streamTableNames, dbPath, tableNames if saveToDfs=true
* @ Sample usage: subscribeNsq(configFilePath, 'orders', "sh")
*/
def subscribeNsq(configFilePath, dataSource, markets=["sz", "sh"], merge=false, saveToDfs=false, streamTableNames=NULL, dbPath=NULL, tableNames=NULL) {
ds = validateDataSourceParam(dataSource)
if(isVoid(ds)) return NULL
marketList = validateMarketsParam(markets)
if(isVoid(marketList)) return NULL
streamTbNameList = validateStreamTableNamesParam(ds, marketList, merge, streamTableNames)
if(isVoid(streamTbNameList)) return NULL
dbName = validateDbPathParam(ds, dbPath)
if(isVoid(dbName)) return NULL
tbNameList = validateTableNamesParam(ds, marketList, merge, tableNames)
if(isVoid(tbNameList)) return NULL
try{ nsq::connect(configFilePath) } catch (ex) { easyNsqLog("nsq::connect", string(ex[0])+":"+string(ex[1])) }
if(ds == `orders) {
return subscribeNsqOrders(marketList, merge, saveToDfs, streamTbNameList, dbName, tbNameList)
} else if(ds == `trade) {
return subscribeNsqTrade(marketList, merge, saveToDfs, streamTbNameList, dbName, tbNameList)
} else if(ds == `snapshot) {
return subscribeNsqSnapshot(marketList, merge, saveToDfs, streamTbNameList, dbName, tbNameList)
}
}
/* *
* @ Function name: closeNsqConnection
* @ Brief: close the connection to nsq data server
* @ Return: true if close connection successfully, otherwise return false
* @ Sample usage: closeNsqConnection()
*/
def closeNsqConnection() {
try {
nsq::close()
} catch (ex) {
easyNsqLog(`closeNsqConnection, string(ex[0])+":"+string(ex[1]), true)
return false
}
return true
}