Shell

本节介绍用于启动 shell 的选项以及与 shell 如何处理空格、引号和 SpEL 表达式解释相关的更高级功能。 Stream DSLComposed Task DSL 的介绍性章节是 shell 命令最常见用法的良好起点。spring-doc.cadn.net.cn

13. 外壳选项

shell 是建立在 Spring Shell 项目之上的。 一些命令行选项来自 Spring Shell,有些特定于数据流。 shell 采用以下命令行选项:spring-doc.cadn.net.cn

unix:>java -jar spring-cloud-dataflow-shell-2.11.5.jar --help
Data Flow Options:
  --dataflow.uri=                              Address of the Data Flow Server [default: http://localhost:9393].
  --dataflow.username=                        Username of the Data Flow Server [no default].
  --dataflow.password=                    Password of the Data Flow Server [no default].
  --dataflow.credentials-provider-command= Executes an external command which must return an
                                                    OAuth Bearer Token (Access Token prefixed with 'Bearer '),
                                                    e.g. 'Bearer 12345'), [no default].
  --dataflow.skip-ssl-validation=       Accept any SSL certificate (even self-signed) [default: no].
  --dataflow.proxy.uri=                  Address of an optional proxy server to use [no default].
  --dataflow.proxy.username=        Username of the proxy server (if required by proxy server) [no default].
  --dataflow.proxy.password=        Password of the proxy server (if required by proxy server) [no default].
  --spring.shell.historySize=                 Default size of the shell log file [default: 3000].
  --spring.shell.commandFile=                 Data Flow Shell executes commands read from the file(s) and then exits.
  --help                                            This message.

您可以使用spring.shell.commandFile选项指向包含 部署一个或多个相关流和任务的所有 shell 命令。 还支持运行多个文件。它们应该作为逗号分隔的字符串传递:spring-doc.cadn.net.cn

--spring.shell.commandFile=file1.txt,file2.txtspring-doc.cadn.net.cn

在创建某些脚本以帮助自动部署时,此选项非常有用。spring-doc.cadn.net.cn

此外,以下 shell 命令有助于将复杂的脚本模块化为多个独立文件:spring-doc.cadn.net.cn

dataflow:>script --file <YOUR_AWESOME_SCRIPT>spring-doc.cadn.net.cn

14. 列出可用命令

打字help在命令提示符下,给出了所有可用命令的列表。 大多数命令用于数据流功能,但也有一些是通用的。 以下列表显示了help命令:spring-doc.cadn.net.cn

Built-In Commands
       help: Display help about available commands
       stacktrace: Display the full stacktrace of the last error.
       clear: Clear the shell screen.
       quit, exit: Exit the shell.
       history: Display or save the history of previously run commands
       version: Show version info
       script: Read and execute commands from a file.

将命令的名称添加到help显示有关如何调用命令的其他信息:spring-doc.cadn.net.cn

dataflow:>help stream create
NAME
       stream create - Create a new stream definition

SYNOPSIS
       stream create [--name String] [--definition String] --description String --deploy boolean

OPTIONS
       --name String
       the name to give to the stream
       [Mandatory]

       --definition String
       a stream definition, using the DSL (e.g. "http --port=9000 | hdfs")
       [Mandatory]

       --description String
       a short description about the stream
       [Optional]

       --deploy boolean
       whether to deploy the stream immediately
       [Optional, default = false]

15. 制表符补全

您可以通过按TAB前导后的关键。例如,按--TABstream create --结果为以下一对建议:spring-doc.cadn.net.cn

dataflow:>stream create --
--definition    --deploy        --description   --name

如果您键入--de然后按 Tab 键,--definition扩展。spring-doc.cadn.net.cn

制表符补全功能也可在应用程序或任务属性的流或组合任务 DSL 表达式中使用。您还可以使用TAB在流 DSL 表达式中获取可用于的可用源、处理器或接收器的提示。spring-doc.cadn.net.cn

16. 空格和引用规则

仅当参数值包含空格或|字符。以下示例将 SpEL 表达式(应用于它遇到的任何数据)传递给变换处理器:spring-doc.cadn.net.cn

transform --expression='new StringBuilder(payload).reverse()'

如果参数值需要嵌入单引号,请使用两个单引号,如下所示:spring-doc.cadn.net.cn

// Query is: Select * from /Customers where name='Smith'
scan --query='Select * from /Customers where name=''Smith'''

16.1. 引号和转义

有一个基于 Spring Shell 的客户端与数据流服务器通信并负责解析 DSL。 反过来,应用程序可能具有依赖于嵌入式语言的应用程序属性,例如 Spring 表达式语言spring-doc.cadn.net.cn

Shell、数据流 DSL 解析器和 SpEL 具有关于它们如何处理引号以及语法转义如何工作的规则。 当组合在一起时,可能会出现混乱。 本节介绍适用的规则,并举例说明涉及所有三个组件时可能遇到的最复杂情况。spring-doc.cadn.net.cn

它并不总是那么复杂

如果不使用数据流 Shell(例如,如果直接使用 REST API)或应用程序属性不是 SpEL 表达式,则转义规则更简单。spring-doc.cadn.net.cn

16.1.1. shell 规则

