2013-10-13 17 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)); 
} 
相關問題