在C++流数据订阅端的handler中如何得到某几个列并组成新表

我用c++ streaming 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);
请先 登录 后评论

1 个回答

wale

可以用Util::createTable(const vector<string>& colNames, const vector<ConstantSP>& 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);
		}

也可以参考《c++ api读写指南》(https://gitee.com/dolphindb/Tutorials_CN/blob/master/c++api.md#table)中table节的方法实现:

attachments-2021-09-b2hflccY614079aa9e4e4.png

请先 登录 后评论