2014-10-31 82 views
5

我想cassandra節點驅動程序,插入一條記錄時卡住的問題,它看起來像cassandra驅動程序無法插入浮點值。ResponseError:預期的4或0字節int

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

參數傳遞給​​是

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

其中

var locx = 32.09; 
var locy = 54.90; 

,並調用執行LO OKS,如:

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

PS 一些意見,而試圖理解這個問題:

  1. 因爲它似乎,問題是浮動,所以我改變float類型(locX,locY的)爲int並重新運行代碼。同樣的錯誤依然存在。因此,浮動CQL類型不是特別關聯的問題。
  2. 接下來,我試圖從INSERT查詢中刪除所有int,並試圖只插入非數字值。此嘗試成功地將值輸入到db中。因此現在看起來就是this problem may be associated with numeric types

回答

17

下列詞語是因爲它是從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);

如果您正在處理批量更新,那麼this issue可能是您的興趣。

+1

在答案很好的細節,很好,你指出批處理問題也。 – jorgebg 2014-11-03 08:42:28

+2

第一種方法適用於我,謝謝 – 2016-03-11 13:16:54

相關問題