简体   繁体   English

我想使用nodejs和angularjs上传一个csv文件,然后将其保存到cassandra

[英]i want to upload a csv file using nodejs and angularjs and then save it to cassandra

This is my node js code i want to upload a csv file using nodejs and angularjs and then save it to cassandra. 这是我的节点js代码,我想使用nodejs和angularjs上传一个csv文件,然后将其保存到cassandra。

    app.use(function(req, res, next) { //allow cross origin requests
        res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
        res.header("Access-Control-Allow-Origin", "http://localhost");
        res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        next();
    });

    app.use(express.static('../client'));
    app.use(bodyParser.json());  

    var storage = multer.diskStorage({ //multers disk storage settings
        destination: function (req, file, cb) {
            cb(null, './uploads/');
        },
        filename: function (req, file, cb) {
            var datetimestamp = Date.now();
            cb(null, file.fieldname + '-' + datetimestamp + '.' + file.originalname.split('.')[file.originalname.split('.').length -1]);
        }
    });

    var upload = multer({ //multer settings
                    storage: storage
                }).single('file');

    /** API path that will upload the files */
    app.post('/upload', function(req, res) {
        upload(req,res,function(err){
            if(err){
                 res.json({error_code:1,err_desc:err});
                 return;
            }
             res.json({error_code:0,err_desc:null});
        });
    });

I want to upload my file directly to cassandra through the gui. 我想通过gui将文件直接上传到cassandra。 any lead or help is thankful. 任何领导或帮助都是感激的。

You should find a way to read from the multipart form data without storing to the webserver hard drive. 您应该找到一种从多部分表单数据中读取而不将其存储到Web服务器硬盘驱动器的方法。

You can use busboy (middleware connect-busboy ) to read the file data from the response as a stream. 您可以使用busboy (中间件connect-busboy )以流的形式从响应中读取文件数据。 Once you can read from the upcoming data, you can insert it to Cassandra. 一旦您可以读取即将到来的数据,就可以将其插入Cassandra。

You have to define: 您必须定义:

  • Max level of concurrency: how many csv lines you want to insert to C* in parallel. 最大并发级别:您要并行插入C *的csv行数。
  • How to handle failure: what to do when a single insert fails. 如何处理失败:单个插入失败时该怎么办。

After that, you can read lines and insert those in parallel to Cassandra like this: 之后,您可以读取行并将其平行于Cassandra插入,如下所示:

// Use some control flow methods (dependency)
const async = require('async');
const csv = stream; //using busboy to obtain the file stream
// Maximum amount of rows to insert in parallel
const limit = 256;
csv.on('readable', function () {
  //its emitted once there is a chunk of data
  var r;
  var rows = [];
  async.whilst(function condition() {
    while ((r = csv.read()) != null && rows.length < limit) {
      rows.push(r);
    }
    return rows.length > 0;
  }, function eachGroup(next) {
    // We have a group of 256 rows or less to save
    // we can do it in a batch
    // or we can do it in parallel with async.each()
    async.each(rows, function (r, eachCallback) {
      //adapt the csv row to parameters
      //sample
      var params = r.split(',);
      client.execute(query, params, { prepare: true}, eachCallback);
    }, next);
  }, function groupFinished(err) {
    if (err) {
      //something happened when saving
      //do something with err
      return;
    }
    // The chunk of csv rows emitted by csv stream where saved
  });
}).on('end', function () {
  // no more data
});

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

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