From c0680853852e807d3757722e54a1d285e0465d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Tue, 25 Oct 2022 15:16:59 +0800 Subject: [PATCH] update --- netease_music/sql/export_sql_generator.js | 44 +++++++++++++++++++ .../sql/replace_insert_table_name.js | 27 ++++++++++++ qianqian_music/.gitignore | 1 + 3 files changed, 72 insertions(+) create mode 100644 netease_music/sql/export_sql_generator.js create mode 100644 netease_music/sql/replace_insert_table_name.js create mode 100644 qianqian_music/.gitignore diff --git a/netease_music/sql/export_sql_generator.js b/netease_music/sql/export_sql_generator.js new file mode 100644 index 0000000..286fee7 --- /dev/null +++ b/netease_music/sql/export_sql_generator.js @@ -0,0 +1,44 @@ +const export_gap = 50000000; +const partition_gap = 50000000; +const table = "comment_1024"; +const index = "comment_id"; +const exportTablePrefix = "comment_export_"; + +// 数字转成字符串,同时在前面填充 +function fill(num, fillers, length) { + var result = `${num}`; + if (result.length < length) + result = new Array(length - result.length + 1).join(fillers) + result; + return result; +} + +let sqlArr1 = []; +let sqlArr2 = []; +let sqlArr3 = []; +for (let i = 200; i < 300; i++) { +// for (let i = 100; i < 200; i++) { + let where = `${index} >= ${fill(i * export_gap, ' ', 12)} and ${index} < ${fill((i + 1) * export_gap, ' ', 12)}`; + + let sql_create_table = `create table ${exportTablePrefix}${fill(i, '0', 4)} select * from ${table} where ${where};`; + sqlArr1.push(sql_create_table); + + let sql_delete_rows = `DELETE FROM ${table} WHERE ${where};`; + sqlArr2.push(sql_delete_rows); + + let sql_partition = ` PARTITION p${fill(i, '0', 4)} VALUES LESS THAN (${fill((i + 1) * partition_gap, ' ', 12)})`; + sqlArr3.push(sql_partition); +} +sqlArr3.push(` PARTITION p_max VALUES LESS THAN MAXVALUE`); + +// 建表 +// console.log(sqlArr1.join('\n')); + +// 删除原表数据 +// console.log(sqlArr2.join('\n')); + +// 新创建表的分区 +// console.log(`partition (\n${sqlArr3.join(',\n')}\n)`); + +for (let i = 107; i < 200; i++) { + console.log(`DROP TABLE IF EXISTS comment_export_${fill(i, '0', 4)};`); +} \ No newline at end of file diff --git a/netease_music/sql/replace_insert_table_name.js b/netease_music/sql/replace_insert_table_name.js new file mode 100644 index 0000000..ab80523 --- /dev/null +++ b/netease_music/sql/replace_insert_table_name.js @@ -0,0 +1,27 @@ +const fs = require('fs'); +const absPath = `D:/sql_export`; + +// 数字转成字符串,同时在前面填充 +function fill(num, fillers, length) { + var result = `${num}`; + if (result.length < length) + result = new Array(length - result.length + 1).join(fillers) + result; + return result; +} + +let begin = 115; +let end = 116; +for (let i = begin; i < end; i++) { + console.log(`读取文件 comment_export_${fill(i, '0', 4)}.sql`); + let sqlFile = fs.readFileSync(`${absPath}/comment_export_${fill(i, '0', 4)}.sql`, "utf-8"); + console.log(`处理文件`); + // console.log(sqlFile); + let sqls = sqlFile.split("\r\n"); + sqls = sqls.map(sql => sql.replace(`INSERT INTO \`comment_export_${fill(i, '0', 4)}\` VALUES`, "INSERT INTO `comment` VALUES")) + // console.log(sqls); + console.log(`拼接文件`); + let newSqlFile = sqls.join('\n'); + console.log(`写入文件`); + fs.writeFileSync(`${absPath}/output/comment_${fill(i, '0', 4)}.sql`, newSqlFile, "utf-8"); + console.log(`完成`); +} \ No newline at end of file diff --git a/qianqian_music/.gitignore b/qianqian_music/.gitignore new file mode 100644 index 0000000..6320cd2 --- /dev/null +++ b/qianqian_music/.gitignore @@ -0,0 +1 @@ +data \ No newline at end of file