我已經開始用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 key
和HELLO 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所以有一點困難時期要弄清楚如何做到這一點...
你一定已經考慮過'std :: map'了吧? –
是的..這就是我在想或無序地圖,以及效率,因爲我正在閱讀,無序圖是有效的... – ferhan
所以你唯一的問題是如何拉'結果'來到'print_rows'到'std :: map ',對嗎? – P0W