1 Star 1 Fork 0

Raisr/csv2mysqlra

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

csv 数据导入 mysql

本工具是通过 sequlize 来将数据导入 mysql。当 csv 文件大小大于 100M 时,程序会使用,逐行读取方式来获取数据,以减少内存消耗。

引入方式

npm i csv2mysqlra -S

使用方法

const csv2mysqlra = require('csv2mysqlra');

csv2mysqlra({
  seqConfig, // sequlize 配置
  csvDirPath: "./csv", // csv 数据所在目录
  modelDirPath: "./model", // sequlize model 定义未知
  batchNum: 600, // 每次批量导入数据的条数 默认 600
  workerNum: 6, // 启用的 worker 数量 默认 6 个
  logDirPath: "./log", // 异常日志目录
  startIndex: 0, // tbNames 起始位置 默认为0
  endIndex: 5, // tbNames 终止位置 默认为要导入表的数量
  tbNames: ["user"], // 表名 于 csv 文件名同名 如果不填,将默认csvDirPath下所有csv文件名为tbNames
  tbMaps:{ user:['user1.csv','user2.csv']}, // 新增功能,model 目录和 csv文件的映射假如 user 表有两份文件要导入,可以使用此功能。但是使用了tbMaps 那么 tbNames 就会失效。
  hasThRow: true, // csv 文件是否有表头行 默认为 true
  csvExtension: ".csv",// csv 文件后缀 默认为 .csv
  filter(tbName,row,ri){ // 数据过滤 需要导入的数据返回true 不需要的数据返回false
    // tbName 当前数据所在表的表名
    // row 当前读取行的字符串
    // ri 当前行所在行号 不含表头行
    return true|false;
  },
  headHandler(tbName, thRow) {
    // tbName 表名
    // thRow 表头所在行的字符串
    return 处理好后的字符串数组
  },
  rowHandler(thead, tbName, row, ri) {
    // thead 表头字符串数组
    // tbName 当前数据所在表的表名
    // row 当前读取行的字符串
    // ri 当前行所在行号 不含表头行
    return 处理好后的对象
  },
});

sequlize 配置

const seqConfig = {
  dialect: "mysql",
  host: "127.0.0.1",
  port: 3306,
  database: "db",
  username: "username",
  password: "password",
};

model 定义方式

module.exports = (sequelize, DataTypes) => {
  const { STRING, DATE, NOW } = DataTypes;

  const User = sequelize.define("users", {
    id: {
      field: "id",
      type: STRING(30),
      comment: "用户工号",
      primaryKey: true,
      unique: true,
    },
    name: {
      field: "name",
      type: STRING(255),
      comment: "用户姓名",
    },
    createdAt: {
      field: "created_at",
      type: DATE,
      comment: "创建时间",
      defaultValue: NOW,
    },
    updatedAt: {
      field: "updated_at",
      type: DATE,
      comment: "更新时间",
      defaultValue: NOW,
    },
  });
  return User;
};

约定

要导入的数据文件名与 model 文件名称必须保持一致。 比如在 model 目录中,定义了 user.js 文件,那么它定义的表所对应的 csv 文件必须是 user.csv

经过测试发现 worker = 6,batchNum = 600。导入速度最快。

MIT License Copyright (c) 2023 Raisr Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

nodejs 模块,使用 sequlize 将数据从csv导入到mysql中 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
NodeJS
1
https://gitee.com/rasir/csv2mysqlra.git
git@gitee.com:rasir/csv2mysqlra.git
rasir
csv2mysqlra
csv2mysqlra
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891