用户指南—数据导入和导出—使用Batch Tool工具导入导出数据

简介: 本文介绍了通过Batch Tool工具导入导出数据的方法。

工具介绍

Batch Tool工具是PolarDB-X团队开发的专为PolarDB-X数据库提供数据导入导出服务的工具。

Batch Tool工具采用JAVA语句实现,核心是生产者消费者模型,支持多线程操作,提供批量导出、批量导入、批量删除、批量更新等功能。数据以csv文件格式传输,方便用户进行数据交互。

Batch Tool工具的命令用法和参数如下:


usage: BatchTool [-?] [-batchsize <arg>] [-con <consumer count>] [-cs
           <charset>] -D <database> [-dir <directory>] [-f <from>] [-F
           <filenum>] [-func] -h <host> [-header] [-i] [-in] [-L <line>]
           [-lastSep] [-lb] [-local] [-maxConn <arg>] [-minConn <arg>]
           [-noesc] [-np] [-O <order by type>] -o <operation> [-OC <ordered
           column>] -p <password> [-P <port>] [-para] [-pre <prefix>] [-pro
           <producer count>] [-quote <auto/force/none>] [-readsize <arg>]
           [-rfonly] [-ringsize <arg>] -s <sep> [-t <table>] -u <user> [-w
           <where>]
     -?,--help                              Help message.
     -batchsize,--batchSize <arg>           Batch size of emitted tuples.
     -con,--consumer <consumer count>       Configure number of consumer
                                            threads.
     -cs,--charset <charset>                Define charset of files.
     -D,--database <database>               Database to use.
     -dir,--dir <directory>                 Directory path including files to
                                            import.
     -f,--from <from>                       Source file(s), separated by ; .
     -F,--filenum <filenum>                 Fixed number of exported files.
     -func,--sqlfunc                        Use sql function to update.
     -h,--host <host>                       Connect to host.
     -header,--header                       Whether the header line is column
                                            names.
     -H,--historyFile <filename>            history file name for resuming from breakpoint
     -i,--ignoreandresume                   Flag of insert ignore & resume from breakpoint
     -in,--wherein                          Using where ... in (...)
     -L,--line <line>                       Max line limit of exported files.
     -lastSep,--withLastSep                 Whether line ends with separator.
     -lb,--loadbalance                      If using load balance.
     -local,--localmerge                    o local merge sort.
     -maxConn,--maxConnection <arg>         Max connection number limit.
     -minConn,--minConnection <arg>         Mim connection number limit.
     -noesc,--noescape                      Don't escape values.
     -np,--noparttion                       No use of parttion.
     -O,--orderby <order by type>           asc or desc.
     -o,--operation <operation>             Batch operation type: export /
                                            import / delete / update.
     -OC,--orderCol <ordered column>        col1;col2;col3.
     -p,--password <password>               Password to use when connecting to
                                            server.
     -P,--port <port>                       Port number to use for connection.
     -para,--paraMerge                      Using parallel merge when doing
                                            order by export.
     -pre,--prefix <prefix>                 Export file name prefix.
     -pro,--producer <producer count>       Configure number of producer
                                            threads.
     -quote,--quoteMode <auto/force/none>   The mode of how field values are
                                            enclosed by double-quotes when
                                            exporting table. Default value is
                                            auto.
     -readsize,--readSize <arg>             Read block size in MB.
     -rfonly,--rfonly                       Only read and process file, no sql
                                            execution.
     -ringsize,--ringBufferSize <arg>       Ring buffer size.
     -s,--sep <sep>                         Separator between fields
                                            (delimiter).
     -t,--table <table>                     Target table.
     -tps,--tpsLimit <arg>                  Tps limit
     -u,--user <user>                       User for login.
     -w,--where <where>                     Where condition: col1>99 AND
                                            col2<100 ...

参数说明

常用参数说明如下:

  • -o:批处理操作,包括export、import、delete、update四个选项。
  • -t:指定目标表名,只能为单个表。
  • -s:指定分隔符,可以为字符或字符串。
  • -f:指定源文件,多个文件名之间使用分号“;”分隔。
  • -OC:指定导出时排序使用的列名,多个列之间使用分号“;”分隔。
  • -cs:指定文本文件的字符集,默认为utf-8。
  • -lastSep:文件每行是否以分隔符结尾。
  • -quote:指定导出或导入时引号包围模式,包括以下三个可选值:
    • auto:默认模式,将根据字段值是否包含特殊字符(如分隔符、换行符等)来添加双引号;
    • force:强制每个字段值都添加双引号;
    • none:强制不添加双引号(适用于已知表字段类型都是数值型、或字符串型字段中不包含特殊字符的情况)。
  • -header:首行是否为字段名。
  • -i:是否开启insert ignore与断点续传。
  • -pre:指定导出文件名的前缀。
  • -F:指定导出文件数量。

工具获取

Batch Tool工具的jar包,单击下载:Batch_tool工具

使用示例

以编译生成的batch-tool.jar为例,查看参数说明:


java -jar batch-tool.jar -?
  • 批量导出数据
## 1.默认导出(文件数等于表的分片数)

