[英]How to get certain columns and form a new table from the handler of the stream subscription terminal?
我使用c++流api订阅流表。 订阅的流表有很多列。 我需要选择几列组成一个新表,并传递给处理队列进行后续处理。 例如,我想获取 100 列中的 30 列并将它们作为新表传输到处理队列中。 怎么做更高效?
vector<ThreadSP> tmp;
ThreadSP tSnapshot = new Thread(new Executor([=, &tmp, &client]() {
DBConnection conn;
try {
bool ret = conn.connect(host, serverport, "admin", "123456");
cout << "connected to the server" << endl;
if (!ret) {
cout << "Failed to connect to the server " << host << endl;
return;
}
}
catch (exception &ex) {
cout << "Failed to connect with error: " << ex.what();
return;
}
auto handler_1 = [&](Message msg) {
TableSP t = (TableSP)msg;
// todo
};
auto t1 = client.subscribe(host, serverport, handler_1, "pub_union_1", "union_1_Sub", 0, true, nullptr,true);
t1->join();
}));
tSnapshot->start();
tmp.emplace_back(tSnapshot);
我的建议是使用 Util::createTable(const vector& colNames, const vector& cols) 创建一个新表。 取原表的前30列创建新表,使用t->getColumnName(i)得到原列名,t->getColumn(i)得到列向量。
auto handler_1 = [&](Message msg) {
vector<string> colName;
vector<ConstantSP> col;
TableSP t = (TableSP)msg;
for (int i = 0; i < 30; ++i) {
colName.push_back(t->getColumnName(i));
col.push_back(t->getColumn(i));
}
TableSP ret = Util::createTable(colName, col);
}
// prepare data
VectorSP col0 = Util::createVector(DT_INT, 0);
VectorSP col1 = Util::createVector(DT_BOOL, 0);
// ...
// col0->append(...)
// col1->append(...)
// ...
vector<ConstantSP> cols;
cols.push_back(col0);
cols.push_back(col1);
// ...
INDEX insertedRows;
string errorMsg;
if(!c->append(cols, insertedRows, errorMsg)) {
// error handling
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.