V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
mylxsw
V2EX  ›  Go 编程语言

用 Go 开发了一款命令行的的数据库导入导出工具 Heimdall

  •  
  •   mylxsw ·
    mylxsw · 2022-11-27 15:36:41 +08:00 · 2611 次点击
    这是一个创建于 488 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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
    
    16 条回复    2022-11-30 13:30:29 +08:00
    lizhenda
        1
    lizhenda  
       2022-11-27 15:38:04 +08:00
    不错哦,准备试试
    v2wtf
        2
    v2wtf  
       2022-11-27 16:13:37 +08:00
    nice work
    xiaoz
        3
    xiaoz  
       2022-11-27 16:31:25 +08:00 via Android
    最近正在思考 MySQL 的备份就看到这个帖子,及时雨啊,感谢楼主。
    aaa5838769
        4
    aaa5838769  
       2022-11-27 16:53:11 +08:00
    支持一下。
    liaohongxing
        5
    liaohongxing  
       2022-11-27 20:39:14 +08:00
    非常不错 。每年都有几次 10g 左右的 mysql 数据搬迁 。不知道对于大文件导入导出内存怎么样,如果半路崩溃是怎么处理的 。能否兼容 tidb 。
    xiaoz
        6
    xiaoz  
       2022-11-27 22:05:33 +08:00
    @liaohongxing 老哥 10G 的数据库迁移用的什么方案?
    blankmiss
        7
    blankmiss  
       2022-11-27 22:10:12 +08:00
    可视化的话 会不会更爽
    ye10010
        8
    ye10010  
       2022-11-27 22:20:39 +08:00
    支持,另外问下如果有特殊字符,比如双引号,能够正确导出为 csv 之类的吗?
    mylxsw
        9
    mylxsw  
    OP
       2022-11-27 23:40:53 +08:00
    @ye10010 没问题的,特殊字符都是 ok 的
    gniviliving
        10
    gniviliving  
       2022-11-28 08:19:38 +08:00
    heimdall 是一个导航页工具呀,这都能重名,世界真小
    https://github.com/linuxserver/Heimdall
    fluent12138
        11
    fluent12138  
       2022-11-28 15:48:41 +08:00
    不支持 SELECT * FROM `table name` 这种``的语法嘛
    mylxsw
        12
    mylxsw  
    OP
       2022-11-28 17:45:09 +08:00
    @fluent12138 支持
    defunct9
        13
    defunct9  
       2022-11-29 13:56:09 +08:00
    ./heimdall export --host 127.0.0.1 --port 3306 --database falcon --user root --password xxxxxx --format xlsx --output 1.xls

    panic: runtime error: index out of range [-1]

    goroutine 1 [running]:
    github.com/mylxsw/heimdall/commands.readAll({0xbe7720?, 0xc00009c000?}, 0x3b)
    /Users/mylxsw/Workspace/codes/github/heimdall/commands/global.go:26 +0x2c5
    github.com/mylxsw/heimdall/commands.resolveExportOption(0xc0002ad648?)
    /Users/mylxsw/Workspace/codes/github/heimdall/commands/export.go:44 +0x85
    github.com/mylxsw/heimdall/commands.ExportCommand(0xc0000d7e00?)
    /Users/mylxsw/Workspace/codes/github/heimdall/commands/export.go:65 +0xc5
    github.com/urfave/cli/v2.(*Command).Run(0xc0000d7e00, 0xc0000a37c0, {0xc0002de2d0, 0xf, 0xf})
    /Users/mylxsw/Deps/Go/pkg/mod/github.com/urfave/cli/[email protected]/command.go:271 +0xaab
    github.com/urfave/cli/v2.(*Command).Run(0xc0002e03c0, 0xc0000a3680, {0xc0000a4000, 0x10, 0x10})
    /Users/mylxsw/Deps/Go/pkg/mod/github.com/urfave/cli/[email protected]/command.go:264 +0xd0d
    github.com/urfave/cli/v2.(*App).RunContext(0xc00009a1e0, {0xbeb040?, 0xc0000a60a0}, {0xc0000a4000, 0x10, 0x10})
    /Users/mylxsw/Deps/Go/pkg/mod/github.com/urfave/cli/[email protected]/app.go:329 +0x665
    github.com/urfave/cli/v2.(*App).Run(...)
    /Users/mylxsw/Deps/Go/pkg/mod/github.com/urfave/cli/[email protected]/app.go:306
    main.main()
    /Users/mylxsw/Workspace/codes/github/heimdall/main.go:68 +0x69a
    defunct9
        14
    defunct9  
       2022-11-29 13:57:21 +08:00
    想给同事 show 一把的,结果没秀出来
    mylxsw
        15
    mylxsw  
    OP
       2022-11-29 15:38:36 +08:00
    @defunct9 少了个 --sql
    defunct9
        16
    defunct9  
       2022-11-30 13:30:29 +08:00
    明白了,这是个表级别的工具,不是库级别的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2891 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 14:20 · PVG 22:20 · LAX 07:20 · JFK 10:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.