我已经开始为Cassandra使用C++ libcql
库了。我正在尝试使用带有libcql库的C ++从Cassandra中检索数据..
每当我使用cqlsh
继续使用命令行并选择这样 -
select records from profile_user where user_id = '1';
我总是在cql命令行上获得以下输出,其中records
列实际上是map
,其中键为e1
且值为HELLO
。以同样的方式,键是e2
,值又是HELLO
。当我在CQL中创建表时,我创建了记录作为地图,因为我正在使用CQL的集合功能。
records
--------------------------------
{'e1': 'HELLO', 'e2': 'HELLO'}
现在来到C ++世界 -
现在我正在尝试从C++ libcql library
中检索相同的内容...我将在C ++中运行相同的上述选择查询,我想返回一个具有e1, e2 as the key
和{的地图{1}} ...可以在C ++中完成吗?
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 ++中的Map中,使得Map中的键应为e1HELLOe2HELLO |
..并且它们的值应为{ {1}}在同一个Map中...然后迭代Map并用C ++打印出结果?这可能与我当前的代码有关吗?
如果是,有人可以提供一个简单的例子吗?感谢...
这基本上是一个C ++问题我想..只是检索数据并将其放入Map ...但我面临的问题是我的背景完全是Java,所以有点难以弄清楚如何那样做......
答案 0 :(得分: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;
}
}
}
}
}
答案 1 :(得分: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));
}