将结果存储在 C++Map 中,然后迭代它,然后打印出来
Store the result in a Map in C++ and then iterate it and then print out?
我已经开始为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
和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
在同一张地图中......然后迭代地图并在C++中打印出结果?这可能与我当前的代码有关吗?
如果是,任何人都可以提供一个简单的例子吗?谢谢。。。
我想这基本上是一个C++问题。只需检索数据并将其放入地图中...但是我面临的问题是我的背景完全是 Java 的,所以很难弄清楚如何做到这一点......
libcql
,我找不到任何文档。查看cql_result_t
标题表明有一些函数可以确定有多少列以及如何访问它们。从外观上看,您只是复制了演示示例,这似乎不是一个特别好的演示。我会从改进print_result()
函数开始,如下所示,看看我会得到什么。我的猜测是,您从查询中获得了"map"类型,并且您需要了解如何通过挖掘其标头来提取和使用相应的表示形式(除非在某处有一些文档)。下面的代码仅提取了一些类型,并且主要打印了处理相应类型所需的打印(假设它实际编译):
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 typen";
break;
case cql::CQL_COLUMN_TYPE_ASCII:
std::cout << "todo: process ascii typen";
break;
case cql::CQL_COLUMN_TYPE_BIGINT:
std::cout << "todo: process bigint typen";
break;
case cql::CQL_COLUMN_TYPE_BLOB:
std::cout << "todo: process blob typen";
break;
case cql::CQL_COLUMN_TYPE_BOOLEAN:
std::cout << "todo: process boolean typen";
break;
case cql::CQL_COLUMN_TYPE_COUNTER:
std::cout << "todo: process counter typen";
break;
case cql::CQL_COLUMN_TYPE_DECIMAL:
std::cout << "todo: process decimal typen";
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 typen";
break;
case cql::CQL_COLUMN_TYPE_UUID:
std::cout << "todo: process uiid typen";
break;
case cql::CQL_COLUMN_TYPE_VARCHAR:
std::cout << "todo: process varchar typen";
break;
case cql::CQL_COLUMN_TYPE_VARINT:
std::cout << "todo: process varint typen";
break;
case cql::CQL_COLUMN_TYPE_TIMEUUID:
std::cout << "todo: process timeuuid typen";
break;
case cql::CQL_COLUMN_TYPE_INET:
std::cout << "todo: process inet typen";
break;
case cql::CQL_COLUMN_TYPE_LIST:
std::cout << "todo: process list typen";
break;
case cql::CQL_COLUMN_TYPE_MAP:
std::cout << "todo: process map typen";
break;
case cql::CQL_COLUMN_TYPE_SET:
std::cout << "todo: process set typen";
break;
}
}
}
}
}
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));
}
- 我编写了以下代码来读取C++矩阵,然后打印其行和列.我收到此错误
- 读取一组用户输入,按升序排序,然后打印结果
- 如何在循环中使用scanf,将值存储到一个变量中,然后打印出来?
- 如何将文件逐行读取到矢量中,然后打印矢量
- 如何打印第一个和最后一个元素的和,然后打印第二个和倒数第二个元素的总和,依此类推
- C++ 如何创建 2D 数组,将其传递给另一个函数,然后打印
- 在结构向量中搜索一个数据成员,然后打印匹配的所有数据成员
- 使用队列和列表 STL 进行C++分配。在将数据填充到列表或队列中然后打印该数据时遇到问题
- 尝试将二进制数组转换为十六进制C++然后打印结果
- 如何将一个数组的每个元素与另一个元素进行比较(如果存在),然后打印是,否则否
- 带有用户输入的C 文本数组,然后打印输入
- 如何在文件中搜索关键字,然后打印其中包含的字符串
- 从文件中读取结果数据,加载它,比较它,然后打印显示
- 将结果存储在 C++Map 中,然后迭代它,然后打印出来
- 如何将结果存储在 Map 中 C++,然后迭代它,然后打印出结果
- 用"*"(特殊字符)初始化向量,然后打印出来
- 如何用自己的值填充tm结构,然后打印出来
- 按字母顺序降序对列表进行排序,然后打印列表
- 在c++中找到文本中的单词,然后打印下一个特定的行
- 如何修改以下代码,以便我实际上将文件的内容读入数组,然后打印出数组?