DistChen

以http形式调用Kettle服务

在Kettle中创建好的Transformation、Jobs除了可以利用Spoon工具来执行外,还可以有其它的执行方式,如下:

  • 命令行调用
  • java 调用
  • 服务调用

这里就简单介绍下如何以服务的形式来访问、执行配置好的Transformation、Jobs。这里要利用Carte工具,我们使用kettle来搭建集群的时候肯定要用到此工具的。

配置文件

确保在%Kettle_Home%/pwd目录下存在这两个文件:

  • carte-config-master-8080.xml
  • kettle.pwd

截图中的另外几个配置文件可不要(除非你要搭建集群),carte-config-master-8080.xml的名称中的8080代表启动时的端口,可根据实际情况修改,对应的内容也要做出相应的修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<slave_config>
<!--
Document description...
- masters: You can list the slave servers to which this slave has to report back to.
If this is a master, we will contact the other masters to get a list of all the slaves in the cluster.
- report_to_masters : send a message to the defined masters to let them know we exist (Y/N)
- slaveserver : specify the slave server details of this carte instance.
IMPORTANT : the username and password specified here are used by the master instances to connect to this slave.
-->
<slaveserver>
<name>master1</name>
<hostname>localhost</hostname>
<port>8080</port>
<master>Y</master>
</slaveserver>
</slave_config>

kettle.pwd文件中存储的是服务器的用户名和密码(已加密),内容也可以根据实际使用情况修改。

用户名/密码设置

服务器启动后,默认用户名为cluster,密码为cluster。如果你想修改用户名或密码,需要修改上述的kettle.pwd配置文件,默认的配置如下:

1
2
3
4
# Please note that the default password (cluster) is obfuscated using the Encr script provided in this release
# Passwords can also be entered in plain text as before
#
cluster: OBF:1v8w1uh21z7k1ym71z7i1ugo1v9q

cluster是用户名,OBF:1v8w1uh21z7k1ym71z7i1ugo1v9qcluster加密后的值。使用 Encr工具可以用来生成密码,命令如下:

1
2
cd %Kettle_Home%
Encr -carte your_password

启动服务

执行如下命令即可启动服务器:

1
Carte hostname port

命令中的port需要与配置文件设置的port对应,正常启动后如下所示:

访问http://localhost:8080,输入用户名/密码即可验证是否正常。

调用服务

启动成功后,就可以调用服务来执行一些操作了,这里以执行一个配置好的Transformation为例:

http://localhost:8080/kettle/executeTrans/?rep=kettle54_local&user=admin&pass=admin&trans=services/demo

这个转换放在了仓库kettle54_local中,仓库的用户名是admin,密码是admin,所处目录是/services,转换的名称是demo,如下所示:

调用后,返回结果如下:

需要注意的是,如果在服务中想要得到响应结果,输出需要勾选此选项,xml输出等同理:

调用服务带参数

调用服务时,也可以传递参数,比如通过id查询一张表里面的某条记录时,数据如下所示:

如果我要查询id=100231的记录,则如下组织URL即可:

http://localhost:8080/kettle/executeTrans/?rep=kettle54_local&user=admin&pass=admin&trans=sh-sdd/BigData&`id=100231`

参数名可以任意设置,只要在获取变量时,对应上就可以了。转换中按照如下的形式即可提取出参数:

之后进行一次流查询即可查询指定ID的数据:

查询结果如下所示:

更多API可参考:http://help.pentaho.com/Documentation/5.4/0R0/070/020/020/020

坚持原创技术分享,您的支持将鼓励我继续创作!