简体   繁体   English

如何从流订阅终端的处理程序中获取某些列并形成新表?

[英]How to get certain columns and form a new table from the handler of the stream subscription terminal?

I use c++ streaming api to subscribe to the stream table.我使用c++流api订阅流表。 The subscribed streaming table has many columns.订阅的流表有很多列。 I need to select a few columns to form a new table and pass it to the processing queue for subsequent processing.我需要选择几列组成一个新表,并传递给处理队列进行后续处理。 For example, I want to get 30 of the 100 columns and transfer them to the processing queue as a new table.例如,我想获取 100 列中的 30 列并将它们作为新表传输到处理队列中。 How to make it more efficiently?怎么做更高效?

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);

My suggestion is to use Util::createTable(const vector& colNames, const vector& cols) to create a new table.我的建议是使用 Util::createTable(const vector& colNames, const vector& cols) 创建一个新表。 Take the first 30 columns from the original table to create a new table, use t->getColumnName(i) to get the original column names and t->getColumn(i) to get the column vector.取原表的前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);
}

You can also add new data to Table by using the function append!您还可以使用append!函数新数据添加到表中

// 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM