在python中怎么建库建表

在python中怎么建库建表

我在DolphinDB GUI中创建了如下分布式数据库表,它是分区类型是组合分区,第一层按日期值分区,第二层按设备id范围分区。

    dbName="dfs://forwardtekDdb1"

tableName="windTurbine"

login("admin","123456")

if(exists(dbName))

dropDatabase(dbName)

tableSchema = table(100:0,`id`tm`val,[INT,DATETIME,FLOAT]);

db1 = database("",VALUE,2017.01M..2020.12M)

db2 = database("",RANGE,0..1000*30+1)

db = database(dbName,COMPO,[db1,db2])

dfsTable = db.createPartitionedTable(tableSchema,tableName,`tm`id)

现在我想把它移植到python中,请问应该怎么写?我在python api readme第3.2节找到了创建分区表的例子,但它是值分区,不是组合分区,我还是不知道应该怎么写。


回答:

import dolphindb as ddb

s = ddb.session()

s.connect("127.0.0.1", 8848,"admin","123456")

dbName="dfs://forwardtekDdb2"

tableName='windTurbine'

script="""

dbName='{db}'

if(exists(dbName))

dropDatabase(dbName)

tableSchema = table(100:0,`id`tm`val,[INT,DATETIME,FLOAT]);

db1 = database("",VALUE,2017.01M..2020.12M)

db2 = database("",RANGE,0..1000*30+1)

db = database(dbName,COMPO,[db1,db2])

db.createPartitionedTable(tableSchema,'{tb}',`tm`id)

""".format(db=dbName,tb=tableName)

s.run(script)


回答:

可以通过database函数来创建。

mydbName="dfs://forwardtekDdb1"

if(s.existsDatabase(mydbName)):

s.dropDatabase(mydbName)

partitions1="2017.01M..2020.12M"

partitions2 = "0..1000*30+1"

s.database(dbName="db1", partitionType=keys.VALUE, partitions=partitions1, dbPath="")

s.database(dbName="db2", partitionType=keys.RANGE, partitions=partitions2, dbPath="")

s.database(dbName="db", partitionType=keys.COMPO, partitions="[db1, db2]", dbPath=mydbName)

s.run("db.createPartitionedTable(table(100:0,`id`tm`val,[INT,DATETIME,FLOAT]), `windTurbine, `tm`id)")

以上是 在python中怎么建库建表 的全部内容, 来源链接: utcz.com/a/165900.html

回到顶部