将结果存储在 C++Map 中,然后迭代它,然后打印出来

Store the result in a Map in C++ and then iterate it and then print out?

本文关键字:然后 打印 迭代 结果 C++Map 存储      更新时间:2023-10-16

我已经开始为Cassandra使用C++ libcql库。我正在尝试使用带有libcql库的C++从Cassandra检索数据。

每当我使用 cqlsh 进入命令行并像这样选择 -

 select records from profile_user where user_id = '1';

我总是在 cql 命令行上得到以下输出,其中records列实际上是一个map,其中键e1,值HELLO。以同样的方式e2键并再次HELLO值。当我在 CQL 中创建表时,我在使用 CQL 的收集功能时创建了记录作为映射。

 records
--------------------------------
 {'e1': 'HELLO', 'e2': 'HELLO'}

现在来到C++世界——

现在我正在尝试从C++ libcql library中检索同样的东西......我将在C++运行相同的上述选择查询,我想返回一个具有e1, e2 as the keyHELLO as there value inside that map的地图......可以在C++做到吗?

/**
 * This method will retrieve the data from Cassandra..
 * And then call print_rows method to print it out on the console
 */
void get_attributes(string id){
    try{
        // some code
        //Connection open
        connection_open();
        execute_query("USE testks;");
        //this will give me the result back of the select query
        cql_result_t& result = execute_query("select * from profile_user where key ='"+id+"';");
        // and this is printing it out on the console
        print_rows(result);
        // some code
    } catch (int e){
        // some code here
    }
}

以下是运行我的C++程序后将在控制台上打印结果的方法 -

/**
 * This method prints out the result on the console..    *
 *
 */
void print_rows(cql::cql_result_t& result) {
    while (result.next()) {
        for (size_t i = 0; i < result.column_count(); ++i) {
            cql::cql_byte_t* data = NULL;
            cql::cql_int_t size = 0;
            result.get_data(i, &data, size);
            std::cout.write(reinterpret_cast<char*>(data), size);
            std::cout << " | ";
        }
        std::cout << std::endl;
    }
}

运行上述C++程序后,我在控制台上看到的结果是这样的 -

e1HELLOe2HELLO |

但我正在寻找的是 - 将结果存储在 C++ 的地图中,以便键应该e1 and e2在地图中。并且它们的值应该HELLO在同一张地图中......然后迭代地图并在C++中打印出结果?这可能与我当前的代码有关吗?

如果是,任何人都可以提供一个简单的例子吗?谢谢。。。

我想这基本上是一个C++问题。只需检索数据并将其放入地图中...但是我面临的问题是我的背景完全是 Java 的,所以很难弄清楚如何做到这一点......

我不知道

libcql,我找不到任何文档。查看cql_result_t标题表明有一些函数可以确定有多少列以及如何访问它们。从外观上看,您只是复制了演示示例,这似乎不是一个特别好的演示。我会从改进print_result()函数开始,如下所示,看看我会得到什么。我的猜测是,您从查询中获得了"map"类型,并且您需要了解如何通过挖掘其标头来提取和使用相应的表示形式(除非在某处有一些文档)。下面的代码仅提取了一些类型,并且主要打印了处理相应类型所需的打印(假设它实际编译):

void print_result(cql::cql_result_t& result)
{
    std::size_t const columns(result.column_count());
    while (result.next()) {
        for (std::size_t column(0); column != columns; ++column) {
            cql::cql_column_type_enum type;
            if (result.column_type(column, type)) {
                switch (type) {
                case cql::CQL_COLUMN_TYPE_CUSTOM:
                    std::cout << "todo: process custom typen";
                    break;
                case cql::CQL_COLUMN_TYPE_ASCII:
                    std::cout << "todo: process ascii typen";
                    break;
                case cql::CQL_COLUMN_TYPE_BIGINT:
                    std::cout << "todo: process bigint typen";
                    break;
                case cql::CQL_COLUMN_TYPE_BLOB:
                    std::cout << "todo: process blob typen";
                    break;
                case cql::CQL_COLUMN_TYPE_BOOLEAN:
                    std::cout << "todo: process boolean typen";
                    break;
                case cql::CQL_COLUMN_TYPE_COUNTER:
                    std::cout << "todo: process counter typen";
                    break;
                case cql::CQL_COLUMN_TYPE_DECIMAL:
                    std::cout << "todo: process decimal typen";
                    break;
                case cql::CQL_COLUMN_TYPE_DOUBLE: {
                    double value;
                    if (result.get_double(column, value)) {
                        std::cout << "column=" << column << " "
                                  << "double=" << value << "n";
                    }
                    else {
                        std::cout << "failed to extract double for column "
                                  << column << "n";
                    }
                    break;
                case cql::CQL_COLUMN_TYPE_FLOAT: {
                    float value;
                    if (result.get_float(column, value)) {
                        std::cout << "column=" << column << " "
                                  << "float=" << value << "n";
                    }
                    else {
                        std::cout << "failed to extract float for column "
                                  << column << "n";
                    }
                    break;
                case cql::CQL_COLUMN_TYPE_INT: {
                    int value;
                    if (result.get_int(column, value)) {
                        std::cout << "column=" << column << " "
                                  << "int=" << value << "n";
                    }
                    else {
                        std::cout << "failed to extract int for column "
                                  << column << "n";
                    }
                    break;
                case cql::CQL_COLUMN_TYPE_TEXT: {
                    std::string value;
                    if (result.get_string(column, value)) {
                        std::cout << "column=" << column << " "
                                  << "text='" << value << "'n";
                    }
                    else {
                        std::cout << "failed to extract text for column "
                                  << column << "n";
                    }
                    break;
                case cql::CQL_COLUMN_TYPE_TIMESTAMP:
                    std::cout << "todo: process timestamp typen";
                    break;
                case cql::CQL_COLUMN_TYPE_UUID:
                    std::cout << "todo: process uiid typen";
                    break;
                case cql::CQL_COLUMN_TYPE_VARCHAR:
                    std::cout << "todo: process varchar typen";
                    break;
                case cql::CQL_COLUMN_TYPE_VARINT:
                    std::cout << "todo: process varint typen";
                    break;
                case cql::CQL_COLUMN_TYPE_TIMEUUID:
                    std::cout << "todo: process timeuuid typen";
                    break;
                case cql::CQL_COLUMN_TYPE_INET:
                    std::cout << "todo: process inet typen";
                    break;
                case cql::CQL_COLUMN_TYPE_LIST:
                    std::cout << "todo: process list typen";
                    break;
                case cql::CQL_COLUMN_TYPE_MAP:
                    std::cout << "todo: process map typen";
                    break;
                case cql::CQL_COLUMN_TYPE_SET:
                    std::cout << "todo: process set typen";
                    break;
                }
            }
        }
    }
}

cql_result_t有一个方法get_map。使用get_map而不是get_data:

cql::cql_result_t *r;
cql::cql_map_t *props = 0;
if (!r->get_map("records", &props)) {
   delete props;
   // throw an error
}
std::auto_ptr<cql::cql_map_t> p(props);
std::map<std::string, std::string> m;
for (std::size_t i = 0; i < p->size(); ++i) {
   std::string key;
   if (!p->get_key_string(i, key))
        // throw an error
   std::string value;
   if (!p->get_value_string(i, value))
        // throw an error
   m.insert(std::make_pair(key, value));
}
相关文章: