2013-10-13 19 views
0

我已经开始用C++ libcql库卡珊德拉的工作..我想用C++与libcql库从卡桑德拉检索数据..将结果存储在C++中的Map中,然后迭代它然后打印出来?

每当我去使用cqlsh在命令行上和不选择这样的 -

select records from profile_user where user_id = '1'; 

我总是在定制列表命令行和其下面的输出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在同一个Map中...然后迭代Map并在C++中输出结果?这可能与我现有的代码有关吗?

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

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

+0

你一定已经考虑过'std :: map'了吧? –

+0

是的..这就是我在想或无序地图,以及效率,因为我正在阅读,无序图是有效的... – ferhan

+0

所以你唯一的问题是如何拉'结果'来到'print_rows'到'std :: map ',对吗? – P0W

回答

1

我不知道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\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; 
       } 
      } 
     } 
    } 
} 
+0

是的没有那么多的文档..但这是github [link](https://github.com/mstump/libcql)为同一个libcql库。让我编译这些,看看它是怎么回事.. – ferhan

0

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)); 
} 
相关问题