# UniversalScriptEngine **Repository Path**: zacharyilearn/dataScript ## Basic Information - **Project Name**: UniversalScriptEngine - **Description**: 这是一个通用脚本引擎,使用纯java编写,可替代python,使用注解方式扩展脚本命令 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2022-08-31 - **Last Updated**: 2022-08-31 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 一、框架简介: 这是一个通用脚本引擎框架,是一个运行在 Java 虚拟机上的脚本命令解释器。它的功能是动态解释执行脚本命令或脚本文件,将脚本命令译成 JAVA 语言代码来完成一系列的功能。 通用脚本引擎在运行命令与脚本文件时不需要预编译步骤,可以更简单地动态解释脚本命令与脚本文件,并运行命令。 脚本引擎按命令在脚本语句中从左到右的先后顺序逐个读取并运行,会依据每个命令的返回值(整数)来判断命令运行成功还是发生错误: 如果命令的返回值是 0 时,表示命令运行成功,脚本引擎继续读取下一个命令并运行! 如果命令的返回值是非 0 时,表示命令运行错误,并且脚本引擎会终止运行并退出! 通用脚本引擎框架支持多线程并发执行脚本语句。 下面是当前 JDK 中所有已注册的脚本引擎,如下: -------------------------------------------------------------------------------------------------- 属性名 属性名 ---------------- -------------------------------------------------------------------------------- 脚本引擎 UniversalScriptEngine 脚本引擎名 sql, usl 脚本引擎版本 1.0 脚本引擎扩展名 sql, usl 脚本引擎扩展类型 application/sql, application/usl 脚本引擎语言名 Universal Script Lanuage 脚本引擎语言版本 UniversalScript - Edition 1.0 脚本引擎并行运行 THREAD-ISOLATED 脚本引擎标准输出 echo 'hello world!' 脚本引擎合并命令 help; help script; help set; 脚本引擎对象方法 obj.split(':', '\') 脚本引擎类名 indi.lvzhaojun.script.UniversalScriptEngine 脚本引擎 Oracle Nashorn 脚本引擎名 nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript 脚本引擎版本 1.8.0_202 脚本引擎扩展名 js 脚本引擎扩展类型 application/javascript, application/ecmascript, text/javascript, text/ecmascript 脚本引擎语言名 ECMAScript 脚本引擎语言版本 ECMA - 262 Edition 5.1 脚本引擎并行运行 脚本引擎标准输出 print('hello world!') 脚本引擎合并命令 help;help script;help set; 脚本引擎对象方法 obj.split(':','\') 脚本引擎类名 jdk.nashorn.api.scripting.NashornScriptEngine -------------------------------------------------------------------------------------------------- 二、使用方法 1. 环境配置 1)下载脚本引擎 atom.jar 文件与数据库的 JDBC 驱动包(如果使用了数据库命令的话)。 2)安装配置 JDK8 或更高版本 JDK。 3)将脚本引擎的 atom.jar 文件加入到工程的 classpath 中。 2. 程序调用 LogFactory.setLoggerLevel("info"); // 设置脚本引擎日志输出级别 ScriptEngineManager e = new ScriptEngineManager(); ScriptEngine engine = e.getEngineByExtension("sql"); try { engine.eval("help"); // 打印使用说明 } finally { engine.eval("exit 0"); // 退出脚本引擎 } 3. 外部调用 java -Dfile.encoding=GBK -cp /Users/user/git/dataScript/atom/target/atom.jar indi.lvzhaojun.script.UniversalScriptEngine echo "hello word"\; echo 'This is Universal Script Engine!'\; JVM 虚拟机可用参数: -Dtls.level=info 设置脚本引擎的日志输出级别(trace, debug, info, warn, error, fatal),非必填,默认是 debug -Dtls.charset=GBK 设置脚本引擎的默认字符集,非必填,默认使用 -Dfile.encoding= 作为默认值 -Dtls.package=indi.lvzhaojun 设置脚本引擎包名的前缀(默认使用包名的前二位),非必填 -Dtls.includes=indi.lvzhaojun 设置脚本引擎启动时扫描哪些JAVA包(包名间用半角逗号分割),非必填,默认为空(表示扫描 classpath 下所有类信息) -Dtls.excludes=indi.lvzhaojun.util 设置脚本引擎启动扫描包时排除哪些包或类(包名用半角逗号分割),非必填,默认为空 -Dtls.resource=~/script/usl/Message.properties 用于设置外部国际化资源文件,非必填,默认为空 -Dtls.proxy=true 设置使用数据库连接代理并打印 JDBC 操作详细日志,非必填,默认为 false 三、架构设计 脚本引擎框架由脚本引擎工厂,脚本引擎配置信息,脚本引擎,脚本引擎上下文信息,编译器,类型转换器,脚本引擎命令,脚本引擎变量方法,数据库方言,国际化资源构成。 脚本引擎工厂:indi.lvzhaojun.script.UniversalScriptEngineFactory ,是 JDK 脚本引擎工厂接口 javax.script.ScriptEngineFactory 的实现类。 脚本引擎:indi.lvzhaojun.script.UniversalScriptEngine,是 JDK 脚本引擎接口 javax.script.ScriptEngine 的实现类。 脚本引擎上下文信息:indi.lvzhaojun.script.UniversalScriptContext,是 JDK 脚本引擎上下文接口 javax.script.ScriptContext 的实现类。用于管理脚本引擎运行中产生的变量与程序。 编译器:indi.lvzhaojun.script.UniversalScriptCompiler,编译器用于将外部输入的脚本语句转为JAVA语言代码,由语法分析器 indi.lvzhaojun.script.UniversalScriptParser,词法分析器 indi.lvzhaojun.script.UniversalScriptReader,语句分析器 indi.lvzhaojun.script.UniversalScriptAnalysis 组成。 类型转换器:indi.lvzhaojun.script.UniversalScriptConverter,用于将 JDBC 查询结果集返回值转为脚本引擎内部使用的类型。 脚本引擎配置信息:indi.lvzhaojun.script.UniversalScriptConfiguration,用于管理脚本引擎基本属性信息。 脚本引擎命令:indi.lvzhaojun.script.UniversalScriptCommand 脚本引擎变量方法:indi.lvzhaojun.script.UniversalScriptVariableMethod 数据库方言:indi.lvzhaojun.database.DatabaseDialect 国际化资源:indi.lvzhaojun.resources.Messages.properties,如果需要扩展其他语言可以通过设置 JVM 参数 -Dtls.resource= 来设置外部资源文件绝对路径 脚本引擎支持的接口有:标准信息输出接口,错误信息输出接口,进度信息输出接口 四、命令分类: 1. 基础命令 脚本引擎框架默认提供了一系列的基础命令(如 echo、set、help 等命令),这些基础命令无需设置或配置就可以直接使用。 也可以通过设置 JVM 参数 -Dtls.excludes=indi.lvzhaojun 禁用基础命令 基础命令可按功能划分为: 1)基本命令:echo,pwd,export,exit,default,grep 等。 2)声明类命令:以 declare 开头的命令 3)逻辑控制类命令:if,while,for,break,continue,step,jump 等 4)数据库类命令:select,insert,delete,update,merge, db 等 5)网络类命令:os,ftp,sftp 等 6)文件类命令:cd,touch,rm,mkdir,cat,head,tail,tar,zip,gunzip,rar 等 7)日期类命令:date 8)线程类命令:container,sleep,nohup,wait 等 9)管道符操作:| 10) 命令代换符操作:`` 2. 自定义命令 如果基础命令不能满足需求时,你可以通过自定命令方式对基础命令进行扩展。 自定义命令需要实现 indi.lvzhaojun.script.UniversalScriptCommandCompiler 命令编译器接口,并且需要在接口 indi.lvzhaojun.script.UniversalScriptCommandCompiler 的实现类上使用 indi.lvzhaojun.annotation.ScriptCommand 注解对命令进行配置。 在首次使用脚本引擎中的类时,会首先扫描 classpath 下的 class 文件(包括 jar 包中的 class 文件)上的 indi.lvzhaojun.annotation.ScriptCommand 注解,并且会判断类是否是实现了接口 indi.lvzhaojun.script.UniversalScriptCommandCompiler,如果满足条件这个类会作为脚本命令进行加载。 如果因为脚本引擎扫描注解消耗时间过长导致启动过慢的话,可以在使用脚本引擎之前通过设置 JVM 虚拟机参数 -Dtls.includes=indi.lvzhaojun,org.apache,.. 来指定扫描 JAVA 包名的范围以提高脚本引擎启动速度。 脚本引擎首次启动时默认扫描的注解只有如下四种。如果想增加类扫描规则,需要将扫描规则添加到 indi.lvzhaojun.bean.BeanFactory.scanRule 变量上。如果想指定类扫描器使用的类加载器,可以通过变量 indi.lvzhaojun.bean.BeanFactory.classLoader 来设置。 脚本引擎命令注解:indi.lvzhaojun.annotation.ScriptCommand 脚本引擎变量方法注解:indi.lvzhaojun.annotation.ScriptVariableFunction 脚本引擎组件注解:indi.lvzhaojun.annotation.Bean 脚本引擎组件实现类的注解:indi.lvzhaojun.annotation.BeanImplement 自定义命令的实现有二种方法: 1)实现脚本引擎命令接口: 所有脚本引擎的命令都需要实现接口 indi.lvzhaojun.script.UniversalScriptCommandCompiler,并在接口实现类上配置注解 indi.lvzhaojun.annotation.ScriptCommand 标记该类是一个脚本引擎命令类。 脚本引擎在首次启动时会扫描类上的注解,如果类上存在注解 indi.lvzhaojun.annotation.ScriptCommand,则加载该类并实例化一个命令工厂实例。 生成的命令工厂的实例对象会交给编译器管理和使用,当编译器在对脚本语句进行编译时,会使用命令工厂的实例对象对脚本语句进行识别,判断脚本语句对应的是哪个命令工厂。 当编译器识别出脚本语句对应的命令工厂后,会调用命令工厂上的 indi.lvzhaojun.script.UniversalScriptCommandCompiler.compile(UniversalScriptSession, UniversalScriptContext, UniversalScriptParser, UniversalScriptAnalysis, String) 方法创建一个命令实例。 编译器会使用命令实例上的 read(UniversalScriptReader, UniversalScriptAnalysis) 方法对脚本语句进行词法分析,词法分析是解析当前命令的完整语句(如果语句跨越多行则可以使用词法分析器向下读取多行内容合并成一个完整语句)。 编译器会使用命令实例上的 compile(UniversalScriptSession, UniversalScriptContext, UniversalScriptParser, UniversalScriptAnalysis, String) 方法对脚本语句进行语义分析,语义分析是审查脚本命令有无语义错误,为代码生成阶段收集类型信息。比如语义分析的一个工作是进行类型审查,审查每个算符是否具有语言规范允许的运算对象,当不符合语言规范时,应抛出异常报告错误。 之后脚本引擎会执行命令实例上的 execute(UniversalScriptSession, UniversalScriptContext, UniversalScriptStdout, UniversalScriptStderr, boolean) 方法,以执行命令的实际业务逻辑。 2)继承已有的命令模版类: 脚本引擎提供了一些现成的命令模版类,开发人员可以在模版类的基础上实现自己的业务逻辑,可以大大减少开发工作量。 用继承已有模版方式实现脚本引擎命令时同样需要在命令类上配置注解 indi.lvzhaojun.annotation.ScriptCommand。 3)命令模版按用途可以分为: 带日志输出功能的命令模版: indi.lvzhaojun.script.command.AbstractTraceCommandCompiler 不带日志输出功能的命令模版:indi.lvzhaojun.script.command.AbstractCommandCompiler 支持文件操作的命令模版: indi.lvzhaojun.script.command.AbstractFileCommandCompiler 支持全局功能的命令模版:indi.lvzhaojun.script.command.AbstractGlobalCommandCompiler 支持主从关系的命令模版类: indi.lvzhaojun.script.command.AbstractSlaveCommandCompiler 4)其他与命令相关接口: 如下接口都是脚本引擎命令的一些非必要的接口,开发人员可以根据实际需求自主选择是否实现如下接口 indi.lvzhaojun.script.UniversalScriptInputStream 如果想要命令支持管道操作,需要实现该接口。 indi.lvzhaojun.script.UniversalScriptControlCommand 如果想要命令支持控制循环体,需要实现该接口。 indi.lvzhaojun.script.UniversalScriptThread 如果想要命令支持异步并发运行,需要实现该接口。 5)命令列表如下所示: 命令名 类名 存在使用说明 ----------- --------------------------------------------------------------- ------------ echo indi.lvzhaojun.script.command.EchoCommandCompiler set indi.lvzhaojun.script.command.SetCommandCompiler var indi.lvzhaojun.script.command.SetCommandCompiler * indi.lvzhaojun.script.command.VariableMethodCommandCompiler ^ indi.lvzhaojun.script.command.PipeCommandCompiler * indi.lvzhaojun.script.command.SubCommandCompiler function indi.lvzhaojun.script.command.FunctionCommandCompiler * indi.lvzhaojun.script.command.ExecuteFunctionCommandCompiler export indi.lvzhaojun.script.command.ExportCommandCompiler step indi.lvzhaojun.script.command.StepCommandCompiler jump indi.lvzhaojun.script.command.JumpCommandCompiler exit indi.lvzhaojun.script.command.ExitCommandCompiler select indi.lvzhaojun.script.command.SQLCommandCompiler insert indi.lvzhaojun.script.command.SQLCommandCompiler update indi.lvzhaojun.script.command.SQLCommandCompiler delete indi.lvzhaojun.script.command.SQLCommandCompiler alter indi.lvzhaojun.script.command.SQLCommandCompiler drop indi.lvzhaojun.script.command.SQLCommandCompiler create indi.lvzhaojun.script.command.SQLCommandCompiler merge indi.lvzhaojun.script.command.SQLCommandCompiler sql indi.lvzhaojun.script.command.SQLCommandCompiler /* indi.lvzhaojun.script.command.SQLCommandCompiler /** indi.lvzhaojun.script.command.SQLCommandCompiler -- indi.lvzhaojun.script.command.SQLCommandCompiler declare indi.lvzhaojun.script.command.DeclareCatalogCommandCompiler db indi.lvzhaojun.script.command.DBConnectCommandCompiler db indi.lvzhaojun.script.command.DBExportCommandCompiler db indi.lvzhaojun.script.command.DBLoadCommandCompiler extract indi.lvzhaojun.script.command.IncrementCommandCompiler container indi.lvzhaojun.script.command.ContainerCommandCompiler commit indi.lvzhaojun.script.command.CommitCommandCompiler rollback indi.lvzhaojun.script.command.RollbackCommandCompiler quiet indi.lvzhaojun.script.command.QuietCommandCompiler call indi.lvzhaojun.script.command.CallCommandCompiler cursor indi.lvzhaojun.script.command.CursorCommandCompiler while indi.lvzhaojun.script.command.WhileCommandCompiler for indi.lvzhaojun.script.command.ForCommandCompiler while indi.lvzhaojun.script.command.ReadCommandCompiler if indi.lvzhaojun.script.command.IfCommandCompiler ssh indi.lvzhaojun.script.command.SSH2CommandCompiler declare indi.lvzhaojun.script.command.DeclareSSHTunnelCommandCompiler sftp indi.lvzhaojun.script.command.SftpCommandCompiler ftp indi.lvzhaojun.script.command.FtpCommandCompiler ls indi.lvzhaojun.script.command.LsCommandCompiler cd indi.lvzhaojun.script.command.CdCommandCompiler length indi.lvzhaojun.script.command.LengthCommandCompiler pwd indi.lvzhaojun.script.command.PwdCommandCompiler mkdir indi.lvzhaojun.script.command.MkdirCommandCompiler rm indi.lvzhaojun.script.command.RmCommandCompiler isfile indi.lvzhaojun.script.command.IsFileCommandCompiler isDirectory indi.lvzhaojun.script.command.IsDirectoryCommandCompiler exists indi.lvzhaojun.script.command.ExistsCommandCompiler cat indi.lvzhaojun.script.command.CatCommandCompiler head indi.lvzhaojun.script.command.HeadCommandCompiler tail indi.lvzhaojun.script.command.TailCommandCompiler wc indi.lvzhaojun.script.command.WcCommandCompiler df indi.lvzhaojun.script.command.DfCommandCompiler dos2unix indi.lvzhaojun.script.command.Dos2UnixCommandCompiler grep indi.lvzhaojun.script.command.GrepCommandCompiler os indi.lvzhaojun.script.command.ExecuteOSCommandCompiler . indi.lvzhaojun.script.command.ExecuteFileCommandCompiler daemon indi.lvzhaojun.script.command.DaemonCommandCompiler declare indi.lvzhaojun.script.command.DeclareProgressCommandCompiler declare indi.lvzhaojun.script.command.DeclareHandlerCommandCompiler undeclare indi.lvzhaojun.script.command.UndeclareHandlerCommandCompiler handler indi.lvzhaojun.script.command.HandlerCommandCompiler declare indi.lvzhaojun.script.command.DeclareStatementCommandCompiler declare indi.lvzhaojun.script.command.DeclareCursorCommandCompiler nohup indi.lvzhaojun.script.command.NohupCommandCompiler terminate indi.lvzhaojun.script.command.TerminateCommandCompiler wait indi.lvzhaojun.script.command.WaitCommandCompiler ps indi.lvzhaojun.script.command.PSCommandCompiler sleep indi.lvzhaojun.script.command.SleepCommandCompiler date indi.lvzhaojun.script.command.DateCommandCompiler default indi.lvzhaojun.script.command.DefaultCommandCompiler find indi.lvzhaojun.script.command.FindCommandCompiler java indi.lvzhaojun.script.command.JavaCommandCompiler uuid indi.lvzhaojun.script.command.UUIDCommandCompiler md5sum indi.lvzhaojun.script.command.MD5CommandCompiler tar indi.lvzhaojun.script.command.TarCommandCompiler gzip indi.lvzhaojun.script.command.GzipCommandCompiler gunzip indi.lvzhaojun.script.command.GunzipCommandCompiler zip indi.lvzhaojun.script.command.ZipCommandCompiler unrar indi.lvzhaojun.script.command.UnrarCommandCompiler unzip indi.lvzhaojun.script.command.UnzipCommandCompiler ddl indi.lvzhaojun.script.command.DDLCommandCompiler ------ undeclare indi.lvzhaojun.script.command.UndeclareStatementCommandCompiler ------ break indi.lvzhaojun.script.command.BreakCommandCompiler ------ fetch indi.lvzhaojun.script.command.FetchStatementCommandCompiler ------ undeclare indi.lvzhaojun.script.command.UndeclareSSHCommandCompiler ------ bye indi.lvzhaojun.script.command.ByeCommandCompiler ------ undeclare indi.lvzhaojun.script.command.UndeclareCursorCommandCompiler ------ undeclare indi.lvzhaojun.script.command.UndeclareCatalogCommandCompiler ------ stacktrace indi.lvzhaojun.script.command.StacktraceCommandCompiler ------ continue indi.lvzhaojun.script.command.ContinueCommandCompiler ------ get indi.lvzhaojun.script.command.GetCommandCompiler ------ fetch indi.lvzhaojun.script.command.FetchCursorCommandCompiler ------ put indi.lvzhaojun.script.command.PutCommandCompiler ------ help indi.lvzhaojun.script.command.HelpCommandCompiler ------ man indi.lvzhaojun.script.command.HelpCommandCompiler ------ progress indi.lvzhaojun.script.command.ProgressCommandCompiler ------ declare indi.lvzhaojun.script.command.DeclareSSHClientCommandCompiler ------ return indi.lvzhaojun.script.command.ReturnCommandCompiler ------ 3. 变量方法命令 变量方法是指在脚本语句中通过使用变量名与变量方法名的方式,访问或修改变量自身值的命令。如:str.trim() 脚本引擎默认提供了一些变量方法以供开发人员使用,如下所示: ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 变量方法名 变量方法 使用说明 参数 返回值 类名 -------------------- ---------------------------------------------------------------------------------------------- ---------------------------------------------------------------- -------------------------------------------------------------------------------------------------- ----------------------------------------------- ------------------------------------------------------- PRINT variableName.print() 使用脚本引擎标准输出接口输出变量值 无参数 无 indi.lvzhaojun.script.method.PrintMethod [ variableName[index] 返回字符串指定位置的字符,返回数组指定位置的元素 参数名:index,类型:整数,范围:大于等于零且小于字符串长度或数组长度 字符串或数组元素 indi.lvzhaojun.script.method.ElementMethod SUBSTR variableName.substr(start, end) 或 variableName.substr(start) 截取字符串或数组 参数名: start 类型: 整数 范围: 大于等于零 小于字符串长度或数组长度 返回截取后的字符串或数组 indi.lvzhaojun.script.method.SubstrMethod 参数名: end 类型: 整数 范围: 大于等于零切小于等于 start 参数,截取后的值不包含 end 位置上的值 TRIM variableName.trim() 删除字符串或数组中字符串二端的空白字符 无参数 返回字符串或数组 indi.lvzhaojun.script.method.TrimMethod LTRIM variableName.ltrim() 删除字符串或数组中字符串左边的空白字符 无参数 返回字符串或数组 indi.lvzhaojun.script.method.LtrimMethod RTRIM variableName.rtrim() 删除字符串或数组中字符串右边的空白字符 无参数 返回字符串或数组 indi.lvzhaojun.script.method.RtrimMethod LENGTH variableName.length() 返回字符串或数组的长度 无参数 返回整数 indi.lvzhaojun.script.method.LengthMethod UPPER variableName.upper() 将字符串中的英文字符转为大写字符 无参数 返回字符串 indi.lvzhaojun.script.method.UpperMethod LOWER variableName.lower() 将字符串中的英文字符转为小写字符 无参数 返回字符串 indi.lvzhaojun.script.method.LowerMethod SPLIT variableName.split() 或 variableName.split(delimiter) 或 variableName.split(delimiter, escape) 使用分隔符参数与转义字符参数对字符串进行分割 无参数时,表示默认使用空白字符串作为分隔符分割字符串 分割之后的字符串数组 indi.lvzhaojun.script.method.SplitMethod 参数名:delimiter 类型:字符串 范围:不能是空白字符 参数名:escape 类型:字符串 范围:只能是非空的单字符 GETFILENAME stringVariableName.getFilename() 返回文件名(不包含文件目录) 无参数 文件名字符串 indi.lvzhaojun.script.method.GetFilenameMethod GETFILELINESEPARATOR stringVariableName.getFileLineSeparator() 返回文件中的行间分隔符 无参数 文件的行间分隔符 indi.lvzhaojun.script.method.GetFileLineSeparatorMethod GETFILEEXT stringVariableName.getFileExt() 返回文件名中的扩展名 无参数 文件名扩展名字符串 indi.lvzhaojun.script.method.GetFileExtMethod GETFILENAMENOEXT stringVariableName.getFilenameNoExt() 返回文件名,但不包含扩展名; 文件扩展名: txt 或 exe 或 zip 等 无参数 文件名字符串 indi.lvzhaojun.script.method.GetFilenameNoExtMethod GETFILESUFFIX stringVariableName.getFileSuffix() 返回文件名的后缀 无参数 文件名后缀字符串 indi.lvzhaojun.script.method.GetFileSuffixMethod GETFILENAMENOSUFFIX stringVariableName.getFilenameNoSuffix() 返回文件名,但不包含文件名后缀; 文件名后缀: tar.gz 或 txt 或 exe 无参数 文件名字符串 indi.lvzhaojun.script.method.GetFilenameNoSuffixMethod GETPARENT stringVariableName.getParent() 返回文件的父目录的绝对路径 无参数 父目录的绝对路径字符串 indi.lvzhaojun.script.method.GetParentMethod DELETEFILE stringVariableName.deleteFile() 删除文件或目录 无参数 true表示删除文件或目录成功 indi.lvzhaojun.script.method.DeleteFileMethod EXISTSFILE stringVariableName.existsFile() 判断文件或目录是否存在 无参数 true表示文件或目录存在 indi.lvzhaojun.script.method.ExistsFileMethod ISFILE stringVariableName.isFile() 判断文件是否存在 无参数 true表示文件存在 indi.lvzhaojun.script.method.IsFileMethod ISDIRECTORY stringVariableName.isDirectory() 判断目录是否存在 无参数 true表示目录存在 indi.lvzhaojun.script.method.IsDirectoryMethod MKDIR stringVariableName.mkdir() 创建目录 无参数 true表示创建目录成功 indi.lvzhaojun.script.method.MkdirMethod TOUCH stringVariableName.touch() 创建文件 无参数 true表示创建文件成功 indi.lvzhaojun.script.method.TouchMethod LS stringVariableName.ls() 显示文件详细信息 无参数 文件详细信息字符串 indi.lvzhaojun.script.method.LsMethod FORMAT dateVariableName.format('yyyy-MM-dd') 或 stringVariableName.format(yyyy-MM-dd) 将日期变量使用 pattern 格式格式化并输出字符串 参数名:pattern 类型:字符串 范围:日期正则表达式 日期时间字符串 indi.lvzhaojun.script.method.FormatMethod 将日期字符串变量转为日期并按参数 pattern 格式输出日期信息 INDEXOF variableName.indexOf(string, from) or variableName.indexOf(string) 搜索字符串参数string 在字符串或字符串数组中首次出现的位置 参数名: string 类型: 字符串 范围: 必填且不能是null或空字符串 字符串参数首次出现的位置,从0开始,-1表示未找到 indi.lvzhaojun.script.method.IndexOfMethod 参数名: from 类型: 整数 范围: 开始搜索的起始位置,选填且大于等于零且小于字符串长度或数组长度 GETYEAR dateVariableName.getYear() 返回日期的年份 无参数 整数 indi.lvzhaojun.script.method.GetYearMethod GETMONTH dateVariableName.getmonth() 返回日期的月份 无参数 1-12 indi.lvzhaojun.script.method.GetMonthMethod GETDAY dateVariableName.getDay() 返回日期的天数 无参数 1-31 indi.lvzhaojun.script.method.GetDayMethod GETDAYS dateVariableName.getDays() 返回日期从1970年1月1日零点开始直到日期时点的天数 无参数 正整数 indi.lvzhaojun.script.method.GetDaysMethod GETHOUR timeVariableName.getHour() 返回日期时间的小时数 无参数 0-23 indi.lvzhaojun.script.method.GetHourMethod GETMINUTE timeVariableName.getMinute() 返回日期时间的分钟数 无参数 0-59 indi.lvzhaojun.script.method.GetMinuteMethod GETSECOND timeVariableName.getSecond() 返回日期时间的秒数 无参数 0-59 indi.lvzhaojun.script.method.GetSecondMethod GETMILLIS timeVariableName.getMillis() 返回日期时间的毫秒数 无参数 0-999 indi.lvzhaojun.script.method.GetMillisMethod INT strVariableName.int() 将字符串转为整数 无参数 整数 indi.lvzhaojun.script.method.IntMethod ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 如果以上变量方法不能满足需求,开发人员可以通过自定义变量方法的方式来实现自己的业务逻辑,自定义的变量方法必须实现接口 indi.lvzhaojun.script.UniversalScriptVariableMethod,且实现类上需要配置 indi.lvzhaojun.annotation.ScriptVariableFunction 注解。 indi.lvzhaojun.annotation.ScriptVariableFunction.name() 返回变量方法的名字。 indi.lvzhaojun.annotation.ScriptVariableFunction.keywords() 返回关键字数组(关键字不能作为变量名使用)。 五、表达式说明: 支持在 set,if,while 等命令中使用表达式进行计算。 开发人员可以使用类 indi.lvzhaojun.script.UniversalScriptExpression 完成如下表达式运算: 算数运算 () +(正) -(负) *(乘) /(除) %(取余) +(加) -(减) 三目运算 ?: 布尔运算 < <= > >= == != 逻辑运算 && || and or 范围运算 in 与 not in 运算符的返回值是布尔值,判断变量是否在设置的范围内,操作符右侧是小括号,小括号内的元素用符号 , 分割。 取反运算 ! 只支持对布尔值进行取反 六、支持数据库: 脚本引擎中数据库操作与相关命令都依赖于通过 JDBC 接口访问数据库中数据,在使用数据库相关功能和命令前需要满足如下条件: 1)数据库必须支持 JDBC 驱动,且 JDBC 驱动包已加入 classpath 下。 2)数据库对应的数据库方言类已加入到 classpath 下。 当前支持的数据库如下图所示,也可以通过新建数据库方言类的方式来增加对其他品牌据库的支持,或对数据库不同版本的支持。 ------------------------------------------------------------------------------ 数据库 说明 数据库方言类名 -------- ---------------------- -------------------------------------------- db2 11.1 indi.lvzhaojun.database.db2.DB2Dialect111 db2 11.5 indi.lvzhaojun.database.db2.DB2Dialect115 db2 IBM DB2 数据库方言模版 indi.lvzhaojun.database.db2.DB2Dialect mysql Mysql 数据库方言模版 indi.lvzhaojun.database.mysql.MysqlDialect oracle Oracle 数据库方言模版 indi.lvzhaojun.database.oracle.OracleDialect ------------------------------------------------------------------------------ 如:现在想增加对 informix 数据库的支持,需要新建一个方言类,且需要在方言类上配置 indi.lvzhaojun.annotation.BeanImplement 注解: @indi.lvzhaojun.annotation.BeanImplement(kind = "informix", mode = "", major = "", minor = "", description = "", type = indi.lvzhaojun.database.DatabaseDialect.class) public class InformixDialect extends indi.lvzhaojun.database.internal.AbstractDialect implements indi.lvzhaojun.database.DatabaseDialect { ... } indi.lvzhaojun.database.DatabaseDialect.type() 方法返回方言支持的数据库方言接口 indi.lvzhaojun.database.DatabaseDialect.class indi.lvzhaojun.database.DatabaseDialect.kind() 方法返回方言支持的数据库种类(主要用于根据条件匹配数据库方言)。 indi.lvzhaojun.database.DatabaseDialect.mode() 方法返回方言支持的模式(主要用于根据条件匹配数据库方言)。 indi.lvzhaojun.database.DatabaseDialect.major() 方法返回方言支持的数据库大版本号(必须是整数或为空)。 indi.lvzhaojun.database.DatabaseDialect.minor() 方法返回方言支持的数据库小版本号(必须是整数或为空)。 indi.lvzhaojun.database.DatabaseDialect.description() 方法返回数据库方言说明信息(可以为空)。 开发人员可以通过 indi.lvzhaojun.bean.BeanFactory.getBean(Class, Object;) 方法得到数据库连接对应的数据库方言,这个方法的第一个参数是 indi.lvzhaojun.database.DatabaseDialect.class,第二个参数可以是一个数据库连接或注解上 kind() mode() 返回值。 如果第二个参数是数据库连接时:根据数据库连接的 getDatabaseMajorVersion() 与 getDatabaseMinorVersion() 方法返回的大版本号与小版本号进行匹配, 优先使用大版本号与小版本号匹配的数据库方言类,如果不能匹配到对应的版本号时,会使用注解中未设置版本号的数据库方言,如果方言注解都设置了版本号则优先返回版本最近的一个方言。 如果第二个参数是字符串时:用字符串参数值与数据库方言类注解上的 kind() 方法返回值进行匹配,判断应该返回哪一个数据库方言。 其他组件与其实现类: indi.lvzhaojun.database.load.Loader indi.lvzhaojun.database.load.parallel.ParallelLoadFileEngine indi.lvzhaojun.database.load.serial.SerialLoadFileEngine indi.lvzhaojun.database.db2.DB2Command indi.lvzhaojun.database.db2.DB2LinuxCommand indi.lvzhaojun.mail.Mail indi.lvzhaojun.mail.MailImpl indi.lvzhaojun.script.UniversalScriptConfiguration indi.lvzhaojun.script.internal.ScriptConfiguration indi.lvzhaojun.script.UniversalScriptConverter indi.lvzhaojun.script.internal.ScriptConverter indi.lvzhaojun.script.UniversalScriptCompiler indi.lvzhaojun.script.compiler.ScriptCompiler 即时编译器 indi.lvzhaojun.script.UniversalScriptChecker indi.lvzhaojun.script.compiler.ScriptChecker indi.lvzhaojun.os.OSFtpCommand indi.lvzhaojun.os.ftp.FtpCommand indi.lvzhaojun.os.ssh.SftpCommand jsch-0.1.51 indi.lvzhaojun.os.OSSecureShellCommand indi.lvzhaojun.os.ssh.SecureShellCommand jsch-0.1.51 indi.lvzhaojun.database.DatabaseConfigurationContainer -> indi.lvzhaojun.database.internal.StandardDatabaseConfigurationContainer indi.lvzhaojun.database.export.ExtractReader -> indi.lvzhaojun.database.export.inernal.ReaderBuilder indi.lvzhaojun.database.export.ExtractWriter -> indi.lvzhaojun.database.export.ExtractWriterBuilder indi.lvzhaojun.database.export.inernal.HttpRequestWriter 卸载数据到用户浏览器 indi.lvzhaojun.database.export.inernal.FtpFileWriter 卸载数据到远程ftp服务器 indi.lvzhaojun.database.export.inernal.SftpFileWriter 卸载数据到远程sftp服务器 indi.lvzhaojun.database.export.inernal.ExtractFileWriter 卸载数据到本地文件 indi.lvzhaojun.bean.Codepage -> indi.lvzhaojun.bean.CodepageBuilder indi.lvzhaojun.bean.NationalHoliday -> indi.lvzhaojun.bean.NationalHolidayBuilder indi.lvzhaojun.bean.ConstructorTypeHoliday indi.lvzhaojun.bean.internal.ChinaNationalHoliday 中国法定节假日 indi.lvzhaojun.zip.Compress -> indi.lvzhaojun.zip.CompressBuilder indi.lvzhaojun.zip.ZipCompress indi.lvzhaojun.zip.GzipCompress indi.lvzhaojun.zip.RarCompress indi.lvzhaojun.zip.TarCompress indi.lvzhaojun.increment.IncrementReplace -> indi.lvzhaojun.increment.IncrementReplaceBuilder indi.lvzhaojun.os.OS -> indi.lvzhaojun.os.OSBuilder indi.lvzhaojun.os.macos.MacOS indi.lvzhaojun.os.linux.LinuxLocalOS indi.lvzhaojun.os.linux.LinuxRemoteOS indi.lvzhaojun.iox.TableLineRuler -> indi.lvzhaojun.iox.TableLineRulerBuilder indi.lvzhaojun.iox.TextTableFile -> indi.lvzhaojun.iox.TextTableFileBuilder indi.lvzhaojun.database.db2.DB2ExportFile DB2数据库export命令导出文件格式, 逗号分隔,双引号转义字符,双引号是字符串限定符 indi.lvzhaojun.iox.CsvFile CSV格式文件 indi.lvzhaojun.iox.CommonTextTableFile 文本文件, 逗号分隔,无转义字符,无字符串限定符 命令 标准输出语句 语法 echo [-n] string[;] 说明 常用选项: -n # 选项用于表示输出信息后不追加回车换行符 \ # 反斜杠: 转义字符 echo on # 表示打开信息输出 echo off # 表示关闭信息输出 echo "${variable}" # 输出变量,双引号中的变量在输出之前会被替换 echo '${variable}' # 使用单引号围住输出信息表示输出字符常量 echo "${variable}" 1>`pwd`/echo.log 2>`pwd`/echo_err.log # 输出标准信息与错误信息到日志文件 echo "${variable}" 1>`pwd`/echo.log 2>&1 # 输出标准信息与错误信息到日志文件 命令 设置局部变量 语法 set varname=value[;] 说明 在变量的赋值表达式中可以引用已有的全局变量、局部变量、全局JDBC配置信息、脚本引擎内部变量, 如: set varname=${count} + 1; set varname=1+1; # 对数值型变量使用加减乘除运算 set varname='1'+'2'; # 对字符型变量进行加法操作 set varname=varname.substr(1, 2).length(); # 使用变量方法修改变量值, 详见变量方法使用说明 set varname='content'; # 设置字符型变量 set varname="content"; # 设置字符型变量 set varname=0; # 设置数值型变量 set varname=select count(*) from tablename;# 可以保存 SQL 语句查询结果 脚本引擎内部变量如下: pwd 如果正在执行脚本文件, pwd 表示脚本文件所在目录的绝对路径 scriptName 如果正在执行脚本文件, scriptName 表示脚本文件的名字 charset 如果正在执行脚本文件, GBK 表示脚本文件的字符集编码 lineSeparator 如果正在执行脚本文件, \n 表示脚本文件中的行间分隔符 exception 如果脚本引擎发生异常, exception 表示异常详细信息 errorscript 如果脚本引擎发生异常, errorscript 表示报错的语句 errorcode 如果脚本引擎发生数据库错误, errorcode 表示数据库厂商定义的错误码 sqlstate 如果脚本引擎发生数据库错误, sqlstate 表示数据库厂商定义的SQL状态码 exitcode 表示最近一次执行语句的返回值 updateRows 表示最近一次执行sql语句影响的数据记录数 jump 表示当前是否处于 jump 语句中. 如果 jump 变量等于 true 表示脚本引擎正处于 jump 语句中 step 表示当前步骤变量值,即最近一个 step 命令的参数值 temp 表示临时文件所在目录 scriptFile 如果正在执行脚本文件, 表示当前脚本文件的绝对路径 变量方法使用说明详 variablemethod 命令的使用说明。 命令 执行变量方法 语法 变量名.变量方法 | 变量名[位置信息] 说明 如下图所示是所有变量方法 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 变量方法名 变量方法 使用说明 参数 返回值 类名 -------------------- ---------------------------------------------------------------------------------------------- ---------------------------------------------------------------- -------------------------------------------------------------------------------------------------- ----------------------------------------------- ------------------------------------------------------- PRINT variableName.print() 使用脚本引擎标准输出接口输出变量值 无参数 无 indi.lvzhaojun.script.method.PrintMethod [ variableName[index] 返回字符串指定位置的字符,返回数组指定位置的元素 参数名:index,类型:整数,范围:大于等于零且小于字符串长度或数组长度 字符串或数组元素 indi.lvzhaojun.script.method.ElementMethod SUBSTR variableName.substr(start, end) 或 variableName.substr(start) 截取字符串或数组 参数名: start 类型: 整数 范围: 大于等于零 小于字符串长度或数组长度 返回截取后的字符串或数组 indi.lvzhaojun.script.method.SubstrMethod 参数名: end 类型: 整数 范围: 大于等于零切小于等于 start 参数,截取后的值不包含 end 位置上的值 TRIM variableName.trim() 删除字符串或数组中字符串二端的空白字符 无参数 返回字符串或数组 indi.lvzhaojun.script.method.TrimMethod LTRIM variableName.ltrim() 删除字符串或数组中字符串左边的空白字符 无参数 返回字符串或数组 indi.lvzhaojun.script.method.LtrimMethod RTRIM variableName.rtrim() 删除字符串或数组中字符串右边的空白字符 无参数 返回字符串或数组 indi.lvzhaojun.script.method.RtrimMethod LENGTH variableName.length() 返回字符串或数组的长度 无参数 返回整数 indi.lvzhaojun.script.method.LengthMethod UPPER variableName.upper() 将字符串中的英文字符转为大写字符 无参数 返回字符串 indi.lvzhaojun.script.method.UpperMethod LOWER variableName.lower() 将字符串中的英文字符转为小写字符 无参数 返回字符串 indi.lvzhaojun.script.method.LowerMethod SPLIT variableName.split() 或 variableName.split(delimiter) 或 variableName.split(delimiter, escape) 使用分隔符参数与转义字符参数对字符串进行分割 无参数时,表示默认使用空白字符串作为分隔符分割字符串 分割之后的字符串数组 indi.lvzhaojun.script.method.SplitMethod 参数名:delimiter 类型:字符串 范围:不能是空白字符 参数名:escape 类型:字符串 范围:只能是非空的单字符 GETFILENAME stringVariableName.getFilename() 返回文件名(不包含文件目录) 无参数 文件名字符串 indi.lvzhaojun.script.method.GetFilenameMethod GETFILELINESEPARATOR stringVariableName.getFileLineSeparator() 返回文件中的行间分隔符 无参数 文件的行间分隔符 indi.lvzhaojun.script.method.GetFileLineSeparatorMethod GETFILEEXT stringVariableName.getFileExt() 返回文件名中的扩展名 无参数 文件名扩展名字符串 indi.lvzhaojun.script.method.GetFileExtMethod GETFILENAMENOEXT stringVariableName.getFilenameNoExt() 返回文件名,但不包含扩展名; 文件扩展名: txt 或 exe 或 zip 等 无参数 文件名字符串 indi.lvzhaojun.script.method.GetFilenameNoExtMethod GETFILESUFFIX stringVariableName.getFileSuffix() 返回文件名的后缀 无参数 文件名后缀字符串 indi.lvzhaojun.script.method.GetFileSuffixMethod GETFILENAMENOSUFFIX stringVariableName.getFilenameNoSuffix() 返回文件名,但不包含文件名后缀; 文件名后缀: tar.gz 或 txt 或 exe 无参数 文件名字符串 indi.lvzhaojun.script.method.GetFilenameNoSuffixMethod GETPARENT stringVariableName.getParent() 返回文件的父目录的绝对路径 无参数 父目录的绝对路径字符串 indi.lvzhaojun.script.method.GetParentMethod DELETEFILE stringVariableName.deleteFile() 删除文件或目录 无参数 true表示删除文件或目录成功 indi.lvzhaojun.script.method.DeleteFileMethod EXISTSFILE stringVariableName.existsFile() 判断文件或目录是否存在 无参数 true表示文件或目录存在 indi.lvzhaojun.script.method.ExistsFileMethod ISFILE stringVariableName.isFile() 判断文件是否存在 无参数 true表示文件存在 indi.lvzhaojun.script.method.IsFileMethod ISDIRECTORY stringVariableName.isDirectory() 判断目录是否存在 无参数 true表示目录存在 indi.lvzhaojun.script.method.IsDirectoryMethod MKDIR stringVariableName.mkdir() 创建目录 无参数 true表示创建目录成功 indi.lvzhaojun.script.method.MkdirMethod TOUCH stringVariableName.touch() 创建文件 无参数 true表示创建文件成功 indi.lvzhaojun.script.method.TouchMethod LS stringVariableName.ls() 显示文件详细信息 无参数 文件详细信息字符串 indi.lvzhaojun.script.method.LsMethod FORMAT dateVariableName.format('yyyy-MM-dd') 或 stringVariableName.format(yyyy-MM-dd) 将日期变量使用 pattern 格式格式化并输出字符串 参数名:pattern 类型:字符串 范围:日期正则表达式 日期时间字符串 indi.lvzhaojun.script.method.FormatMethod 将日期字符串变量转为日期并按参数 pattern 格式输出日期信息 INDEXOF variableName.indexOf(string, from) or variableName.indexOf(string) 搜索字符串参数string 在字符串或字符串数组中首次出现的位置 参数名: string 类型: 字符串 范围: 必填且不能是null或空字符串 字符串参数首次出现的位置,从0开始,-1表示未找到 indi.lvzhaojun.script.method.IndexOfMethod 参数名: from 类型: 整数 范围: 开始搜索的起始位置,选填且大于等于零且小于字符串长度或数组长度 GETYEAR dateVariableName.getYear() 返回日期的年份 无参数 整数 indi.lvzhaojun.script.method.GetYearMethod GETMONTH dateVariableName.getmonth() 返回日期的月份 无参数 1-12 indi.lvzhaojun.script.method.GetMonthMethod GETDAY dateVariableName.getDay() 返回日期的天数 无参数 1-31 indi.lvzhaojun.script.method.GetDayMethod GETDAYS dateVariableName.getDays() 返回日期从1970年1月1日零点开始直到日期时点的天数 无参数 正整数 indi.lvzhaojun.script.method.GetDaysMethod GETHOUR timeVariableName.getHour() 返回日期时间的小时数 无参数 0-23 indi.lvzhaojun.script.method.GetHourMethod GETMINUTE timeVariableName.getMinute() 返回日期时间的分钟数 无参数 0-59 indi.lvzhaojun.script.method.GetMinuteMethod GETSECOND timeVariableName.getSecond() 返回日期时间的秒数 无参数 0-59 indi.lvzhaojun.script.method.GetSecondMethod GETMILLIS timeVariableName.getMillis() 返回日期时间的毫秒数 无参数 0-999 indi.lvzhaojun.script.method.GetMillisMethod INT strVariableName.int() 将字符串转为整数 无参数 整数 indi.lvzhaojun.script.method.IntMethod ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 命令 脚本引擎支持管道符操作 语法 命令一 | 命令二 | .. | 命令N 说明 cat `pwd`/text | tail -n 1 命令 脚本引擎支持命令替换操作 语法 `..` 说明 使用场景如下: 在赋值语句中使用: set currentstr=`echo 20210101 | date "yyyy-MM-dd"` 在布尔表达式中使用: if `cat $temp/headtest.log | tail -n 1` != 5 then exit 1 fi 在表达式中使用: 'set loadtask_stdout="现在是" +`date -d ${tmp_enddate} yyyy年MM月dd日`+", 进行日期检查!"' 在输出语句中使用: echo `currentdate.format(yyyy-MM-dd)` != '2020-01-01' 在字符串中使用: if "`date -d 20201213 'yyyy-MM-dd' +0day`" != "2020-12-13" then echo "`date -d 20201213 'yyyy-MM-dd' +0day`" != "2020-12-13" exit 122 fi 命令 自定义方法 语法 function 方法名() { .. } 说明 在方法体中使用 $1 表示外部输入的参数1, $0 表示方法名, $# 表示输入参数个数, 使用 return 语句退出方法体. 需要注意在方法体中不能使用 step 与 jump 语句 . 脚本引擎保留的方法名: function step() {echo $1;} 执行 step 命令的处理逻辑 . function echo() {echo $1;} 执行 echo 命令的处理逻辑 . function error() {echo $1;} 执行错误的处理逻辑 命令 执行用户自定义方法 语法 functionName [参数]...[;] 说明 例子: function test() {echo $1;} # 定义一个方法 test “hello world!” 命令 设置全局变量 设置全局用户自定义方法 语法 export set name = value [;] export function 用户自定义方法名 [;] 说明 子脚本会自动继承父脚本中定义的全局变量与全局用户自定义方法 命令 建立步骤标记 语法 step 步骤名[;] 说明 1.标记当前执行位置,配合 jump 命令使用可以实现跳转到标记位置处开始执行的效果, 如下: # script start set jumpvar="string1" jump ${jumpvar} ... step string1 delete from ... ; insert into ... ; commit; ... step string2; ... 2.配合 jump 与 function() error() {} 命令使用可以实现从脚本上一次执行报错位置开始向下执行的效果,跳过已执行部分, 如下: # script start function error() { # 记录报错时 step 位置信息, 用于下一次从报错处开始执行 insert into table ... ... } # 查询上一次执行位置信息 set jumpvar=select ... ; # 跳转到上一次执行位置 jump ${jumpvar} ... step string1 delete from ... ; insert into ... ; commit; ... step string2; ... 命令 跳转到 step 命令位置后继续向下执行命令 语法 jump 步骤名[;] 说明 配合 step 命令可以实现跳转到指定 step 标记, 详见 step 命令使用说明. 在找到 step 命令前会根据命令的 enableJump() 方法的返回值判断是否需要执行命令 在脚本文件中可以使用内置变量 jump 判断当前脚本引擎是否处于 jump 命令状态, 如: # script start jump stepMessage; ... if "$jump" == "true" then ... else ... fi ... step stepMessage ... 命令 退出当前正在执行的语句 语法 exit 返回值 说明 命令的返回值只能是整数,返回零表示执行正确,返回非零表示执行错误, 如: exit 0; exit -1; exit 1 命令 执行SQL语句 语法 [sql] [ select .. | insert .. | delete .. | update .. | merge .. | alter .. | create .. | drop .. ]; 说明 SQL语句结尾处必须用 ; 符号表示结束 需要在SQL语句中把字符 $ 替换成 &ads; 内置变量名 updateRows 表示SQL语句影响记录的笔数, 如:echo SQL共更新 ${updateRows} 条记录! 命令 定义数据库编目信息 语法 declare [global] 数据库编目名 catalog configuration use driver 数据库驱动类名 url 数据库JDBC的URL路径 username 用户名 password 密码 [;] declare [global] 数据库编目名 catalog configuration use file JDBC配置文件绝对路径 [;] 说明 global是可选选项,表示数据库编目配置信息是全局可被子脚本继承使用 数据库驱动类名是必填选项,二端可用单引号或双引号包住 数据库JDBC的URL路径是必填选项,二端可用单引号或双引号包住 用户名是必填选项,二端可用单引号或双引号包住 密码是必填选项,二端可用单引号或双引号包住 JDBC配置文件中必须要有 driverClassName 属性,url 属性,username 属性,password 属性。 host 属性是可选选项表示数据库服务器的主机名或IP地址 admin 与 adminPw 属性是可选选项表示数据库实例用户名和密码 sshUser,sshUserPw 与 sshPort 属性是可选选项分别表示数据库服务器SSH协议端口,用户名,密码。 使用实例: declare global name catalog configuration use file /home/udsf/jdbc.properties; declare global name catalog configuration use driver com.ibm.db2.jcc.DB2Driver url 'jdbc:db2://127.0.0.1:50000/databaseName' username admin password admin; 命令 建立数据库连接 关闭当前数据库连接 语法 db connect to 数据库编目名[;] db connect reset 命令 从数据库中卸载数据到指定位置 语法 db export to 卸载位置 of 数据类型 [ modified by 参数名=参数值 参数名=参数值 参数名 ] select * from table ; 说明 默认支持的卸载位置说明: 将数据卸载到本地操作系统上: 本地操作系统文件绝对路径 将数据卸载到远程sftp服务器上: sftp://用户名@远程服务器host:端口?password=登陆密码/数据文件存储路径 将数据卸载到远程 ftp服务器上: ftp://用户名@远程服务器host:端口?password=登陆密码/数据文件存储路径 将数据通过http响应下载给用户: http://download/HttpServletRequest对象的变量名/HttpServletResponse对象的变量名/下载文件名(需要提前将 HttpServletRequest 对象与 HttpServletResponse 对象保存到脚本引擎变量中,变量分别是: httpServletRequest, httpServletResponse) 实现用户自定义卸载位置: 卸载位置的用户自定义格式: bean://类型值/模式值/大版本号/小版本号 卸载位置的用户自定义JAVA类的样例: @indi.lvzhaojun.annotation.BeanImplement(kind = "kind", mode = "mode", major = "major", minor = "minor", type = indi.lvzhaojun.database.export.ExtractWriter.class) public class UserDefineWriter implements indi.lvzhaojun.database.export.ExtractWriter { .. } 使用方式: db export to bean://kind/mode/major/minor/../.. of txt select * from table; 已实现的用户自定义卸载位置: http 卸载数据到用户浏览器 indi.lvzhaojun.database.export.inernal.HttpRequestWriter ftp 卸载数据到远程ftp服务器 indi.lvzhaojun.database.export.inernal.FtpFileWriter sftp 卸载数据到远程sftp服务器 indi.lvzhaojun.database.export.inernal.SftpFileWriter local 卸载数据到本地文件 indi.lvzhaojun.database.export.inernal.ExtractFileWriter 默认支持的数据类型说明: del DB2数据库export命令导出文件格式, 逗号分隔,双引号转义字符,双引号是字符串限定符 indi.lvzhaojun.database.db2.DB2ExportFile csv CSV格式文件 indi.lvzhaojun.iox.CsvFile txt 文本文件, 逗号分隔,无转义字符,无字符串限定符 indi.lvzhaojun.iox.CommonTextTableFile 实现用户自定义数据类型: 必须实现 indi.lvzhaojun.iox.TextTable 接口上的方法 如: 自定义 csv 数据类型: @indi.lvzhaojun.annotation.BeanImplement(kind = "csv", mode = "", major = "", minor = "", description = "csv文件格式", type = indi.lvzhaojun.iox.TextTable.class) public class CsvExtractStyle implements indi.lvzhaojun.iox.TextTable { .. public CsvExtractStyle() .. .. } 参数说明: catalog 表示数据库编目编号,用于设置从哪个数据源中卸载数据,默认使用脚本引擎当前数据库编目 message 消息文件绝对路径参数, 可以保存卸数数据任务的运行消息,如果文件路径无法访问时默认将消息文件保存到用户跟目录下的 $HOME/messagefile/export 目录下。 listener 任务生命周期监听器集合, 每个监听器的 java 类名之间用半角逗号分隔,监听器类必须实现 indi.lvzhaojun.database.export.UserListener 接口 convert 数据集字段的处理逻辑集合, 格式:字段名:字段处理逻辑类名,格式: JAVA处理逻辑类名?属性名=属性值&属性名=属性值 其中字段处理逻辑类名必须实现 indi.lvzhaojun.database.JdbcObjectConverter 接口, 可以在类名后使用 “?属性名=属性值” 格式向处理逻辑中设置属性 使用半角逗号分隔 convert 参数映射关系的优先级 高于 数据库方言提供的映射关系 charset 表示数据卸载后的字符集,默认使用 JVM 的 file.encoding 参数作为默认值 codepage 表示数据卸载后的字符集,默认使用 JVM 的 file.encoding 参数作为默认值(与 charset 属性冲突) rowdel 行间分隔符,使用回车或换行符需要转义,如: \\r \\n coldel 每行记录中字段之间的分隔符 escape 表示转义字符,只能是单字符 colname 表示向卸载位置上写入数据前先写入标题信息, 无参数值时表示使用字段名作为列标题信息;有参数值时表示使用参数值作为列标题信息(标题间用半角逗号分隔,标题为空时默认使用标题位置对应的字段名作为列标题,使用 1:name 表示第一个位置对应 name 列名) chardel 字符串字段二端的限定符 charhide 字符串字段的字符过滤器参数,指定哪些字符需要过滤,未设置参数时默认过滤回车符和换行符 escapes 表示对字符型字段值中哪些字段进行转义,卸载字符型字段值时使用转义字符进行转义 outbuf 数据输出流中缓冲区的行数(必须大于零),默认缓冲 100 行 append 写入数据的方式, 设置 true 时表示将数据追加写入到卸载位置上,false 表示覆盖原有数据,默认 false maximum 表示最大行数, 超过最大行数时将后续数据写入到一个新存储上,通过增加数字编号区分不同存储信息,默认 0 表示无限制 dateformat 表示日期格式 timeformat 表示时间格式 timestampformat 表示时间戳格式 progress 表示卸载数据的进度输出接口编号,需要提前在脚本引擎中定义进度输出接口 db export 命令支持 container 命令,可以大批量并行执行多个数据卸载命令 命令 将指定位置的数据文件装载到数据库表中 语法 db load from 文件位置 of 数据类型 [ method P(3,2,1) C(字段名, 字段名) ] [ modified by 参数名=参数值 参数名=参数值 参数名 ] [ replace | insert | merge ] into table[(字段名,字段名,字段名)] [ for exception tableName ] [ indexing mode [ rebuild | incremental ]] [ statistics use profile ] [ prevent repeat operation ]; 说明 一、文件位置说明: 用于指定数据文件绝对路径,可以指定多个文件路径(用半角逗号分割,文件结构必须一致)。 二、数据类型说明: 1)默认支持的数据类型有: del DB2数据库export命令导出文件格式, 逗号分隔,双引号转义字符,双引号是字符串限定符 indi.lvzhaojun.database.db2.DB2ExportFile csv CSV格式文件 indi.lvzhaojun.iox.CsvFile txt 文本文件, 逗号分隔,无转义字符,无字符串限定符 indi.lvzhaojun.iox.CommonTextTableFile 2)实现用户自定义数据类型: 必须实现 indi.lvzhaojun.iox.TextTable 接口上的方法 如: 自定义 csv 数据类型: @indi.lvzhaojun.annotation.BeanImplement(kind = "csv", mode = "", major = "", minor = "", description = "csv文件格式", type = indi.lvzhaojun.iox.TextTable.class) public class CsvExtractStyle implements indi.lvzhaojun.iox.TextTable { .. public CsvExtractStyle() .. .. } 三、参数说明: catalog 表示数据库编目编号,用于设置从哪个数据源中卸载数据,默认使用脚本引擎当前数据库编目。 tableCatalog 表示数据库表所属数据库的编目 launch 表示装数引擎启动条件,属性值可以是类名或脚本语句(脚本语句返回值是0表示可以执行数据装载,返回值是非0不能执行数据装载) convert 数据集字段的处理逻辑集合, 格式:字段名:字段处理逻辑类名?属性名=属性值&属性名=属性值,用于设置字段名数据转换器的映射关系 savecount 表示每装载 n 行后建立一致点。消息文件中将生成和记录一些消息,用于表明在保存点所在时间上有多少输入行被成功地装载。 dateformat 表示日期字符串格式 timeformat 表示时间字符串格式 timestampformat 表示时间戳字符串格式 keepblanks 表示将数据装入到一个变长列时,会截断尾部空格,若未指定则将保留空格。只有参数名无需设置参数值; chardel 用于指定字符串二段的限定符(如字符串二段使用双引号限定范围)。 readbuf 用于指定输入流缓冲区长度(单位字节),可以是 100M 或 1G 这种格式 message 用于指定消息文件绝对路径 coldel 用于指定每行记录中字段间的分隔符 escapechar 用于指定字符型字段中的转义字符 nocrlf 表示自动删除字符串中的回车符和换行符,只有参数名无需设置参数值 dumpfile 用于指定错误数据存储文件路径 codepage 用于指定数据文件的代码页 progress 用于指定装载文件的进度输出接口编号,需要提前在脚本引擎中定义进度输出接口 四、数据装载模式说明: replace 表示先清空数据库表中所有数据,然后再读取文件批量插入到数据库表中。 insert 表示插入模式:读取文件内容,并将文件每行记录通过批量接口插入到数据库表中。 merge 表示合并模式:读取文件内容,并将文件每行记录通过批量接口插入到数据库表中。如果数据在数据库表中已存在则使用文件内容更新,如果数据不存在则将文件行插入到表中。 _ 使用合并模式时,需要使用 method C(字段名, 字段名) 语句设置判断记录是否相等的字段名。 五、设置字段顺序: 可以使用 method P(3,2,1) 句柄设置文件中列插入到数据库表的顺序,如 method P(3,2,1) 表示按第三列,第二列,第一列的顺序读取文件中每列数据并插入到数据库表中。 可以在数据库表名后面使用小括号与字段名的方式指定向数据库表中插入字段的顺序,如:tableName(fname1,fname3,fname2),表示按 fname1,fname3,fname2 字段顺序插入数据。 六、其他句柄说明: for exception tableName 句柄用于指定对于主键冲突错误问题,自动将重复记录保存到 for 语句指定的表中。 prevent repeat operation 句柄用于指定装载文件之前,先根据消息文件中的内容判断是否需要重新装载数据文件,只有参数名无需设置参数值。 indexing mode [ rebuild | incremental ] 句柄用于设置索引模式,rebuild 表示先删除索引文件装载成功后重建索引,incremental 表示只向索引中添加新的数据。 statistics use profile 句柄表示在数据文件装载成功后,因为数据库表中添加了更多的数据,导致之前的目标表统计信息很可能已经无效了。可以为数据库表重新收集统计信息。 db load 命令支持 container 命令,可以大批量并行执行多个数据文件装载命令。 命令 对比二个表格型文件并抽取增量数据 语法 extract increment compare 新文件 of 数据类型 modified by 属性名=属性值 and 旧文件 of 数据类型 modified by 属性名=属性值 write [new [and] upd [and] del] into filepath [of 数据类型] [modified by 属性名=属性值] [write log into [filepath | stdout | stderr]] 说明 参数名说明: 新文件表示新文件的绝对路径 旧文件表示旧文件的绝对路径 数据类型表示文件内容对应的类型标识符,当前脚本引擎支持的类型有: del DB2数据库export命令导出文件格式, 逗号分隔,双引号转义字符,双引号是字符串限定符 indi.lvzhaojun.database.db2.DB2ExportFile csv CSV格式文件 indi.lvzhaojun.iox.CsvFile txt 文本文件, 逗号分隔,无转义字符,无字符串限定符 indi.lvzhaojun.iox.CommonTextTableFile 可以定义增量数据输出流将新增数据,变化数据,删除数据输出到指定文件中,如下面语句表示将新增数据与变化的数据写入到 filepath 文件中,且新增数据的第一个字段修改为false,第二个字段内容修改 uuid,第三个字段内容修改为格式是yyyyMMddHHmmss的当前时间 write new and upd into /home/user/inc.txt of txt modified by newchg=1:false,2:uuid updchg=3:date=yyyyMMddHHmmss 可以将新增,变化,删除数据都写入到一个文件中(如果输出流中未定义数据类型时,默认使用新文件的数据类型),如: write into /home/user/inc.txt modified by newchg=1:uuid 可以定义日志信息输出流(非必填)将增量日志写入到指定文件中或脚本引擎的标准输出流中,如: write log into /home/user/inc.log write log into stdout write log into stderr 新旧文件输出流支持的属性有: index: 必填,表示文件中唯一确定一条记录的索引字段集合,格式: 字段位置信息, 如:1,2,3,4 如果已设置 table 属性则可以使用表中字段名如: id,name,age,value compare: 表示文件中比较字段(相同索引字段时,用于区分二条记录是否相等的字段,如果二条记录中的索引字段与比较字段都相等则认为二条记录相等),格式: 字段位置信息如:1,2,3,4 如果已设置 table 属性则可以使用表中字段名如:name,age,val1,val2。未设置参数时会默认比较记录中每个字段值 table: 表示文件中字段对应的数据库表名(可以是 schema.tableName 格式) catalog: 表示脚本引擎中定义的数据库编目号 charset 表示数据卸载后的字符集,默认使用 JVM 的 file.encoding 参数作为默认值 codepage 表示数据卸载后的字符集,默认使用 JVM 的 file.encoding 参数作为默认值(与 charset 属性冲突) readbuf: 表示读取文件时使用的字符缓冲区长度,默认 100M 个字符 coldel: 表示文件中字段间的分隔符 escape: 表示文件中字符串中的转义字符 column: 表示文件中每行记录的字段个数(如果记录的字段数不等于这个值时会抛出异常) colname: 表示文件中字段名,格式是:位置信息:字段名,如: 1:客户名,2:客户编号 如果已设置 table 属性则可以使用表中字段名如:username:客户名,2:userage progress: 表示脚本引擎中已定义的进度输出编号,用于输出文件的读取进度信息 nosort: 设置 true 表示剥离增量之前不会排序文件,默认是 false 表示先排序文件然后再执行剥离增量 sortcache: 排序文件输出流使用的缓冲行数,默认是 100 行 sortrows: 排序文件时每个临时文件的最大行数,默认是 10000 行 sortThread: 排序文件时的线程数,默认是 3 个线程 sortReadBuf:排序文件时的输入流的缓冲区长度,单位:字符 maxfile: 排序文件时,每个线程每次合并的最大临时文件数, 默认是 4 个文件 keeptemp: 设置 true 表示排序文件后保留临时文件,默认是 false 表示删除产生的临时文件 sortclass: 字符串排序规则的类名(实现 java.util.Comparator 接口),默认类名是 indi.lvzhaojun.iox.TableColumnComparator covsrc: 设置 true 表示排序文件后覆盖源文件,默认是 false 表示保留源文件内容 增量数据输出流支持的属性有: newchg: 表示对新增数据中字段的替换规则 updchg: 表示对变化数据中字段的替换规则 delchg: 表示对删除数据中字段的替换规则 charset: 表示文件对应的字符集编码,默认使用JAVA虚拟机默认的文件字符集 codepage: 表示文件对应的代码页(与 charset 属性冲突) append: 设置 true 表示追加方式写入文件,默认是 false 表示覆盖文件 outbuf: 设置输出流的缓冲行数,默认是 20 行 剥离增量命令支持 container 命令,可以大批量并行执行多个剥离增量命令 命令 建立并发任务的运行容器,并立即运行并发任务 语法 container to execute tasks in parallel [ using 参数名=参数值 参数名=参数值 参数名 ] begin 并发任务 end 说明 参数名说明: thread 表示同时运行任务的数量,如未设置参数默认同时最多执行3个任务 参数会作为并发任务的默认参数值 container to execute tasks in parallel using thread=3 rowdel=\r\n coldel=: begin db export to $filepath1.del of del select * from table with ur; db export to $filepath2.del of del select * from table with ur; db export to $filepath3.del of del select * from table with ur; db export to $filepath4.del of del select * from table with ur; db export to $filepath5.del of del select * from table with ur; db export to $filepath6.del of del select * from table with ur; end 命令 提交当前数据库连接上的事务 语法 commit[;] 命令 回滚当前数据库连接上的事务 语法 rollback[;] 命令 以静默方式执行命令 语法 quiet 命令语句; 说明 如果命令发生错误时不会抛出异常或输出错误信息. quiet select * from table; quiet commit 命令 执行数据库存储过程 语法 call SCHEMA.PRODUCENAME(?)[;] 说明 例子: call SYSPROC.ADMIN_CMD('reorg indexes all for table ALLOW READ ACCESS'); call TEST('read in msg', ?); call TEST('read in msg', $RES); echo $RES; 命令 遍历数据库游标 语法 cursor 游标名 loop .. end loop 说明 可以在循环体中使用 break, continue, return 控制语句 break 命令:退出当前循环 continue 命令:执行下一次循环 return 命令:退出当前方法 命令 while 循环 语法 while .. loop .. end loop 说明 可以在循环体中使用 break, continue, return 控制语句 break 命令:退出当前循环 continue 命令:执行下一次循环 return 命令:退出当前方法 命令 for 循环 语法 for 变量名 in 数组表达式 loop .. end loop 说明 for 循环语句用于便利数组中的元素信息,每次循环会按顺序将数组中的元素保存到变量名中。 继而可以通过变量名在循环体中引用数组中的变量。 数组表达式范围: 1)可以是替换命令如:`ls` 2)可以是一个数组变量名,如: ${var} 3)可以是字符串常量,如:(1,2,3,4) 可以在循环体中使用 break, continue, return 控制语句 break 命令:退出当前循环 continue 命令:执行下一次循环 return 命令:退出当前方法 命令 读取文件或文本信息 语法 while read 变量名 do .. done < [ filepath | command ] 说明 可以在循环体中使用 break, continue, return 控制语句 break 命令:退出当前循环 continue 命令:执行下一次循环 return 命令:退出当前方法 # 循环遍历 /home/user 目录下的文件 while read line do .. done < ls /home/user # 逐行读取文件中内容 while read line do .. done < /home/user/list.txt 命令 if语句 语法 if .. then .. elsif .. then .. elseif .. then .. fi 说明 支持嵌套使用 命令 登录 ssh 服务器并执行命令 语法 ssh username@host:port?password= && . /etc.profile && . ~/.profile && shell command [;] 说明 例子: ssh admin@192.168.1.1:10?password=admin && ./shell.sh && . ~/load.sh 命令 建立本地端口转发隧道 语法 建立隧道命令: declare 隧道名 ssh tunnel use proxy 代理服务器用户名@代理服务器HOST:代理服务器SSH端口号?password=密码 connect to 本地端口号:远程服务器HOST:远程服务器SSH端口号 [;] 其中本地服务器端口号为零时表示端口由操作系统随机分配,随机分配的端口号通过标准输出接口输出 关闭隧道命令: undeclare 隧道名 ssh tunnel [;] 说明 配合 sftp 命令实现通过本地局域网代理服务器访问远程服务器ssh端口的功能, 例子: # 建立隧道并获取本地端口号 set localport=`declare sshname ssh tunnel use proxy root@192.168.1.10:22?password=root connect to 0:192.168.10.20:22 | tail -n 1` # 建立sftp连接 'sftp test@127.0.0.1:${localport}?password=test' put `pwd`/file.txt /home/test bye # 关闭隧道 undeclare sshname tunnel 命令 建立SFTP连接 语法 sftp 用户名@服务器HOST:端口?password=密码 说明 可以使用如下 SFTP 命令操作文件: cd filepath 进入远程服务器目录 ls filepath 查看远程服务器上文件列表信息 rm filepath 删除远程服务器上文件或目录 mkdir filepath 在远程服务器上创建目录 pwd filepath 查看远程服务器上当前目录的绝对路径 exists filepath 判断远程服务器上的文件或目录是否存在 isfile filepath 判断远程服务器的文件是否存在 isDirectory filepath 判断远程服务器的目录文件是否存在 get remotefilepath localfilepath 从远程服务器下载文件 put localfilepath remotefilepath 上传文件到远程服务器 bye 关闭 SFTP 连接 在 cd ls rm mkdir pwd exists isFile isDirectory 语句中可以使用 -l 选项, 表示操作本地操作系统上的文件 命令 建立FTP连接 语法 ftp 用户名@服务器HOST:端口?password=密码 说明 可以使用如下FTP命令操作文件: cd filepath 进入远程服务器目录 ls filepath 查看远程服务器上文件列表信息 rm filepath 删除远程服务器上文件或目录 mkdir filepath 在远程服务器上创建目录 pwd filepath 查看远程服务器上当前目录的绝对路径 exists filepath 判断远程服务器上的文件或目录是否存在 isfile filepath 判断远程服务器的文件是否存在 isDirectory filepath 判断远程服务器的目录文件是否存在 get remotefilepath localfilepath 从远程服务器下载文件 put localfilepath remotefilepath 上传文件到远程服务器 bye 关闭 FTP 连接 在 cd ls rm mkdir pwd exists isFile isDirectory 语句中可以使用 -l 选项, 表示操作本地操作系统上的文件 命令 显示本地目录下的文件或远程ftp服务器当前目录下文件 语法 ls 文件名或文件路径 [;] 说明 -l 表示文件名或文件路径是本地操作系统文件路径 文件名或文件路径二端可以使用成对的单引号或双引号 命令 进入本地目录或远程服务器目录 语法 cd 文件名或文件路径 [;] 说明 -l 表示文件名或文件路径是本地操作系统文件路径 文件名或文件路径二端可以使用成对的单引号或双引号 命令 显示本地文件或远程文件的大小,或测量字符串的长度 语法 length string; length -h string; length -b string; length -c string; length -f filepath; length -r remotefilepath; 说明 -h 选项表示输出可读高的信息 -b 选项表示显示字节数 -c 选项表示显示字符数 -f 选项表示本地文件的字节数 -r 选项表示显示远程文件的字节数 命令 显示本地目录路径或远程ftp服务器当前目录路径 语法 pwd [-l] [;] 说明 -l 选项表示显示本地操作系统上的目录 命令 创建本地目录或在远程服务器上创建目录 语法 mkdir [-l] 文件名或文件路径 [;] 说明 -l 选项表示文件名或文件路径是本地操作系统上的文件 文件名或文件路径二端可以使用成对的单引号或双引号 命令 删除本地文件或目录或远程ftp服务器上的文件或目录 语法 rm [-l] 文件名或文件路径 [;] 说明 -l 选项表示文件名或文件路径是本地操作系统上的文件 文件名或文件路径二端可以使用成对的单引号或双引号 命令 判断文件是否存在或远程ftp服务器上是否存在文件 语法 [!]isfile [-l] 文件名或文件路径 [;] 说明 -l 选项表示文件名或文件路径是本地操作系统上的文件 文件名或文件路径二端可以使用成对的单引号或双引号 命令 判断目录文件是否存在或远程ftp服务器上是否存在目录 语法 [!]isDirectory [-l] 文件名或文件路径 [;] 说明 -l 选项表示文件名或文件路径是本地操作系统上的文件 文件名或文件路径二端可以使用成对的单引号或双引号 命令 判断文件路径在本地是否存在 或 远程ftp服务器上是否存在文件路径 语法 [!]exists [-l] 文件名或文件路径 [;] 说明 -l 选项表示文件名或文件路径是本地操作系统上的文件 文件名或文件路径二端可以使用成对的单引号或双引号 命令 输出文件内容 语法 cat 文件名或文件路径 [;] 说明 文件名或文件路径二端可以使用成对的单引号或双引号 命令 输出文件前几行的内容 语法 head [-n 行号] 文件名或文件路径 [;] 说明 head -n 10 /home/user/file.txt # 表示输出文件前10行的内容 文件名或文件路径二端可以使用成对的单引号或双引号 命令 输出文件结尾几行的内容 语法 tail [-n 行号] 文件名或文件路径 [;] 说明 tail -n 1 /home/user/file.txt # 表示输出文件结尾最后一行的内容 文件名或文件路径二端可以使用成对的单引号或双引号 命令 显示文件的行数 字数 字节数 文件名 语法 wc [-l] [-w] [-c] filepath [;] 说明 -l 选项表示行数 -w 选项表示字符数 -c 选项表示字节数 文件名或文件路径二端可以使用成对的单引号或双引号 命令 显示当前操作系统的文件系统信息 语法 df [;] 说明 df 命令的输出信息: 第一个字段是文件系统 第二个字段是总容量 第三个字段是剩余容量 第四个字段是已用容量 第五个字段是文件系统类型(如: ext4) 第六个字段是挂载位置信息 命令 将文件或字符串中的行间分隔符转为换行符 语法 dos2unix 文件路径|字符串 [;] 说明 无选项 命令 过滤数据 语法 grep string 说明 -i 选项表示忽略字符大小写 -v 选项表示不包括字符串参数 可以在管道符后过滤前一个命令的标准输出信息 cat $temp/greptest.txt | grep -i test | wc -l 命令 执行本地操作系统命令 语法 os command [;] 说明 os cd /home/user/dir os ipconfig /all 脚本引擎当前默认命令是 os 时,执行本地命令语句时可以不使用 os 前缀 命令 执行脚本文件 语法 . 文件名或文件路径 [;] source 文件名或文件路径 [;] 说明 可以在脚本文件前使用 nohup 命令实现并行执行脚本文件,如: nohup . /home/user/script.sql & - 可以使用 >> 或 > 字符将日志信息输出到指定文件 - 可以使用 1>> stdlogfile 表示将标准输出信息写入 stdlogfile 日志 . - 可以使用 2>> errlogfile 表示将错误输出信息写入 errlogfile 日志 . - 可以使用 2>&1 语句将标准输出与错误输出都写到同一个日志文件. - 可以使用 wait 语句等待并行脚本执行完毕,并返回脚本的返回值 子脚本可继承父脚本的全局变量、全局的数据库编目配置信息、全局的用户自定义方法、全局的异常错误处理逻辑、全局的 echo 命令处理逻辑、全局的错误处理逻辑、全局的步骤输出逻辑 命令 执行脚本文件 语法 daemon 文件名或文件路径 [;] 说明 与 source 命令不同点: 脚本执行完毕后,会将脚本产生的局部变量,全局变量,全局的数据库编目信息同步到当前脚本引擎中 命令 建立进度输出逻辑 语法 declare [global] [任务编号] progress use 输出方式 print 输出信息 total 总循环次数 times [;] 说明 -m 可选选项, 选项用于多任务进度信息输出 任务编号是可选选项, 用于在多并发任务时区分不同任务的唯一编号 global是可选选项, 用于表示是全局进度输出逻辑(可被子脚本继承) 输出方式是必填选项, 用于设置进度输出方式: out表示使用标准输出 err表示使用错误输出 step表示使用step输出 总循环次数是必填选项,用于设置总循环次数,只能是整数数值 输出信息中可以使用 ${process} 输出当前进度百分比 输出信息中可以使用 ${totalRecord} 输出总循环次数 输出信息中可以使用 ${leftTime} 输出预估的剩余时间 输出信息中可以使用 ${taskId} 输出多任务输出的任务编号 例如: # 定义一个进度输出信息 declare global progress use step print "正在更新数据库记录 ${process}, 共有 ${totalRecord} 笔数据记录, ${leftTime} " total 10000 times while ... loop # 进度输出 progress ... end loop 命令 建立异常处理逻辑 语法 declare (exit | continue) handler for ( exception | exitcode != 0 | sqlstate == '02501' | errorcode -803 ) begin .. end 说明 可以使用脚本引擎中保留变量: exception 当脚本引擎发生异常时, exception 表示异常详细信息 errorcode 当脚本引擎发生数据库错误时, errorcode 表示数据库厂商提供的错误码 sqlstate 当脚本引擎发生数据库错误时, sqlstate 表示数据库厂商提供的SQL状态 errorscript 当脚本引擎发生异常错误时, errorscript 表示发生错误的脚本语句 exitcode 当脚本引擎执行语句完毕时, exitcode 表示语句执行的返回值, 一般来讲返回0表示正确 非0表示错误 命令 删除异常处理逻辑 语法 undeclare handler for ( exception | exitcode == 0 | sqlstate == 120 | sqlcode == -803 ) ; 命令 显示异常处理逻辑 语法 handler[;] 说明 打印脚本引擎当前的 echo方法处理逻辑,error方法处理逻辑,step方法处理逻辑,所有异常处理逻辑 命令 建立数据库批处理逻辑 语法 declare 批处理名字 statement by 笔数 batch with insert into table (f1,f2) values (?,?) ; 说明 可以使用 FETCH 变量名1, 变量名2, .. INTO 批处理名字; 语句批量更新数据库中数据 可以使用 undeclare 批处理名字 statement; 语句关闭批处理程序. 例子: declare s1 statement by 1000 batch with insert into table (f1,f2) values (?,?) ; set val1='1' set val2='2' FETCH val1, val2 insert s1; undeclare s1 statement; 命令 建立数据库查询游标 语法 declare 游标名 cursor with return for select * from table ; 说明 可以使用 cursor 游标名 loop .. end loop 语句遍历游标对象. 可以使用 fetch cursorName into variableName1, variableName2, variableName3; # 语句将游标中当前行的字段保存到自定义变量中. 可以使用 undeclare 游标名 cursor; 语句关闭游标. 例子: db connect to databasename declare cno cursor with return for select * from table ; cursor cno loop fetch cno into tmp_val1, tmp_val2, tmp_val3; echo ${tmp_val1} ${tmp_val2} ${tmp_val3} end loop undeclare cno cursor; 命令 后台执行命令 语法 nohup 命令语句 [&] [;] 说明 nohup . /home/user/script.sql & # 并行执行脚本 set pid=`nohup . scriptfile.sql & | tail -n 1` # 后台执行脚本并获取脚本 pid 编号 命令 终止脚本引擎中所有的用户会话 或 终止脚本引擎中某个用户会话 或 终止当前用户会话中的进程; 语法 terminate [-p 后台进程编号] [-s 用户会话编号] [;] 说明 terminate; # 表示终止脚本引擎中所有的用户会话 terminate -s Mc6e4645c26d94666a0a65621078aaeff; # 表示终止脚本引擎中的一个用户会话 terminate -p 21; # 表示终止当前用户会话中的一个进程 脚本引擎会调用命令的 terminate() 方法以尝试终止命令执行 即: 脚本引擎是否会立即退出执行取决于当前执行命令的 terminate() 方法实现. 命令 等待子进程执行完毕 语法 wait pid=进程编号 [ 1m | 1s | 1h | 1day ][;] 说明 使用语句 [ 1m | 1s | 1h | 1day ] 设置等待子进程的超时时间, 超时后自动退出 day 表示单位是自然日 h 表示单位是小时 m 表示单位是分钟 s 表示单位是秒 millis 表示单位是毫秒 命令 查看进程信息 语法 ps [-s] [;] 说明 -s 选项表示显示脚本引擎中所有用户会话 后台进程信息说明: 进程号 表示进程编号 脚本行号 表示脚本语句所在行号 是否有效 表示进程状态:true表示正在运行 已被终止 表示进程终止:true表示已终止进程 启动时间 表示进程启动时间 结束时间 表示进程结束时间 返回值 表示进程返回值 运行命令 表示进程执行命令 用户会话信息说明: 会话编号 表示用户会话的编号 父会话编号 表示用户会话的父用户会话编号 是否有效 表示用户会话的状态 已被终止 表示用户会话是否已终止 * 表示是当前用户会话信息 命令 使当前进程进入休眠 语法 sleep 1s[;] 说明 支持的休眠时间的单位有:day|h|m|s|millis day 表示单位是自然日 h 表示单位是小时 m 表示单位是分钟 s 表示单位是秒 millis 表示单位是毫秒 命令 日期命令 语法 date [-d datetime string] { pattern } [ +|- number day|month|year|hour|minute|second|millis ]* [;] 说明 -d 设置日期格式, 可以使用单引号或双引号包住 支持的日期格式: y+.*MM.*dd yyyy-MM-dd, e.g: 2017-01-01 || 2017/01/01 || 2017.01.01 # 年月日之间的分隔符可以是以下字符之一: - / | \ _ : : . 。 MM/dd/yyyy yyyy-M-d, e.g: 2017-1-1 yyyyMMdd yyyyMMddHH yyyyMMddHHmm yyyyMMddHHmmss yyyyMMddHHmmssSSS yyyy-MM-dd hh yyyy-MM-dd hh:mm yyyy-MM-dd hh:mm:ss yyyy-MM-dd hh:mm:ss:SSS Sun Oct 11 00:00:00 GMT+08:00 1998 Sun Oct 11 00:00:00:000 GMT+08:00 1998 二零一七年十二月二十三 1998年10月11日 31 december 2017 at 08:38 31 dec 2017 date # 输出当前日期时间,显示格式:yyyy-MM-dd hh:mm:ss date -d 2020-01-01 yyyyMMdd # 格式化指定日期,-d参数值格式详见“支持的日期格式” date + 1 day # 当前时间加一天 命令 输出默认命令 设置默认命令 语法 default [;] default [sql | os] [;] 说明 在脚本引擎不能识别语句时, 会自动使用默认命令执行语句. default sql; # 不能识别语句时默认作为sql执行 default os; # 不能识别语句时默认作为本地操作系统命令执行 命令 搜索文件 语法 find -n string [-r] [-h] [-e charsetName] [-o logfilepath] [-s delimiter] [-d] [-p] filepath [;] 说明 -n 搜索内容(可以是正则表达式) -R 只遍历当前目录 -h 查找隐藏文件 -e 被搜索文件的字符集 -o 输出文件 -s 输出信息的分隔符 -d 去掉重复记录 -p 显示字符串所在位置的详细信息 命令 执行JAVA类 语法 java JavaClassName [参数]... [;] 说明 被执行的 java 类需要继承 indi.lvzhaojun.script.command.AbstractJavaCommand, 需要实现抽象方法 execute() 需要通过实现 terminate() 方法终止运行中的命令 可以通过实现 boolean enableJump() 方法通知脚本引擎在执行 jump 命令时是否可以不执行 int execute(UniversalScriptSession session, UniversalScriptContext context, UniversalScriptStdout stdout, UniversalScriptStderr stderr, String[] args) 方法. 例子: java indi.lvzhaojun.script.cmd.JavaCommandTest 10 -c 20200101 命令 生成唯一32位字符串 语法 uuid[;] 命令 生成MD5值 语法 md5sum 文件名或文件路径 md5sum 字符内容 说明 生成文件或字符内容的MD5值 命令 压缩文件或目录 解压文件 语法 tar -zcvf 文件名或绝对路径 tar -xvf 文件名或绝对路径 说明 压缩文件: tar -zcvf 文件名或绝对路径 解压文件: tar -xvf 文件名或绝对路径 命令 压缩文件或目录 语法 gzip 文件名或绝对路径 命令 解压文件 语法 gunzip 文件名或绝对路径 命令 压缩文件或目录 语法 zip 文件名或绝对路径 命令 解压文件 语法 unrar 文件名或绝对路径 命令 解压文件 语法 unzip 文件名或绝对路径