java -jar batch-tool.jar -h 127.0.0.1 -u testUser -p testPasswd -P 3306 -D db_name -o export -t table_name -s ,
## 2.导出文件数=3 (-F:指定文件数)
java -jar batch-tool.jar -h 127.0.0.1 -u testUser -p testPasswd -P 3306 -D db_name -o export -t table_name -s , -F 3
## 3.指定单个文件最大行数=10000 (-L:指定单文件行数)
java -jar batch-tool.jar -h 127.0.0.1 -u testUser -p testPasswd -P 3306 -D db_name -o export -t table_name -s , -L 10000
## 4.带where条件 若条件带有空格,需要使用引号 (-w:where条件语句)
java -jar batch-tool.jar -h 127.0.0.1 -u testUser -p testPasswd -P 3306 -D db_name -o export -t table_name -s , -w "id < 150 and id > 120"
  • 批量导入数据(需手动创建目标表,Batch Tool只包含数据传输)
## 1.多个文件用分号 (;) 分隔
java -jar batch-tool.jar -h 127.0.0.1 -u testUser -p testPasswd -P 3306 -D db_name -o import -t table_name -s , -f "table_name_0;table_name_1;table_name_2;table_name_3"
## 2.默认根据拆分键进行sharding插入, 如不采用,打开-np开关即可
java -jar batch-tool.jar -h 127.0.0.1 -u testUser -p testPasswd -P 3306 -D db_name -o import -t table_name -s , -f "file0;file2" -np
## 3.指定生产者、消费者线程(-pro:生产者线程,读取文件线程; -con:消费者线程,导入数据线程)
java -jar batch-tool.jar -h 127.0.0.1 -u testUser -p testPasswd -P 3306 -D db_name -o import -t table_name -s , -f "table_name_0;table_name_1" -np -pro 16 -con 16
## 4.打开insert ignore和断点续传
java -jar batch-tool.jar -h 127.0.0.1 -u testUser -p testPasswd -P 3306 -D db_name -o import -t table_name -s , -f "table_name_0;table_name_1" -i
  • 批量删除数据(删除数据库中包含文件中的数据,原理:构建DELETE语句,根据表结构填充文件中的数据)
java -jar batch-tool.jar -h 127.0.0.1 -u testUser -p testPasswd -P 3306 -D db_name -o delete -t table_name -s , -f "file0"
  • 批量更新数据(更新数据库中包含文件中的数据,原理:构建UPDTATE语句,根据表结构填充文件中的数据)
java -jar batch-tool.jar -h 127.0.0.1 -u testUser -p testPasswd -P 3306 -D db_name -o update -t table_name -s ,
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
相关文章
|
网络协议 网络架构 Windows
【Windows】MCSM面板搭建Mycraft服务器,实现公网远程联机
【Windows】MCSM面板搭建Mycraft服务器,实现公网远程联机
709 0
|
运维 Cloud Native 关系型数据库
“瑶池”正式发布,一站式数据管理与服务
11月3日,在2022云栖大会上,阿里巴巴集团副总裁、阿里云数据库产品事业部负责人李飞飞发表《一站式、全场景数据管理与服务》主题演讲,分享了数据库发展的四个核心趋势,并发布数据库独立品牌“瑶池”,将云原生关系型数据库PolarDB、云原生数据仓库AnalyticDB (简称ADB)、云原生多模数据库Lindorm等产品统一归属到全新品牌“瑶池”。
2707 0
 “瑶池”正式发布,一站式数据管理与服务
|
人工智能 前端开发 JavaScript
阿里云安全类云产品,验证码使用时滑动验证流程及线上问题排查
阿里云验证码产品,使用业界先进的风控引擎结合“规则+AI”模型,有效区分真实用户和机器自动化脚本攻击,避免机器请求造成业务损失。主要适用于垃圾注册、刷库撞库,薅羊毛,短信被刷等风险场景。为您提供安全可靠的业务环境。本文为大家介绍验证码使用时滑动验证流程及验证不通过的问题排查。
66106 5
阿里云安全类云产品,验证码使用时滑动验证流程及线上问题排查
|
消息中间件 监控
异步处理和消息队列的结合使用
异步处理和消息队列的结合使用
|
边缘计算 人工智能 监控
边缘计算与AI结合的场景案例研究
【8月更文第17天】随着物联网(IoT)设备数量的爆炸性增长,对实时数据处理的需求也随之增加。传统的云计算模型在处理这些数据时可能会遇到延迟问题,尤其是在需要即时响应的应用中。边缘计算作为一种新兴的技术趋势,旨在通过将计算资源更靠近数据源来解决这个问题。本文将探讨如何将人工智能(AI)技术与边缘计算结合,以实现高效的实时数据分析和决策制定。
1368 1
|
SQL 前端开发 数据库
智能评估时代:SurveyKing开源问卷系统YYDS
我发现了一个开源的问卷/考试系统SurveyKing([GitHub](https://github.com/javahuang/surveyking) / [Gitee](https://gitee.com/surveyking/surveyking)),拥有强大功能,包括丰富的问卷设计选项。它在GitHub上有2.9k stars。虽然后端开源,但前端代码未公开。快速体验可通过Docker运行`docker run -p 1991:1991 surveyking/surveyking`。项目部署涉及数据库初始化和编写`docker-compose.yml`。
889 22
智能评估时代:SurveyKing开源问卷系统YYDS
|
存储 关系型数据库 MySQL
MySQL数据库数据块大小
MySQL数据库数据块大小
296 2
|
数据安全/隐私保护
【qt】考试系统项目
【qt】考试系统项目
212 0
|
存储 算法 安全
详细解读CA认证原理以及实现(上)
详细解读CA认证原理以及实现(上)
629 0