可以说,在报价方面最复杂的组成部分是壳牌。不过,规则可以很简单地列出:spring-doc.cadn.net.cn

  • shell 命令由键 (--something) 和相应的值。不过,有一个特殊的无密钥映射,稍后将对此进行描述。spring-doc.cadn.net.cn

  • 值通常不能包含空格,因为空格是命令的默认分隔符。spring-doc.cadn.net.cn

  • 不过,可以通过用引号(单引号 () 或双引号 () )将值括起来来添加空格。'"spring-doc.cadn.net.cn

  • 在部署属性中传递的值(例如deployment <stream-name> --properties " …​")不应再次引用。spring-doc.cadn.net.cn

  • 如果用引号括起来,则值可以通过在前面加上反斜杠 () 来嵌入相同类型的文字引号。\spring-doc.cadn.net.cn

  • 其他转义也可用,例如\t,\n,\r,\f以及 Unicode 转义形式\uxxxx.spring-doc.cadn.net.cn

  • 无键映射以特殊方式处理,因此不需要引用即可包含空格。spring-doc.cadn.net.cn

例如,shell 支持!命令来执行本机 shell 命令。这!接受单个无键参数。这就是以下示例有效的原因:spring-doc.cadn.net.cn

dataflow:>! rm something

这里的论点是整体rm somethingstring,按原样传递给底层 shell。spring-doc.cadn.net.cn

再举一个例子,以下命令严格等效,参数值为something(不带引号):spring-doc.cadn.net.cn

dataflow:>stream destroy something
dataflow:>stream destroy --name something
dataflow:>stream destroy "something"
dataflow:>stream destroy --name "something"

16.1.2. 属性文件规则

从文件加载属性时,规则会放宽。spring-doc.cadn.net.cn

  • 属性文件中使用的特殊字符(Java 和 YAML)需要转义。例如,应该替换为 ,\\\\t\\t等等。spring-doc.cadn.net.cn

  • 对于 Java 属性文件 (--propertiesFile <FILE_PATH>.properties),属性值不应用引号括起来。即使它们包含空格,也不需要它。spring-doc.cadn.net.cn

    filter.expression=payload > 5
  • 对于 YAML 属性文件 (--propertiesFile <FILE_PATH>.yaml),但是,这些值需要用双引号括起来。spring-doc.cadn.net.cn

    app:
        filter:
            filter:
                expression: "payload > 5"

16.1.3. DSL 解析规则

在解析器级别(即在流或任务定义的主体内部),规则如下所示:spring-doc.cadn.net.cn

因此,的--expression选项在以下示例中在语义上是等效的:spring-doc.cadn.net.cn

filter --expression=payload>5
filter --expression="payload>5"
filter --expression='payload>5'
filter --expression='payload > 5'

可以说,最后一个更具可读性。这要归功于周围的引号。实际表达式是payload > 5.spring-doc.cadn.net.cn

现在,假设我们要针对字符串消息进行测试。如果我们想将有效负载与 SpEL 文字字符串进行比较,"something",我们可以使用以下内容:spring-doc.cadn.net.cn

filter --expression=payload=='something'           (1)
filter --expression='payload == ''something'''     (2)
filter --expression='payload == "something"'       (3)
1 这有效,因为没有空格。不过,它不是很清晰。
2 这使用单引号来保护整个参数。因此,实际的单引号需要加倍。
3 SpEL 识别带有单引号或双引号的 String 文字,因此最后一种方法可以说是最易读的。

请注意,前面的示例应在 shell 之外考虑(例如,直接调用 REST API 时)。 当在 shell 中输入时,整个流定义本身很可能位于双引号内,需要转义。整个示例如下所示:spring-doc.cadn.net.cn

dataflow:>stream create something --definition "http | filter --expression=payload='something' | log"

dataflow:>stream create something --definition "http | filter --expression='payload == ''something''' | log"

dataflow:>stream create something --definition "http | filter --expression='payload == \"something\"' | log"

16.1.4. SpEL 语法和 SpEL 文字

拼图的最后一块是关于 SpEL 表达式的。 许多应用程序接受要解释为 SpEL 表达式的选项,并且如前所述,字符串文字也以特殊方式处理。规则如下:spring-doc.cadn.net.cn

作为最后一个示例,假设您要使用转换处理器。 此处理器接受expression选项,这是一个 SpEL 表达式。它将根据传入消息进行评估,默认值为payload(原封不动地转发消息有效负载)。spring-doc.cadn.net.cn

重要的是要了解以下陈述是等效的:spring-doc.cadn.net.cn

transform --expression=payload
transform --expression='payload'

但是,它们与以下内容(以及它们的变体)不同:spring-doc.cadn.net.cn

transform --expression="'payload'"
transform --expression='''payload'''

第一个系列计算为消息有效负载,而后一个示例计算为文字字符串payload.spring-doc.cadn.net.cn

16.1.5. 将所有内容放在一起

作为最后一个完整的示例,考虑如何强制将所有消息转换为字符串文字hello world,通过在数据流 shell 的上下文中创建流:spring-doc.cadn.net.cn

dataflow:>stream create something --definition "http | transform --expression='''hello world''' | log" (1)

dataflow:>stream create something --definition "http | transform --expression='\"hello world\"' | log" (2)

dataflow:>stream create something --definition "http | transform --expression=\"'hello world'\" | log" (2)
1 在第一行中,单引号将字符串括起来(在数据流分析器级别),但它们需要加倍,因为它们位于字符串文本(由等号后面的第一个单引号开始)中。
2 第二行和第三行分别使用单引号和双引号,以包含数据流解析器级别的整个字符串。因此,可以在字符串内使用另一种引号。整个内容位于--definition但是,使用双引号的 shell 参数。因此,双引号被转义(在 shell 级别)。