简体   繁体   English

Cassandra:使用node.js将二进制Blob写入表

[英]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. 我试图让我的node.js应用程序读取一个文件,然后将其添加到表中,但我无法弄清楚。

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: 这是node.js代码:

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 ). Cassandra驱动程序需要仅使用文件名和处理程序调用fs.readFile而不返回编码时返回的Buffer类型(请参阅fs.readFile docs )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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