-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsample.py
39 lines (37 loc) · 1.42 KB
/
sample.py
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
import dolphindb as ddb
import datetime as date
import pandas as pd
import numpy as np
s = ddb.session()
s.connect("localhost",8081,"admin","123456")
dates = '2019.08.01..2019.08.10'
syms = '`IBM`AAPL`MSFT`GS`YHOO`TS`TSL`C`MC`HA'
script='data = table(1:0,`date`sym`price, [DATE,SYMBOL,DOUBLE])'
print(script)
t = s.run(script)
## Create a partitioned DFS table
userName = 'admin'
pwd = '123456'
symRange = 'cutPoints({a},3)'.format(a=syms)
dbPath = 'dfs://demo'
tableName = 'testTableName'
if(s.existsDatabase(dbPath)):
s.dropDatabase(dbPath)
lgnScript = 'login("{a}","{b}")'.format(a=userName,b=pwd)
dbDateScript='dbDate=database("",VALUE,{a})'.format(a=dates)
dbSymScript='dbSym=database("",RANGE,{b})'.format(b=symRange)
script= '{login};{db1};{db2};db = database("{db}",COMPO,[dbDate,dbSym])'.format(login=lgnScript, db1=dbDateScript, db2=dbSymScript, db=dbPath)
s.run(script)
## Write to the DFS table
script='db.createPartitionedTable(data,`{a}, `date`sym)'.format(a=tableName)
print(script)
s.run(script)
loadScript = 'tb = loadTable("{a}","{b}")'.format(a=dbPath,b=tableName)
s.run(loadScript)
script = 'tableInsert{tb}'
print(script)
dataTable = pd.DataFrame({'date':np.array(['2019-08-01', '2019-08-02'], dtype="datetime64[D]"),'sym':['MSFT','GS'],'price':[90,89]})
s.run(script, dataTable)
script='select * from loadTable("{a}","{b}")'.format(a=dbPath,b=tableName)
re = s.run(script)
print(re)