如何在Cassandra中使用cqlsh在map <text,boolean>字段中插入自定义types?

我得到这样的错误:

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

插入自定义types对象的任何示例,其中一个字段的types为frozen<map<text, boolean>>

任何想法如何使用nodejs cassandra-driver做到这一点?

UPDATE

试图@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&lttext, boolean>); cqlsh:test> create table app(id uuid primary key, name text, defaultSetting frozen&ltsetting>); 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>
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&lttext, boolean>); cqlsh:test> create table app(id uuid primary key, name text, defaultSetting frozen&ltsetting>); 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> 

我将我的节点版本更新为“v0.12.4”。 但是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任何想法为什么'能力'不显示使用节点cassandra驱动程序?

这里是一个在cqlsh中的例子。 持有用户types的列必须标记为“ 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) 

我将采取你的问题的第二部分。

任何想法如何使用nodejs cassandra-driver做到这一点?

根据我在DataStax node.js关于集合的驱动程序文档中看到的内容 ,您应该使用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);