如何在Cassandra中使用cqlsh插入带有map <text,boolean =“”>字段的自定义类型?

时间:2015-06-09 13:37:52

标签: node.js cassandra cqlsh

我收到这样的错误:

field is not of type frozen<map<text, boolean>>

用于插入字段为frozen<map<text, boolean>>类型的自定义类型对象的任何示例?

任何想法如何使用nodejs cassandra-driver?

更新

试图@Olivier Michallat提到。但我的答案如下。知道为什么会这样而不是josn吗?

cqlsh:test> create type t(m map<text, boolean>);
cqlsh:test> create table foo(k int primary key, v frozen<t>);
cqlsh:test> insert into foo(k,v) values (1, {m: {'foo': true}});
cqlsh:test> select * from foo;

 k | v
---+-------------------------------------------------------------------------
 1 | \x00\x00\x00\x10\x00\x00\x00\x01\x00\x00\x00\x03foo\x00\x00\x00\x01\x01

(1 rows)

更新2

感谢@Olivier Michallat,最新的cqlsh给了我预期的结果。


amt@amt-db:~$ docker run -it --rm --link cass1:cass poklet/cassandra cqlsh cass
Connected to Test Cluster at cass:9042.
[cqlsh 5.0.1 | Cassandra 2.1.5 | CQL spec 3.2.0 | Native protocol v3]
Use HELP for help.
cqlsh> use test;
cqlsh:test> create type setting(timeout timestamp, capabilities map<text, boolean>);
cqlsh:test> create table app(id uuid primary key, name text, defaultSetting frozen<setting>);
cqlsh:test> insert into app(id, name, defaultSetting) values (7031d49e-70cf-450d-90d8-178fa97c5e67, 'Test Cassandra', {timeout: 30, capabilities: {'read': true, 'write': false, 'delete': false}});
cqlsh:test> select * from app;

 id                                   | defaultsetting                                                                                       | name
--------------------------------------+------------------------------------------------------------------------------------------------------+----------------
 7031d49e-70cf-450d-90d8-178fa97c5e67 | {timeout: '1970-01-01 00:00:00+0000', capabilities: {'delete': False, 'read': True, 'write': False}} | Test Cassandra

(1 rows)
cqlsh:test> 

我将节点版本更新为&#34; v0.12.4&#34;。但是nodejs cassandra-driver没有给我预期的响应。

//filename: udt.js
var cassandra = require('cassandra-driver');

var options = {
    contactPoints: ["192.168.2.203"],
    keyspace: "test",
    encoding: {
        map: Map,
        set: Set
    }
};
var client = new cassandra.Client(options);

client.connect(function (err) {
    if(err){
        console.info("connect err", err);
        return;
    }
    var query = 'select * from app';
    client.execute(query, function(err, result){
        if (err) {
            console.info("query err", err);
            return;
        }
        console.info(result.rows);
    });
});

查看结果


manu@kochi:~/app/test$ node udt.js 
[ { id: Uuid: 7031d49e-70cf-450d-90d8-178fa97c5e67,
    defaultsetting: 
     { timeout: Thu Jan 01 1970 05:30:00 GMT+0530 (IST),
       capabilities: {} },
    name: 'Test Cassandra' } ]

@ BryceAtNetwork23任何想法为什么&#39;功能&#39;没有使用节点cassandra-driver显示?

2 个答案:

答案 0 :(得分:1)

这是cqlsh中的一个例子。保存用户类型的列必须标记为frozen(表示Cassandra将其序列化为单个值):

cqlsh:test> create type t(m map<text, boolean>);
cqlsh:test> create table foo(k int primary key, v frozen<t>);
cqlsh:test> insert into foo(k,v) values (1, {m: {'foo': true}});
cqlsh:test> select * from foo;

 k | v
---+--------------------
 1 | {m: {'foo': True}}

(1 rows)

答案 1 :(得分:1)

我会接受你问题的第二部分。

  

任何想法如何使用nodejs cassandra-driver?

根据我在DataStax node.js driver documentation on collections中看到的内容,您应该使用Javascript对象作为地图值(使用与Oliver指示的类似的命名约定):

var key = 1;
var m = {
   foo: true
};
var query = 'UPDATE foo SET v = ? WHERE k = ?';
client.execute(query, [m, key], { prepare: true}, callback);