如何使用nodejs在cassandra中插入媒体文件?

时间:2019-05-31 08:03:25

标签: node.js cassandra

我们如何使用节点js将图像插入Cassandra数据库 我尝试了以下方法。

我已经使用base64(image)来存储图像,但是我需要将base64存储在Cassandra的blob数据类型中。我知道Cassandra中的blob数据类型等同于节点js中的Buffer数据类型。


var cassandra=require('cassandra-driver');
var client=new cassandra.Client({contactPoints:['localhost'],localDataCenter:"datacenter1",keyspace:"employee"});
var path=require('path');
var fs=require('fs');
var base64 = fs.readFileSync(path.resolve(__dirname + '/download.jpeg'), 'base64');

var imageBuffer = new Buffer(base64, 'base64');





var query="INSERT INTO photo(id,image) VALUES ('ranjith',imageBuffer)";

client.execute(query,function(err){
   if(err) {
      console.log(err);
   } else {
      console.log('success');
   }
});

这是错误消息:

> { ResponseError: line 1:57 no viable alternative at input ')'
> (...id,image) VALUES ('ranjith',[imageBuffer]))
>     at FrameReader.readError (/home/innvoot/Desktop/cassandra_and_node_trails/node_modules/cassandra-driver/lib/readers.js:326:15)
>     at Parser.parseBody (/home/innvoot/Desktop/cassandra_and_node_trails/node_modules/cassandra-driver/lib/streams.js:194:66)
>     at Parser._transform (/home/innvoot/Desktop/cassandra_and_node_trails/node_modules/cassandra-driver/lib/streams.js:137:10)
>     at Parser.Transform._read (_stream_transform.js:186:10)
>     at Parser.Transform._write (_stream_transform.js:174:12)
>     at doWrite (_stream_writable.js:396:12)
>     at writeOrBuffer (_stream_writable.js:382:5)
>     at Parser.Writable.write (_stream_writable.js:290:11)
>     at Protocol.ondata (_stream_readable.js:639:20)
>     at emitOne (events.js:116:13)   name: 'ResponseError',   info: 'Represents an error message from the server',   message: 'line 1:57
> no viable alternative at input \')\' (...id,image) VALUES
> (\'ranjith\',[imageBuffer]))',   code: 8192,   coordinator:
> '127.0.0.1:9042',   query: 'INSERT INTO photo(id,image) VALUES
> (\'ranjith\',imageBuffer)' }

我希望结果是“成功”。

1 个答案:

答案 0 :(得分:0)

我不是javascript专家,但看到您的错误;

  

输入\'] \'(... id,image)值(\'ranjith \',[imageBuffer]))'

我认为查询语法可能是错误的,可以尝试

const query = 'INSERT into photo (id,image) VALUES (?,?)';

client.execute(query, [ 'ranjith', imageBuffer ], function(err, result) {
  assert.ifError(err);
  console.log('Success');
});