简体   繁体   中英

Cassandra: Writing binary blob to table with node.js

I am trying to have my node.js app read a file and then add it to a table but I can not figure it out.

Here is the table info

CREATE TABLE tracking.tracking_data (
first_name text,
last_name text,
timestamp timestamp,
heat double,
location text,
m blob,
speed double,
telepathy_powers int,
PRIMARY KEY ((first_name, last_name), timestamp)

Here is the node.js code:

var populateData = function(first_name, last_name, timestamp, heat, location, speed, telepathy_powers, file) {
fs.readFile(file, 'binary', function(err, data) {
if (err) {
return console.log(err);
} else {
var client = new cassandra.Client({
contactPoints: ['node1', 'node2', 'node3'],
keyspace: 'tracking'
});
var query = 'INSERT INTO tracking_data (first_name,last_name,timestamp,heat,location,m,speed,telepathy_powers) VALUES (?,?,?,?,?,?,?,?);';
const parms = [first_name, last_name, timestamp, heat, location, data, speed, telepathy_powers];
client.execute(query, parms, {
prepare: true
}, function(err, result) {
if (err) {
console.log('\n' + err);
}
});
}
});

The error is:

TypeError: Not a valid blob, expected Buffer obtained 'root:x:0:0:root:/root:/bin/ash\nbin:x:1:1:bin:/bin:/sbin/nologin\ndaemon:x:2:2:daemon:/sbin:/sbin/nologin\nadm:x:3:4:adm:/var/adm:/sbin/nologin\nlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\nsync:x:5:0:sync:/sbin:/bin/sync\nshutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\nhalt:x:7:0:halt:/sbin:/sbin/halt\nmail:x:8:1

I suspect that the problem is because you specified the encoding for file, even although it's binary. Cassandra driver needs Buffer type that is returned when fs.readFile is called only with file name & handler, without encoding (see fs.readFile docs ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM