ResponseError:期望4或0字节int [英] ResponseError : Expected 4 or 0 byte int

查看:287
本文介绍了ResponseError:期望4或0字节int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试cassandra节点驱动程序,并插入一个记录卡问题,看起来像cassandra驱动程序不能插入浮点值。

 问题:当在db中传递int值时,api给出以下错误:
Debug:hapi,internal,implementation,error
ResponseError:期望4或0字节int b $ b在FrameReader.readError(/home/gaurav/Gaurav-Drive/code/nodejsWorkspace/cassandraTest/node_modules/cassandra-driver/lib/readers.js:291:13)
在Parser.parseError(/ home /gaurav/Gaurav-Drive/code/nodejsWorkspace/cassandraTest/node_modules/cassandra-driver/lib/streams.js:185:45)
在Parser.parseBody(/ home / gaurav / Gaurav-Drive / code / nodejsWorkspace /cassandraTest/node_modules/cassandra-driver/lib/streams.js:167:19)
在Parser._transform(/ home / gaurav / Gaurav-Drive / code / nodejsWorkspace / cassandraTest / node_modules / cassandra-driver / lib /streams.js:101:10)
在Parser.Transform._read(_stream_transform.js:179:10)
在Parser.Transform._write(_stream_transform.js:167:12)
at doWrite(_stream_writable.js:225:10)
at writeOrBuffer(_stream_writable.js:215:5)
在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'); $ <$> $

/ p>

  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> ;,
interest set< varchar> ;,
locX float,
locY float,
city varchar,
favorite map< varchar,varchar>,
PRIMARY KEY(uid)
);

PS
尝试理解问题时的一些意见:


  1. 因为看起来,问题是float,所以我把float(locX,locY)类型改为int并重新运行代码。相同的错误仍然存​​在。因此,它不是特别与浮动CQL类型相关联的问题。

  2. 接下来,我尝试从INSERT查询中删除所有int,并尝试只插入非数字值。此尝试成功地将值输入db。因此,现在看来,此问题可能与数字类型相关联。


解决方案

以下词语是从cassandra节点驱动程序数据类型文档



在编码数据时,在使用参数正常执行时,驱动程序会尝试猜测目标类型基于输入类型。考虑下面的例子:


$ b(
$ b)

$ b

  var key = 1000; 
client.execute('SELECT * FROM table1其中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);

如果您正在处理批量更新, https://datastax-oss.atlassian.net/browse/NODEJS-25?jql=text%20%20%22ResponseError%22\">此问题可能是您的兴趣。


I am trying cassandra node driver and stuck in problem while inserting a record, it looks like cassandra driver is not able to insert float values.

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)

I am trying to execute following query from code:

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');

parameter passed to execute() is

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

where

var locx = 32.09;
var locy = 54.90;

and call to execute looks like:

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 Some observations while trying to understand the issue:

  1. Since it seems, problem is with float so i changed type float (of locX, locY) to int and re-run the code. Same error persist. Hence, it is not problem associated specifically to float CQL type.
  2. Next, i attempted to remove all int from the INSERT query and attempted to insert only non-numeric values. This attempt successfully inputted the value into db. Hence it looks like now that, this problem may be associated with numeric types.

解决方案

Following words are as it is picked from cassandra node driver data type documentation

When encoding data, on a normal execute with parameters, the driver tries to guess the target type based on the input type. Values of type Number will be encoded as double (as Number is double / IEEE 754 value).

Consider the following example:

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

If the key column is of type int, the execution fails. There are two possible ways to avoid this type of problem:

  1. Prepare the data (recommended) - prepare the query before execution

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

  2. Hinting the target types - Hint: the first parameter is an integer`

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

If you are dealing with batch update then this issue may be of your interest.

这篇关于ResponseError:期望4或0字节int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