Vasantha Muddam
Vasantha Muddam

Reputation: 3

how to insert media files in cassandra using nodejs?

How we insert an image into a Cassandra database using node js I have tried with following.

I have used base64(image) to store the image, but I need store base64 in blob datatype of Cassandra. I know the blob datatype in Cassandra is equivalent to Buffer datatype in node js.


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

Here is the error message:

> { 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)' }

I expect to get "SUCCESS" as the result.

Upvotes: 0

Views: 329

Answers (1)

Wizard
Wizard

Reputation: 1184

I'm no javascript expert, but seeing your error;

no viable alternative at input \')\' (...id,image) VALUES (\'ranjith\',[imageBuffer]))'

I feel that the query syntax could be wrong, could try

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

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

Upvotes: 1

Related Questions