【问题标题】:How to bind to a collection of UDT in Cassandra C++ driver如何绑定到 Cassandra C++ 驱动程序中的 UDT 集合
【发布时间】:2018-01-12 22:44:54
【问题描述】:

我有一个表,其中有一列类型为 LIST<FROZEN<custom_type>,其中 custom_type 是 UDT,用户定义类型 (CREATE TYPE custom_type...)。

在 Datastax C++ 驱动程序中,如何将值绑定到该列?

我尝试过:

CassDataType* udt = cass_keyspace_meta_user_type_by_name("keyspace_name", "custom_type");
CassCollection* coll = cass_collection_new_from_data_type("custom_type", 5);
for (int ii = 0; ii < 5; ++ii)
{
    CassUserType* usr = cass_user_type_new_from_data_type(udt);
    cass_user_type_set_string_by_name(usr, "udt_name_1", data[ii].stringvalue);
    cass_user_type_set_int32_by_name(usr, "udt_name_2", data[ii].intvalue);
    cass_collection_append_user_type(coll, usr);
    cass_user_type_free(usr);
}
cass_statement_bind_collection_by_name(stmt, "column_name", coll);

此代码在第二行失败,cass_collection_new_from_data_type() 返回 NULL。 udt 不为 NULL。

cass_collection_new_from_data_type()的源码看,如果参数是!is_collection(),这个函数返回NULL,我的显然不是。那么这个函数cass_collection_new_from_data_type() 应该这样做吗?我应该如何获取要绑定的 CassCollection*?

谢谢。

【问题讨论】:

    标签: c++ cassandra datastax


    【解决方案1】:

    您需要指定真正的集合类型,而不是您的用户定义类型:

        CassCollection* phone = cass_collection_new(CASS_COLLECTION_TYPE_LIST, 5);
    

    其余部分保持原样。以examples/udt/udt.c 为例。

    【讨论】:

    猜你喜欢
    • 2017-08-22
    • 1970-01-01
    • 2015-12-01
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 2018-12-17
    相关资源
    最近更新 更多