将结果存储在C ++中的映射,然后迭代它,然后打印出来? [英] Store the result in a Map in C++ and then iterate it and then print out?

查看:128
本文介绍了将结果存储在C ++中的映射,然后迭代它,然后打印出来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用 C ++ libcql 库为Cassandra ..我试图从Cassandra使用C + + libcql库..检索数据



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

 从profile_user中选择记录,其中user_id ='1'; 

我总是得到下面的输出在cql命令行和其中记录列实际上是映射,其中键是 e1 ,值是 HELLO 。同样,键是 e2 ,值也是 HELLO 。当我在CQL中创建表时,记录作为我使用CQL的收集功能的地图。

 记录
------- -------------------------
{'e1':'HELLO','e2':'HELLO'}



现在来到C ++世界 -



现在我试图从 C ++ libcql库检索同样的事情...我将运行上面的选择查询在C + +,我想返回一个映射将具有 e1,e2作为键 HELLO作为该映射内的值 ...可能在C ++中这样做?

  / ** 
*此方法将从Cassandra中检索数据。
*然后调用print_rows方法在控制台上打印出来
* /
void get_attributes(string id){
try {

// some code

//连接打开
connection_open();

execute_query(USE testks;);

//这将给我select查询的结果
cql_result_t& result = execute_query(select * from profile_user where key ='+ id +';);

//这是在控制台上打印出来的
print_rows(result);

//一些代码
} catch(int e){
//这里的代码
}
}

下面是在运行我的C ++程序后在控制台上输出结果的方法 -

  / ** 
*此方法打印出控制台上的结果。*
*
* /
void print_rows(cql :: cql_result_t& result){
while(result.next()){
for(size_t i = 0; 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和e2 在地图..它们的值应该 HELLO 在同一个Map中...然后迭代地图并打印出C ++中的结果?这是否可能与当前的代码有关?



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



这是一个基本的C ++问题我想..只是检索数据,并把它放入地图...但我面临的问题是我背景是完全在Java,所以有点难的时间来弄清楚如何做这个...

解决方案

我不知道 libcql ,我找不到任何文档。查看 cql_result_t 表示有函数来确定有多少列和如何访问它们。从它的外观,你只是复制的演示示例,似乎不是一个特别好的演示。我将首先细化 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\\\
;
break;
case cql :: CQL_COLUMN_TYPE_ASCII:
std :: cout<< todo:process ascii type\\\
;
break;
case cql :: CQL_COLUMN_TYPE_BIGINT:
std :: cout<< todo:process bigint type\\\
;
break;
case cql :: CQL_COLUMN_TYPE_BLOB:
std :: cout<< todo:process blob type\\\
;
break;
case cql :: CQL_COLUMN_TYPE_BOOLEAN:
std :: cout<< todo:process boolean type\\\
;
break;
case cql :: CQL_COLUMN_TYPE_COUNTER:
std :: cout<< todo:process counter type\\\
;
break;
case cql :: CQL_COLUMN_TYPE_DECIMAL:
std :: cout<< todo:process decimal type\\\
;
break;
case cql :: CQL_COLUMN_TYPE_DOUBLE:{
double value;
if(result.get_double(column,value)){
std :: cout<< column =<列<<
<< double =<<值<< \\\

}
else {
std :: cout< failed for extract double for column
<<列<< \\\
;
}
break;
case cql :: CQL_COLUMN_TYPE_FLOAT:{
float value;
if(result.get_float(column,value)){
std :: cout<< column =<列<<
<< float =<<值<< \\\
;
}
else {
std :: cout< failed for extract float for column
<<列<< \\\
;
}
break;
case cql :: CQL_COLUMN_TYPE_INT:{
int value;
if(result.get_int(column,value)){
std :: cout<< column =<列<<
<< int =<<值<< \\\
;
}
else {
std :: cout< failed for extract int for column
<<列<< \\\
;
}
break;
case cql :: CQL_COLUMN_TYPE_TEXT:{
std :: string value;
if(result.get_string(column,value)){
std :: cout<< column =<列<<
<< text ='<<值<< '\\\
;
}
else {
std :: cout< 无法提取列
<<列<< \\\
;
}
break;
case cql :: CQL_COLUMN_TYPE_TIMESTAMP:
std :: cout<< todo:process timestamp type\\\
;
break;
case cql :: CQL_COLUMN_TYPE_UUID:
std :: cout<< todo:process uiid type\\\
;
break;
case cql :: CQL_COLUMN_TYPE_VARCHAR:
std :: cout<< todo:process varchar type\\\
;
break;
case cql :: CQL_COLUMN_TYPE_VARINT:
std :: cout<< todo:process varint type\\\
;
break;
case cql :: CQL_COLUMN_TYPE_TIMEUUID:
std :: cout<< todo:process timeuuid type\\\
;
break;
case cql :: CQL_COLUMN_TYPE_INET:
std :: cout<< todo:process inet type\\\
;
break;
case cql :: CQL_COLUMN_TYPE_LIST:
std :: cout<< todo:process list type\\\
;
break;
case cql :: CQL_COLUMN_TYPE_MAP:
std :: cout<< todo:process map type\\\
;
break;
case cql :: CQL_COLUMN_TYPE_SET:
std :: cout<< todo:process set type\\\
;
break;
}
}
}
}
}


I have started working with C++ libcql library for Cassandra.. I am trying to retrieve data from Cassandra using C++ with libcql library..

Whenever I go on the command line using cqlsh and do select like this -

 select records from profile_user where user_id = '1';

I always get the below output on the cql command line and in which records column is actually a map in which key is e1 and value is HELLO. In the same way key is e2 and value is HELLO again.. When I created the table in CQL, I created records as the map as I was using collection feature of CQL..

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

Now Coming to C++ world-

Now I am trying to retrieve the same thing from the C++ libcql library... I will be running the same above select query in C++ and I want to return a map which will have e1, e2 as the key and HELLO as there value inside that map... It is possible to do it in 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
    }
}

Below is the method which will print out the results on the console after running my C++ program -

/**
 * 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;
    }
}

The result that I see on the console after running my above C++ program is something like this -

e1HELLOe2HELLO |

But what I am looking for is - Store the result in a Map in C++, in such a way such that key should be e1 and e2 in the Map.. And the value for them should be HELLO in the same Map... And then iterate the Map and print out the result in C++? Is this possible to do with the current code I have?

If yes, can anyone provide a simple example on this? Thanks...

It is basically a C++ question I guess.. Just retrieve the data and put it into the Map... But the problem I am facing is my background is totally in Java so having little bit hard time to figure out how to do that...

解决方案

I don't know libcql and I failed to locate any documentation. Looking at the header for cql_result_t indicates that there are functions to determine how many columns there are and how to access them. From the looks of it, you merely copied the demo example which doesn't seem to be a particular good demo. I would start off with refining the print_result() function to look something like the below and see what I'd get. My guess is that you get a "map" type from your query and you'll need to see how to extract and use the corresponding representation by digging through their headers (unless there is somewhere some documentation). The code below mere extracts a few types and mostly prints that it needs to deal with processing the respective type (assuming it actually compiles):

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

这篇关于将结果存储在C ++中的映射,然后迭代它,然后打印出来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