AutoFitTableAppender

The AutoFitTableAppender (AFTA) is suitable for scenarios where the table schema on the C++ API differs from the table schema on the DolphinDB server. AFTA first attempts to convert the table schema to match the server's before writing data with tableInsert. It operates as a single-threaded, synchronized write method.

Constructing an AFTA

The constructor declaration is as follows:

AutoFitTableAppender(string dbUrl, string tableName, DBConnection& conn);

Arguments

  • dbUrl: The path of a DFS database folder. Specify it as an empty string for an in-memory table.
  • tableName: The table name.
  • DBConnection: A connected DBConnection object.

append

You can use the append function to append a table to the AFTA object. It will not return until the AFTA has finished writing all the data.

The function declaration is as follows:

int append(TableSP table);

Arguments

  • table: The table to be appended.

Return Values

The number of rows that have been appended.

Examples

(1) Create a DFS table with the following script:

dbPath = "dfs://SAMPLE_TRDDB";
tableName = `demoTable
if(existsDatabase(dbPath)){
	dropDatabase(dbPath)
}
db = database(dbPath, VALUE, 2010.01.01..2010.01.30)
pt=db.createPartitionedTable(table(1000000:0, `name`date`price, [STRING,DATE,DOUBLE]), tableName, `date)

(2) Use AFTA to append a table to the DFS table “demoTable“.

TableSP createDemoTable(){
    vector<string> colNames = {"name", "date","price"};
    vector<DATA_TYPE> colTypes = {DT_STRING, DT_DATE, DT_DOUBLE};
    int colNum = 3, rowNum = 10, indexCapacity = 10;
    ConstantSP table = Util::createTable(colNames, colTypes, rowNum, indexCapacity);
    vector<VectorSP> columnVecs;
    for(int i = 0; i < colNum; ++i)
        columnVecs.push_back(table->getColumn(i));

    for(int i = 0; i < rowNum; ++i){
        columnVecs[0]->set(i, Util::createString("name_"+std::to_string(i)));
        columnVecs[1]->set(i, Util::createDate(2010, 1, i+1));
        columnVecs[2]->set(i, Util::createDouble((rand()%100)/3.0));
    }
    return table;
}

int main(int argc, const char **argv)
{
    DBConnection conn;
    conn.connect("127.0.0.1", 8848, "admin", "123456");
    AutoFitTableAppender appender("dfs://SAMPLE_TRDDB", "demoTable", conn);
    TableSP table = createDemoTable();
    appender.append(table);
    ConstantSP result = conn.run("select * from loadTable('dfs://SAMPLE_TRDDB', `demoTable)");
    std::cout <<  result->getString() << std::cout;
}