javascript - 截断临时表,加载 XML 文件数据,然后使用 Node.js 从 Mysql 中的临时表插入主表
问题描述
我需要截断一个临时表,然后将 XML 文件加载到该表中,并且需要从该表中将数据插入到主表中,但是我能够加载到临时表中并插入到主表中,但是只要我添加代码截断表,表被截断,文件被加载到临时表中,但主表中没有插入。感谢帮助。
const dir = "./files/06-09-2021";
const runProcess = async () => {
try {
await checkDir();
await trunTempQuery();
const dirResult = await readDir();
await loadTempData(dirResult);
await loadData();
await sendResponse(dirResult);
} catch (err) {
console.error(err);
res.send("Some error: please check console on application");
}
};
const trunTempQuery = async () => {
console.log("in truncate query");
await pool.promise().query({
sql: `TRUNCATE TABLE mobileright.blk_temp;`,
});
};
const readDir = async () => {
return await fs.readdir(dir, "utf8");
};
const checkDir = async () => {
return await fs
.ensureDir(dir)
.then(() => {
console.log("Files Found");
})
.catch((err) => {
console.error(err);
});
};
const loadData = async () => {
console.log("In loadData");
return await pool.promise().query({
sql: `INSERT INTO mobileright.sms_blk_lists(MSISDN_vch,MSISDNEvent_vch,timeStamp_dt,changeID_vch,MSISDNNew_vch,operatorID_vch)
SELECT MSISDN,MSISDNEvent,timeStamp,changeID,MSISDNNew,operatorID
FROM mobileright.blk_temp`,
});
};
const loadTempData = async (dirResult) => {
console.log("In loadTempData");
try {
if (!dirResult.length) {
console.log(dirResult);
res.send("No files for today");
} else {
await dirResult.forEach(async (file) => {
return await pool.promise().query({
sql: "LOAD XML LOCAL INFILE ? INTO TABLE mobileright.blk_temp ROWS IDENTIFIED BY ?",
values: [
"C:/work/blacklisting/files/06-09-2021/" + file,
"<MSISDNChange>",
],
infileStreamFactory: () =>
fs.createReadStream(
"C:/work/blacklisting/files/06-09-2021/" + file
),
});
});
}
} catch (err) {
console.log(err);
}
};
const sendResponse = (dirResult) => {
console.log("response sent");
res.send({ dirResult });
};
runProcess();
解决方案
推荐阅读
- airflow - 在 Airflow 中永久运行 DAG 的最佳方式
- webstorm - WebStorm - HTML - 我输入的每个单词都会变成一个 HTML 标签,我该如何阻止它?
- linux - Linux addr2line 命令返回 ??:0
- java - 如何将 Java 库用于独立应用程序(MATLAB、MacOs)
- javascript - 使用 jsonp 如何将结果保存在外部变量中
- python - 如何使用谷歌分面潜水显示图像?
- php - 在 Laravel 5.8 中验证多维数组时是否可以使用另一个字段的值?
- mecab - 是否有 mecab(日语单词解析器)算法的描述?
- ios12 - 在 iOS12 中以编程方式检查蜂窝数据的开/关
- redirect - isomorphic-unfetch 302 响应在 IE11 中运行缓慢