HeimdallMySQL 數(shù)據(jù)庫(kù)導(dǎo)入導(dǎo)出工具
Heimdall 是一款專門為 MySQL 設(shè)計(jì)的命令行導(dǎo)入導(dǎo)出工具,使用它,你可以通過寫 SQL 的方式,將查詢結(jié)果直接導(dǎo)出 xlsx、csv、markdown、html、json、yaml、xml、sql 等格式的文件,也可以將 xlsx 和 csv 格式的文件導(dǎo)入到數(shù)據(jù)庫(kù)表中。
命令行選項(xiàng)
heimdall 支持以下命令
- import (別名 load) 將 xlsx 或者 csv 文件導(dǎo)入到數(shù)據(jù)庫(kù)表中
- export (別名 query) 將 SQL 查詢結(jié)果導(dǎo)出到各種格式的文件
import/load
使用 import/load 命令,你可以從 xlsx 或者 csv 文件導(dǎo)入數(shù)據(jù)庫(kù)到數(shù)據(jù)表中。支持以下命令行選項(xiàng):
- --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 查詢結(jié)果導(dǎo)出到各種格式的文件,當(dāng)前支持 JSON/YAML/Markdown/CSV/XLSX/HTML/SQL 等格式。支持以下命令行選項(xiàng):
- --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 文件內(nèi)容導(dǎo)入到數(shù)據(jù)庫(kù)表 people 中,標(biāo)題 區(qū)域 對(duì)應(yīng)字段 area,姓名對(duì)應(yīng) name,身份證號(hào)對(duì)應(yīng) idcard:
heimdall import --host 127.0.0.1 --port 3306 --database example --user root --password root \
--table people \
--field 區(qū)域:area \
--field 姓名:name \
--field 身份證號(hào)碼:idcard \
--file ~/Downloads/data.xlsx
從業(yè)務(wù)庫(kù)中導(dǎo)出最近 30 天新增的客戶企業(yè)清單為 Excel 文件
heimdall export --database example --host 127.0.0.1 --user root --password root \ --sql "SELECT id, name AS '企業(yè)名稱', address AS '企業(yè)地址', city_name AS '城市', district_name AS '區(qū)縣', DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s') AS '創(chuàng)建時(shí)間' FROM enterprise WHERE created_at > DATE_SUB(NOW(), INTERVAL 30 DAY) ORDER BY id DESC" \ --streaming \ --format xlsx \ --output 最近30天新增企業(yè)列表.xlsx
評(píng)論
圖片
表情
