2014-11-04 83 views
2

我正在尝试在Cassandra中使用用户定义的类型和nodejs。 按照文档,我成功地设法在cqlsh中执行它(https://www.datastax.com/documentation/cql/3.1/cql/cql_using/cqlUseUDT.html)。我设法在cqlsh中插入项目,但不使用cassandra-driver模块。在Cassandra中使用用户定义的类型和nodejs

我明白他们是一些错综复杂的JavaScript(http://www.datastax.com/documentation/developer/nodejs-driver/1.0/nodejs-driver/reference/nodejs2Cql3Datatypes.html)的使用,我尝试了不同的选项提供,但我无法得到它的工作。

这里是我的考验及其产生的错误信息:

var insertQuery = 'INSERT INTO mykeyspace.users (id, name) VALUES (?, ?)' 

client.execute(insertQuery, ['62c36092-82a1-3a00-93r1-46196ee77204', { firstname: 'paul', lastname: 'jacques'}], {hints: ['uuid', 'map<text, text>']}, function(err) { console.log(err);}) 

{ name: 'ResponseError', message: 'Not enough bytes to read 0th field java.nio.HeapByteBuffer[pos=0 lim=9 cap=9]', info: 'Represents an error message from the server', code: 8704, query: 'INSERT INTO mykeyspace.users (id, name) VALUES (?, ?)' }

client.execute(insertQuery, ['62c36092-82a1-3a00-93r1-46196ee77204', { firstname: 'paul', lastname: 'jacques'}], { prepare: true }, function(err) { console.log(err);}) 

    { [TypeError: Not a valid blob, expected Buffer obtained { firstname: 'paul', lastname: 'jacques' }] 
    query: 'INSERT INTO mykeyspace.users (id, name) VALUES (?, ?)' } 

那么什么是正确的语法来得到它的工作?

注意:我正在使用cassandra 2.1.1

回答

2

DataStax Node.js驱动程序不支持用户定义的类型,所以驱动程序将无法相应地序列化数据。

您可以在CQL中使用json符号来访问各个字段。例如,像下面的查询将工作:

SELECT addr.street, addr.city FROM location WHERE id=?; 

UPDATE SET addr['street'] = ?, addr['city'] = ? WHERE id = ? 
+0

谢谢,什么是存储的嵌套JSON对象则首选方法是什么?我目前使用blob,但这使得查询缓冲列不可能,并更新更昂贵... – Spearfisher 2014-11-04 15:14:00

+0

冻结的UDTs是用于小对象,如果你能使它与我包含在我的答案中的json符号一起工作,你应该使用它(稍后你可以使用驱动程序级别udt)。 如果您需要大型对象,则可以使用多个列,或者应该为其使用不同的列族。 – jorgebg 2014-11-05 13:10:58