ResponseError:预期4或0字节int

时间:2014-10-31 20:26:12

标签: node.js cassandra cassandra-2.0

我正在尝试使用cassandra节点驱动程序并在插入记录时遇到问题,看起来cassandra驱动程序无法插入浮点值。

Problem: When passing int value for insertion in db, api gives following error:
    Debug: hapi, internal, implementation, error 
        ResponseError: Expected 4 or 0 byte int (8)
        at FrameReader.readError (/home/gaurav/Gaurav-Drive/code/nodejsWorkspace/cassandraTest/node_modules/cassandra-driver/lib/readers.js:291:13)
        at Parser.parseError (/home/gaurav/Gaurav-Drive/code/nodejsWorkspace/cassandraTest/node_modules/cassandra-driver/lib/streams.js:185:45)
        at Parser.parseBody (/home/gaurav/Gaurav-Drive/code/nodejsWorkspace/cassandraTest/node_modules/cassandra-driver/lib/streams.js:167:19)
        at Parser._transform (/home/gaurav/Gaurav-Drive/code/nodejsWorkspace/cassandraTest/node_modules/cassandra-driver/lib/streams.js:101:10)
        at Parser.Transform._read (_stream_transform.js:179:10)
        at Parser.Transform._write (_stream_transform.js:167:12)
        at doWrite (_stream_writable.js:225:10)
        at writeOrBuffer (_stream_writable.js:215:5)
        at Parser.Writable.write (_stream_writable.js:182:11)
        at write (_stream_readable.js:601:24)

我正在尝试从代码中执行以下查询:

INSERT INTO ragchews.user
(uid ,iid ,jid ,jpass ,rateCount ,numOfratedUser ,hndl ,interests ,locX ,locY ,city )
VALUES
('uid_1',{'iid1'},'jid_1','pass_1',25, 10, {'NEX1231'}, {'MUSIC'}, 21.321, 43.235, 'delhi');
传递给execute()

参数是

var params = [uid, iid, jid, jpass, rateCount, numOfratedUser, hndl, interest, locx, locy, city];

,其中

var locx = 32.09;
var locy = 54.90;

并调用执行如下:

var addUserQuery = 'INSERT INTO ragchews.user (uid ,iid ,jid ,jpass ,rateCount ,numOfratedUser ,hndl ,interests ,locX ,locY ,city) VALUES (?,?,?,?,?,?,?,?,?,?,?);';
var addUser = function(user, cb){
    console.log(user);
    client.execute(addUserQuery, user, function(err, result){
        if(err){
            throw err;
        }
        cb(result);
    });
};

CREATE TABLE ragchews.user( 
    uid varchar,    
    iid set<varchar>,   
    jid varchar,    
    jpass varchar,  
    rateCount int,  
    numOfratedUser int, 
    hndl set<varchar>,  
    interests set<varchar>, 
    locX float, 
    locY float, 
    city varchar,   
    favorite map<varchar, varchar>, 
    PRIMARY KEY(uid)
);

P.S 在尝试理解问题时的一些观察结果:

  1. 因为看起来问题是浮点数所以我将类型float(locX,locY)更改为int并重新运行代码。同样的错误仍然存​​在。因此,它不是浮动CQL类型特别相关的问题。
  2. 接下来,我尝试从INSERT查询中删除所有int,并尝试仅插入非数字值。此尝试成功将值输入db。因此现在看来,this problem may be associated with numeric types

1 个答案:

答案 0 :(得分:27)

以下字词是从cassandra node driver data type documentation

中挑选出来的

对数据进行编码时,在使用参数执行的正常操作中,驱动程序会尝试根据输入类型猜测目标类型。 Number类型的值将被编码为double(因为Number是double / IEEE 754值)。

考虑以下示例:

var key = 1000;
client.execute('SELECT * FROM table1 where key = ?', [key], callback);

如果键列的类型为int,则执行失败。有两种方法可以避免此类问题:

  1. 准备数据(推荐) - 在执行前准备查询

    client.execute('SELECT * FROM table1 where key = ?', [key], { prepare : true }, callback);

  2. 提示目标类型 - 提示:第一个参数是整数。

    client.execute('SELECT * FROM table1 where key = ?', [key], { hints : ['int'] }, callback);

  3. 如果您正在处理批量更新,那么this issue可能是您感兴趣的。