Heimdall - Golang 开发的数据库导入导出工具

Heimdall 是一款专门为 MySQL 设计的命令行导入导出工具,使用它,你可以通过写 SQL 的方式,将查询结果直接导出 xlsx、csv、markdown、html、json、yaml、xml、sql 等格式的文件,也可以将 xlsx 和 csv 格式的文件导入到数据库表中。

项目地址:

https://github.com/mylxsw/heimdall

命令行选项

heimdall 支持以下命令

  • import (aka load) 将 xlsx 或者 csv 文件导入到数据库表中
  • export (aka query) 将 SQL 查询结果导出到各种格式的文件

import/load

使用 import/load 命令,你可以从 xlsx 或者 csv 文件导入数据库到数据表中。支持以下命令行选项:

  • –host value, -H value MySQL host (default: “127.0.0.1”)
  • –port value, -P value MySQL port (default: 3306)
  • –user value, -u value MySQL user (default: “root”)
  • –password value, -p value MySQL password
  • –database value, -d value MySQL database
  • –connect-timeout value database connect timeout (default: 3s)
  • –debug, -D Debug mode (default: false)
  • –file value, -i value, –input value *[ –file value, -i value, –input value ]* input excel or csv file path, this flag can be specified multiple times for importing multiple files at the same time
  • –table value, -t value target table name
  • –field value, -f value *[ –field value, -f value ]* field map, eg: excel_field:db_field, this flag can be specified multiple times
  • –include value, -I value *[ –include value, -I value ]* include fields, if set, only these fields will be imported, this flag can be specified multiple times
  • –exclude value, -E value *[ –exclude value, -E value ]* exclude fields, if set, these fields will be ignored, this flag can be specified multiple times
  • –csv-sepertor value csv file sepertor, default is ‘,’ (default: “,”)
  • –tx, -T import data using transaction, all success or all failure, only work with InnoDB or other engines that support transaction (default: false)
  • –dry-run perform import tests to verify correctness of imported files, but do not commit transactions, only work with InnoDB or other engines that support transaction (default: false)
  • –help, -h show help (default: false)

export/query

使用 export/query 命令,你可以将 SQL 查询结果导出到各种格式的文件,当前支持 JSON/YAML/Markdown/CSV/XLSX/HTML/SQL 等格式。支持以下命令行选项::

  • –host value, -H value MySQL host (default: “127.0.0.1”)
  • –port value, -P value MySQL port (default: 3306)
  • –user value, -u value MySQL user (default: “root”)
  • –password value, -p value MySQL password
  • –database value, -d value MySQL database
  • –connect-timeout value database connect timeout (default: 3s)
  • –debug, -D Debug mode (default: false)
  • –sql value, -s value SQL statement
  • –format value, -f value output format, support csv, json, yaml, xml, table, html, markdown, xlsx, plain, sql (default: “csv”)
  • –output value, -o value write output to a file, default output directly to STDOUT
  • –streaming, -S whether to use streaming output, if using streaming output, it will not wait for the query to complete, but output line by line during the query process. The output format only supports csv/xlsx/json/plain/sql (default: false)
  • –no-header, -n do not write table header (default: false)
  • –query-timeout value, -t value query timeout, when the stream option is specified, this option is invalid (default: 2m0s)
  • –xlsx-max-row value the maximum number of rows per sheet in an Excel file, including the row where the header is located (default: 1048576)
  • –table value when the format is sql, specify the table name
  • –help, -h show help (default: false)

示例

将 xlsx 文件内容导入到数据库表 people 中,标题 区域 对应字段 area,姓名对应 name,身份证号对应 idcard:

heimdall import --host 127.0.0.1 --port 3306 --database example --user root --password root \
    --table people \
    --field 区域:area \ 
    --field 姓名:name \
    --field 身份证号码:idcard \
    --file ~/Downloads/data.xlsx

从业务库中导出最近 30 天新增的客户企业清单为 Excel 文件

heimdall export --database example --host 127.0.0.1 --user root --password root \
      --sql "SELECT id, name AS '企业名称', address AS '企业地址', city_name AS '城市', district_name AS '区县', DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s') AS '创建时间' FROM enterprise WHERE created_at > DATE_SUB(NOW(), INTERVAL 30 DAY) ORDER BY id DESC" \
      --streaming \
      --format xlsx \
      --output 最近30天新增企业列表.xlsx
本作品采用《CC 协议》,转载必须注明作者和本文链接
mylxsw
讨论数量: 1

import支持sql文件不

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!