loadTextEx

Syntax

loadTextEx(dbHandle, tableName, partitionColumns, filename, [delimiter], [schema], [skipRows=0], [transform])

Arguments

dbHandle the distributed database where the imported data will be saved. The database can be either in the distributed file system or an in-memory database.

tableName a string indicating the name of the table with the imported data.

partitionColumns a string scalar/vector indicating partitioning column(s). For sequential partition, partitionColumns is “” as it doesn’t need a partitioning column. For composite partition, partitionColumns is a string vector.

filename the input text file name with its absolute path.

delimiter a STRING scalar indicating the table column separator. It can consist of one or more characters, with the default being a comma (‘,’).

schema a table. It can have the following columns, among which “name” and “type” columns are required.

column name

data type

meaning

name

STRING scalar

column name

type

STRING scalar

data type

format

STRING scalar

the format of temporal columns

col

INT scalar or vector

the columns to be loaded

skipRows is an integer between 0 and 1024 indicating the rows in the beginning of the text file to be ignored. The default value is 0.

transform is a unary function. The parameter of the function must be a table.

Details

Load a text file into DolphinDB database.

If dbHandle is specified and is not empty string “”: load a text file to a distributed database. The result is a table object with metadata of the table.

If dbHandle is empty string “” or unspecified: load a text file as a partitioned in-memory table. For this usage, when we define dbHandle with function database, the parameter directory must also be the empty string “” or unspecified.

If parameter transform is specified, we need to first execute createPartitionedTable and then load the data. The system will apply the function specified in parameter transform and then save the results into the database.

Function loadTextEx and function loadText share many common features, such as whether the first row is treated as the header, how the data types of columns are determined, how the system adjusts illegal column names, etc. For more details, please refer to function loadText.

Examples

Use the following script to generate the text file to be used:

$ n=10000
$ ID=rand(100, n)
$ dates=2017.08.07..2017.08.11
$ date=rand(dates, n)
$ vol=rand(1..10 join int(), n)
$ t=table(ID, date, vol)
$ saveText(t, "C:/DolphinDB/Data/t.txt");

Ex 1. Load t.txt into a DFS database with a range domain on ID.

$ db = database("dfs://rangedb", RANGE, 0 51 101)
$ pt=loadTextEx(db,`pt,`ID,"C:/DolphinDB/Data/t.txt");

To load table pt from the database:

$ db = database("dfs://rangedb")
$ pt = loadTable(db, `pt);

Ex 2. Load t.txt into a DFS database with a composite domain.

$ dbDate = database("", VALUE, 2017.08.07..2017.08.11)
$ dbID=database("", RANGE, 0 51 101)
$ db = database("dfs://compoDB", COMPO, [dbDate, dbID])
$ loadTextEx(db,`pt,`date`ID, "C:/DolphinDB/Data/t.txt");

Ex 3. Load t.txt into a partitioned in-memory table with value domain on date. Please note that here we need to assign “” to parameter tableName.

$ db = database("", VALUE, 2017.08.07..2017.08.11)
$ pt = db.loadTextEx("", `date, "C:/DolphinDB/Data/t.txt");

$ pt.sortBy!(`ID`x);

Ex 4. Convert all NULL values to 0 and then load the data into a dfs database with a composite domain.

$ dbDate = database("", VALUE, 2017.08.07..2017.08.11)
$ dbID=database("", RANGE, 0 51 101)
$ db = database("dfs://compoDB", COMPO, [dbDate, dbID]);

$ db.createPartitionedTable(t,`pt,`date`ID)
$ loadTextEx(db,`pt,`date`ID, "C:/DolphinDB/Data/t.txt",,,,nullFill!{,0});