Uploading Data objects
This section introduces how to use member function DBConnection::upload
to uplaod C++ objects to DolphinDB server with detailed examples.
The function declaration for DBConnection::upload
is as follows:
ConstantSP upload(const string& name, const ConstantSP& obj);
Arguments
- name: The name of the object uploaded.
- obj: The object to be uploaded.
Examples
Example 1: Upload a Table Object
//define a function to create a table
TableSP createDemoTable(){
vector<string> colNames = {"name", "date", "price"};
vector<DATA_TYPE> colTypes = {DT_STRING, DT_DATE, DT_DOUBLE};
int colNum = 3, rowNum = 10000, indexCapacity=10000;
ConstantSP table = Util::createTable(colNames, colTypes, rowNum, indexCapacity);
vector<VectorSP> columnVecs;
for(int i = 0; i < colNum; ++i)
columnVecs.push_back(table->getColumn(i));
int array_dt_buf[Util::BUF_SIZE]; //create a buffer for the "date" column of array type
double array_db_buf[Util::BUF_SIZE]; //create a buffer for the "price" column of array type
int start = 0;
int no=0;
while (start < rowNum) {
size_t len = std::min(Util::BUF_SIZE, rowNum - start);
int *dtp = columnVecs[1]->getIntBuffer(start, len, array_dt_buf); //points to buffer heads of buffer returned by `getIntBuffer`
double *dbp = columnVecs[2]->getDoubleBuffer(start, len, array_db_buf); //points to buffer heads of buffer returned by `getDoubleBuffer`
for (size_t i = 0; i < len; ++i) {
columnVecs[0]->setString(i+start, "name_"+std::to_string(++no)); //assign values to the "name" column of string type
dtp[i] = 17898+i;
dbp[i] = (rand()%100)/3.0;
}
columnVecs[1]->setInt(start, len, dtp); //use `setInt` to update the array
columnVecs[2]->setDouble(start, len, dbp); //use `setDouble` to update the array
start += len;
}
return table;
}
int main(int argc, const char **argv)
{
DBConnection conn(false, false, 30, false);
conn.connect("127.0.0.1", 8848, "admin", "123456");
ConstantSP t = createDemoTable();
ConstantSP result = conn.upload("t", t); //upload table t
std::cout << conn.run("t")->size() << std::endl; //output the row count of table t: 10000
}
Example 2: Upload a Compressed Table Object
From version 1.30.19 onwards, the C++ API introduces a feature to minimize
network transmission overhead. It allows compressing table data before uploading
through the setColumnCompressMethods
function. This function
enables specifying different compression methods for individual columns within a
table. Currently supported compression algorithms are "lz4" and "delta". The
"delta" algorithm is restricted to SHORT, INT, LONG, and date/time data types.
In scenarios with slow network speeds, it is advisable to leverage table
compression to enhance performance.
The following example demonstrates how to enable compression and upload a compressed table object.
int main(int argc, const char **argv)
{
//specify parameter compress to true when construct the DBConnection object
DBConnection conn_compress(false, false, 7200, true);
//connect to the server
conn_compress.connect("127.0.0.1", 8848);
//create a shared stream table containing a DATE and a LONG column
conn_compress.run("share streamTable(1:0, `time`value,[DATE,LONG]) as table1");
//generate 600000 records to be uploaded
const int count = 600000;
vector<int> time(count);
vector<long long>value(count);
int basetime = Util::countDays(2012, 1, 1);
for (int i = 0; i<count; i++) {
time[i] = basetime + (i % 15);
value[i] = i;
}
VectorSP timeVector = Util::createVector(DT_DATE, count, count);
VectorSP valueVector = Util::createVector(DT_LONG, count, count);
timeVector->setInt(0, count, time.data());
valueVector->setLong(0, count, value.data());
vector<ConstantSP> colVector{ timeVector,valueVector };
//create a table
vector<string> colName = { "time","value" };
TableSP table = Util::createTable(colName, colVector);
//specify compression algorithm for each column
vector<COMPRESS_METHOD> typeVec{ COMPRESS_DELTA,COMPRESS_LZ4 };
table->setColumnCompressMethods(typeVec);
//upload table to DolphinDB stream table "table1"
vector<ConstantSP> args{ table };
int insertCount = conn_compress.run("tableInsert{table1}", args)->getInt();
//output: 600000
std::cout << insertCount << std::endl;
}