def loadTable(self, tableName, dbPath=None, partitions=None, memoryMode=False): """ :param dbPath: DolphinDB table db path :param tableName: DolphinDB table name :param partitions: partitions to be loaded when specified :param memoryMode: loadTable all in ram or not :return:a Table object """ if partitions is None: partitions = [] if dbPath: runstr = '{tableName} = loadTable("{dbPath}", "{data}",{partitions},{inMem})' fmtDict = dict() tbName = _generate_tablename() fmtDict['tableName'] = tbName fmtDict['dbPath'] = dbPath fmtDict['data'] = tableName if type(partitions) is list: if len(partitions) and type(partitions[0]) is not str: fmtDict['partitions'] = ( '[' + ','.join(str(x) for x in partitions) + ']') if len(partitions) else "" else: fmtDict['partitions'] = ('["' + '","'.join(partitions) + '"]') if len(partitions) else "" else: if type(partitions) is str: fmtDict['partitions'] = '"' + partitions + '"' else: fmtDict['partitions'] = partitions fmtDict['inMem'] = str(memoryMode).lower() runstr = re.sub(' +', ' ', runstr.format(**fmtDict).strip()) self.run(runstr) return Table(data=tbName, s=self) else: return Table(data=tableName, s=self)
def loadTextEx(self, dbPath="", tableName="", partitionColumns=None, filePath="", delimiter=","): """ :param tableName: loadTextEx table name :param dbPath: database path, when dbPath is empty, it is in-memory database :param partitionColumns: partition columns as a python list :param filePath:the file to load into database :param delimiter: :return: a Table object """ if partitionColumns is None: partitionColumns = [] isDBPath = True if "/" in dbPath or "\\" in dbPath or "dfs://" in dbPath: dbstr = 'db=database("' + dbPath + '")' # print(dbstr) self.run(dbstr) tbl_str = '{tableNameNEW} = loadTextEx(db, "{tableName}", {partitionColumns}, "{filePath}", {delimiter})' else: isDBPath = False tbl_str = '{tableNameNEW} = loadTextEx(' + dbPath + ', "{tableName}", {partitionColumns}, "{filePath}", {delimiter})' fmtDict = dict() fmtDict['tableNameNEW'] = _generate_tablename() fmtDict['tableName'] = tableName fmtDict['partitionColumns'] = str(partitionColumns) fmtDict['filePath'] = filePath fmtDict['delimiter'] = delimiter # tbl_str = tableName+'=loadTextEx(db,"' + tableName + '",'+ str(partitionColumns) +',"'+ filePath+"\",'"+delimiter+"')" tbl_str = re.sub(' +', ' ', tbl_str.format(**fmtDict).strip()) # print(tbl_str) self.run(tbl_str) if isDBPath: return Table(data=fmtDict['tableName'], dbPath=dbPath, s=self) else: return Table(data=fmtDict['tableNameNEW'], s=self)
def loadTableBySQL(self, tableName, dbPath, sql): """ :param tableName: DolphinDB table name :param dbPath: DolphinDB table db path :param sql: sql query to load the data :return:a Table object """ # loadTableBySQL runstr = 'db=database("' + dbPath + '")' # print(runstr) self.run(runstr) runstr = tableName + '= db.loadTable("%s")' % tableName # print(runstr) self.run(runstr) runstr = tableName + "=loadTableBySQL(<%s>)" % sql # runstr = sql # print(runstr) self.run(runstr) return Table(data=tableName, s=self)
def table(self, dbPath=None, data=None, tableAliasName=None, inMem=False, partitions=None): """ :param data: pandas dataframe, python dictionary, or DolphinDB table name :param dbPath: DolphinDB database path :param tableAliasName: DolphinDB table alias name :param inMem: load the table in memory or not :param partitions: the partition column to be loaded into memory. by default, load all :return: a Table object """ if partitions is None: partitions = [] return Table(dbPath=dbPath, data=data, tableAliasName=tableAliasName, inMem=inMem, partitions=partitions, s=self)
def ploadText(self, filename, delimiter=","): tableName = _generate_tablename() runstr = tableName + '= ploadText("' + filename + '","' + delimiter + '")' self.run(runstr) return Table(data=tableName, s=self)
def table(self, data, dbPath=None): return Table(data=data, dbPath=dbPath, s=self)