将结果存储在C++中的地图中,然后迭代,然后打印出来? 我已经开始用C++的卡桑德拉的LBCQL库工作了。我试图用C++检索卡桑德拉的数据,用LICBQL库…< /P>

将结果存储在C++中的地图中,然后迭代,然后打印出来? 我已经开始用C++的卡桑德拉的LBCQL库工作了。我试图用C++检索卡桑德拉的数据,用LICBQL库…< /P>,c++,map,cassandra,libcql,C++,Map,Cassandra,Libcql,每当我使用cqlsh在命令行上进行选择时,都会像这样选择- select records from profile_user where user_id = '1'; e1HELLOe2HELLO | 我总是在cql命令行上得到下面的输出,其中records列实际上是一个map,其中key是e1,value是HELLO。同样,key是e2,value是HELLO。。当我在CQL中创建表时,我使用CQL的收集功能创建了记录作为映射 records ---------------------

每当我使用cqlsh在命令行上进行选择时,都会像这样选择-

 select records from profile_user where user_id = '1';
e1HELLOe2HELLO |
我总是在cql命令行上得到下面的输出,其中records列实际上是一个map,其中key是e1,value是HELLO。同样,key是e2,value是HELLO。。当我在CQL中创建表时,我使用CQL的收集功能创建了记录作为映射

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

现在进入C++世界-< /P> 现在我试图从C++ LICBCL库中检索同样的东西…我将运行相同的上述选择查询在C++中,我想返回一个地图,将有E1,E2作为关键和你好,因为有价值在地图…用C++ +< /p>做这件事是可能的吗?

/**
 * 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++程序-< /P>后在控制台上打印结果的方法

/**
 * 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++中的地图中,这样键应该是E1和E2在地图上。在同一个映射中,它们的值应该是HELLO。。。然后迭代地图并打印结果到C++?这可能与我现有的代码有关吗

如果是,有人能提供一个简单的例子吗?谢谢

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

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

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 type\n";
                    break;
                case cql::CQL_COLUMN_TYPE_ASCII:
                    std::cout << "todo: process ascii type\n";
                    break;
                case cql::CQL_COLUMN_TYPE_BIGINT:
                    std::cout << "todo: process bigint type\n";
                    break;
                case cql::CQL_COLUMN_TYPE_BLOB:
                    std::cout << "todo: process blob type\n";
                    break;
                case cql::CQL_COLUMN_TYPE_BOOLEAN:
                    std::cout << "todo: process boolean type\n";
                    break;
                case cql::CQL_COLUMN_TYPE_COUNTER:
                    std::cout << "todo: process counter type\n";
                    break;
                case cql::CQL_COLUMN_TYPE_DECIMAL:
                    std::cout << "todo: process decimal type\n";
                    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 type\n";
                    break;
                case cql::CQL_COLUMN_TYPE_UUID:
                    std::cout << "todo: process uiid type\n";
                    break;
                case cql::CQL_COLUMN_TYPE_VARCHAR:
                    std::cout << "todo: process varchar type\n";
                    break;
                case cql::CQL_COLUMN_TYPE_VARINT:
                    std::cout << "todo: process varint type\n";
                    break;
                case cql::CQL_COLUMN_TYPE_TIMEUUID:
                    std::cout << "todo: process timeuuid type\n";
                    break;
                case cql::CQL_COLUMN_TYPE_INET:
                    std::cout << "todo: process inet type\n";
                    break;
                case cql::CQL_COLUMN_TYPE_LIST:
                    std::cout << "todo: process list type\n";
                    break;
                case cql::CQL_COLUMN_TYPE_MAP:
                    std::cout << "todo: process map type\n";
                    break;
                case cql::CQL_COLUMN_TYPE_SET:
                    std::cout << "todo: process set type\n";
                    break;
                }
            }
        }
    }
}

cql_result_t有一个方法get_map。使用get_映射而不是get_数据:

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

你一定考虑过std::map,对吧?是的。。这就是我所想的,或者说无序映射也是为了提高效率,因为我读到无序映射是有效的…所以你唯一的问题是如何将打印行的结果拉到std::map中,对吗?是的,没错。。。但是它应该是这样的,在map中,key应该是e1,hello应该是它的值。。同样,键应该是e2,hello它的值也是…我不熟悉cql库,但看起来像是您的数据,即e1HELLOe2HELLO需要使用一些文本解析或简单地使用cql库中的一些函数进行拆分,一旦您获得id和值等数据,把它们放到地图上非常简单。是的,没有那么多文档。。但这是同一个libcql库的github。。让我编译一下,看看它是怎么回事。。