包含cassandra query的承诺数组触发不好

bnl4lu3b  于 2021-06-15  发布在  Cassandra
关注(0)|答案(0)|浏览(183)

我目前正试图用xlsx文件的内容填充cassandra表,但我遇到了一个问题。
我设法用所有必要的查询来填充我的表(“insert into my_table(values name,…)values(values,…);”)。所以我的数组有7000个字符串。
然后我做了一个承诺,在这个承诺中我做了一个循环来填充和排列承诺,以便在所有承诺都结束时触发第一个承诺。
这是我写的密码
index.js=>

const ImportFileContent = require("./scripts/import_file_content")
const InsertDb = require("./scripts/insertDb")

const cassandra = require('cassandra-driver');
const databaseConfig = require('./config/database.json');

const authProvider = new cassandra.auth.PlainTextAuthProvider(databaseConfig.cassandra.username, databaseConfig.cassandra.password);

const db = new cassandra.Client({
    contactPoints: databaseConfig.cassandra.contactPoints,
    authProvider: authProvider
});

// ImportFileContent.importFileContent return an array of string, those strings contains all the 7000+ queries
ImportFileContent.importFileContent().then(queries => {
    InsertDb.clients(db, queries).then(result => {
        console.log(result);
         db.shutdown(function (err, result) {
        });
    });
});

insertdb.js=>

let DB = null;

module.exports = {
    ClientsLeasing: function (db, queries) {
        DB = db;
        return insertClientsLeasing(queries);
    }
}

function insertClientsLeasing(queries) {
    return new Promise((resolve, reject) => {
        let nbError = 0;
        let nbSuccess = 0;
        let promisesArray = [];
        //I made i <2000 here because my cassandra setup doesn't manage more than 2048 request in parallele
        for (let i = 0; i < 2000; i++) {
            promisesArray.push(new Promise(function (resolve, reject) {
                DB.execute(queries[i], function (err, result) {
                    if (err) {
                        nbError++;
                        reject(err)
                    } else {
                        nbSuccess++;
                        resolve();
                    }
                });
            }));
        }
        Promise.all(promisesArray).then((result) => {
            console.log("is over")
            console.log("over ===================== success => ", nbSuccess, "                    errors => ", nbError);
            resolve("success");
        }).catch((error) => {
            console.log(error);
            console.log("is over error")
            console.log("over ===================== success => ", nbSuccess, "                    errors => ", nbError);
            resolve("error");
        });

    });
}

我的表有两个主键:创建日期(which is now())和客户机id,它们可能位于xlsx的多个行中(这可能是问题的根源?)。
所以现在当我启动这个代码时,我的输出是

然后,当我通过cqls在表中进行计数时,我只得到1962行的输出

我觉得我在履行诺言的时候漏掉了什么。我真的不明白。
谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题