kettle系列教程二
kettle系列教程二 1、Hello World 示例 通过这个例子,介绍kettle的一些基础知识及应用: n 使用Spoon工具 n 转换的创建 n 步骤及连线 n 预定义变量 n 在Spoon中预览和执行 n 使用pan工具在终端窗口执行转换 概述 我们要实现的目标是:通过一个包含人名称的C





kettle系列教程二
1、Hello World 示例
通过这个例子,介绍kettle的一些基础知识及应用:
n 使用Spoon工具
n 转换的创建
n 步骤及连线
n 预定义变量
n 在Spoon中预览和执行
n 使用pan工具在终端窗口执行转换
概述
我们要实现的目标是:通过一个包含人名称的CSV文件,创建一个XML文件,内容是针对每个人添加上问候。
如果csv文件内容如下:
last_name, name
Suarez,Maria
Guimaraes,Joao
Rush,Jennifer
Ortiz,Camila
Rodriguez,Carmen
da Silva,Zoe
则输出的XML文件内容如下:
- <Rows>
- <row>
<msg>Hello, Maria!</msg>
</row>
- <row>
<msg>Hello, Joao!</msg>
</row>
- <row>
<msg>Hello, Jennifer!</msg>
</row>
- <row>
<msg>Hello, Camila!</msg>
</row>
- <row>
<msg>Hello, Carmen!</msg>
</row>
- <row>
<msg>Hello, Zoe!</msg>
</row>
</Rows>
我们要设计的第一个转换就是创建由csv文件生成一个问候XML文件。
转换是由步骤和连接组成,这些步骤和连接构成数据流,因此转换是基于数据流的。
环境准备
开始设计一个转换之前,我们先创建一个文件夹Tutorial,我们将保存所有的文件到该文件夹中,然后创建一个csv文件,内容就是前面人员信息,你可以复制到文本文件中,然后命名为list.csv。
设计转换
有三个子任务组成:
ü 创建一个转换
ü 设计转换中的步骤和连接
ü 配置步骤和连接诶
创建转换
1) 点击文件菜单中新建,或者通过快捷键Ctrl+N,选择转换.
2) 在“主对象树”视图中点右击转换1,然后点击设置,或者在设计界面中右击,然后点击转换设置,或者使用Ctrl+T快捷方式。
3) 在设置窗口中可以设置转换的相关属性;我们这里写名称和描述,然后保存。
4) 保存该转换到Tutorial文件夹中,命名为hello,最终创建了hello.ktl文件。
设置转换中的步骤和连接
步骤是转换中的最小设计单元,spoon中有很多不同的步骤,放在不同的分组中,如输出、输入等。每个步骤被设计完成一个特定的功能,如读取参数或格式化一个数据集。
连接是两个步骤之间连接的图形展示,从源到目标。流经连接的数据构成源步骤的输出数据,目标步骤的输入数据。一般连接有一个源步骤和一个目标步骤,但是有时从一个源步骤出来多个连接,这时输出数据流可以是复制或分发给不同的目标步骤;同样多个连接到达一个目标步骤,则需要目标步骤能够合并来自不同源步骤的输入数据。
hello.ktl文件需要实现以下功能:读取csv文件、针对每个人员添加问候、保存问候信息到xml文件中;三个环节使用不同的步骤实现,如下图:
这个例子,步骤之间连接是一对一关系,比较简单,详细的设计流程如下:
1) 在核心对象的输入类别中选择CSV文件输入,拖动到右边工作区中;
2) 在脚本类别,拖动javascript代码到右边工作区中;
3) 在输出类别,拖动xml文件输出到工作区;
现在把CSV文件输入和javascript代码两个步骤连接起来
1) 选择第一个步骤;
2) 按住shift键并拖动第一个步骤的图标到目标步骤;
3) 按照同样的方式连接javascript代码和xml文件输出两个步骤;
配置步骤行为
每个步骤都有一个配置窗口,不同类别下的不同步骤配置内容不同,但是步骤名称总是需要设置成有意义的名称,步骤描述是对步骤实现功能的描述。
配置CSV文件输入步骤
1) 双击CSV文件输入步骤
2) 弹出配置窗口界面,我们设置文件位置、格式、内容等;
3) 替换默认步骤名称,一般设置和业务相关的名称,这里我们使用name list
4) 文件名称需要填写文件名称及位置
注:在文本框的右边有菱形变量图标,表示该文本框中可以使用变量;变量的格式为${name_of_variable},我们可以定位光标在文本框中,然后按快捷键(ctrl+Alt+Space),文本框的下拉框中会出现预定义的变量及用户定义变量,当然我们目前没有定义任何变量。我们写如下内容:
${Internal.Transformation.Filename.Directory}/list.csv
当程序运行时,变量会被替换成当前转换文件的路径,然后在该目录下搜索 list.csv文件;
5) 点击获取字段,增加输入文件的列名称到表格中,该操作默认假设CSV文件有行标题。
6) 表格中展现了列名称last_name和name,如下图:
7) 取消“简易转换”(懒转换)
8) 点击预览,确认文件正确加载,会弹出窗口显示数据。
9) 点击确定,完成CSV文件输入配置。
配置javascript代码步骤
1) 双击javascript代码步骤,弹出配置窗口
2) 与前面步骤不同,这里可以写javascript代码,输入“hello”,连接到每个名称前面。
3) 命名该步骤为Greetings.
4) 在配置窗口的主区域写代码,左边窗口,有一组变量及函数,可以在代码中使用;尤其是最后两个节点,输入字段和输出字段,可以在代码中使用;这个例子中有两个字段last_name和name,写入下面代码:
var msg = 'Hello, ' + name.getString() + "!";
注:name.getString()可以手动写入,或在左边树双击自动录入。
5) 在最下面的表格里,可以写代码中创建的任何变量作为输出流中的字段;这里我们创建一个msg变量,因为我们需要把msg内容写到xml输出文件中。如下图所示。
注:不要混淆这里的代码变量和kettle内置变量,他们完全不同。
6) 点击确定完成JavaScript代码步骤配置。
7) 选择刚才配置的步骤(JavaScript代码),为了检查离开这步骤的输出新的字段,我们将查看输入及输出字段。输入字段就是到达该步骤的数据列;输出字段就是离开步骤的数据列;有些步骤简单传输输入数据,这时,输入数据和输出数据通常相同;有些步骤增加字段到输出流中,如:计算步骤;还有一些步骤过滤或合并数据,这样输出数据中字段减少相对输入数据字段,如:分组步骤。
8) 在该步骤上点击右键,弹出上下文菜单。
9) 选择显示输入字段,可以看到输入字段,last_name和name,来自csv文件输入步骤。
10) 选择显示输出字段,可以不仅有已经存在的字段,而且还增加了msg字段。
配置XML文件输出步骤
1) 双击XML输出步骤,弹出配置界面,这里可以设置输出文件的名称和位置,以及输出字段。可以设置全部或部分字段;
2) 设置步骤名称:File With Greetings;
3) 在文件设置框中录入:${Internal.Transformation.Filename.Directory}/Hello.xml
4) 点击获取字段,用三个输入字段填充表格,输出文件中我们只想显示消息,所以删除name和last_name;
5) 再次保存该转换设计文件;
运行机制
当你执行一个转换,几乎所有步骤是同时执行的,因为转换时异步执行的;行数据流按照设计步骤流转,每个处理流执行到下一个步骤无需等待其他步骤。实际开发时,不了解这个特性,可能出现和设想不一样的结果。
至此,该示例基本配置完毕。该转换读取输入文件,然后通过javas代码步骤给每行添加问候,最后该内容被发送到输出文件中。因为例子中行数很少,很难看出了是异步执行。但是,切记,有可能在消息被写在输出文件的同时,有数据行刚离开第一步。
验证、预览和执行
1) 执行转换之前,检查每个属性配置实用验证按钮,spoon将验证语法是否正确,查找不可到达的步骤及不存在的连接。如何你一直跟着本教程的步骤练习,应该能验证通过,可以准备预览输出结果。
2) 选择JavaScript代码步骤,然后点击预览按钮,如下图:
3) 如你所见,spoon假设你预览你选择的步骤,点击快速启动,然后,弹出JavaScript代码步骤的数出数据,如何数据和你期望一致,我们准备执行转换。
4) 点击运行按钮在工具栏中。
5) spoon将弹出一个窗口,这里可以设置一些信息,如执行参数和日志级别等,点击运行。
6) 在主窗口的日志窗口中可以看到当前执行的日志信息及每步执行情况。
Read:来自前一步的数据行数;
Written:离开这步到下一步骤时的数据行数;
Input:从一个文件或表中读取的数据行数;
Output:往表或文件中写的数据行数;
Errors:执行中的错误信息,如果有错误,整行都是红色;
日志窗口中,可以看到每步骤执行情况。最后一行是:
Spoon - The transformation has finished!!
如果没有错误,可以打开新生成的Hello.xml文件,检查是否正确。
pan命令
pan是在终端执行转换的工具。在window是pan.bat,其他平台是pan.sh,在安装目录可以找到。如果你直接运行pan,不加任何选项,可以看到pan打印所有的选项。
简单命令如下:
pan /file
/norep选项是让spoon不要去连接资源库;
/file是转换文件的名称;
其他选项在运行时是缺省值;
你输入命令并确定后,转换同样被执行。这时,日志被打印到终端界面中,也可以设置日志打印到文件中。日志的格式有点变化,但内容和图形界面一致。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Dewu APP is currently a very popular brand shopping software, but most users do not know how to use the functions in Dewu APP. The most detailed usage tutorial guide is compiled below. Next is the Dewuduo that the editor brings to users. A summary of function usage tutorials. Interested users can come and take a look! Tutorial on how to use Dewu [2024-03-20] How to use Dewu installment purchase [2024-03-20] How to obtain Dewu coupons [2024-03-20] How to find Dewu manual customer service [2024-03-20] How to check the pickup code of Dewu [2024-03-20] Where to find Dewu purchase [2024-03-20] How to open Dewu VIP [2024-03-20] How to apply for return or exchange of Dewu

How to upgrade numpy version: Easy-to-follow tutorial, requires concrete code examples Introduction: NumPy is an important Python library used for scientific computing. It provides a powerful multidimensional array object and a series of related functions that can be used to perform efficient numerical operations. As new versions are released, newer features and bug fixes are constantly available to us. This article will describe how to upgrade your installed NumPy library to get the latest features and resolve known issues. Step 1: Check the current NumPy version at the beginning

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

1. First open WeChat. 2. Click [+] in the upper right corner. 3. Click the QR code to collect payment. 4. Click the three small dots in the upper right corner. 5. Click to close the voice reminder for payment arrival.

PhotoshopCS is the abbreviation of Photoshop Creative Suite. It is a software produced by Adobe and is widely used in graphic design and image processing. As a novice learning PS, let me explain to you today what software photoshopcs5 is and how to use photoshopcs5. 1. What software is photoshop cs5? Adobe Photoshop CS5 Extended is ideal for professionals in film, video and multimedia fields, graphic and web designers who use 3D and animation, and professionals in engineering and scientific fields. Render a 3D image and merge it into a 2D composite image. Edit videos easily

Testing a monitor when buying it is an essential part to avoid buying a damaged one. Today I will teach you how to use software to test the monitor. Method step 1. First, search and download the DisplayX software on this website, install it and open it, and you will see many detection methods provided to users. 2. The user clicks on the regular complete test. The first step is to test the brightness of the display. The user adjusts the display so that the boxes can be seen clearly. 3. Then click the mouse to enter the next link. If the monitor can distinguish each black and white area, it means the monitor is still good. 4. Click the left mouse button again, and you will see the grayscale test of the monitor. The smoother the color transition, the better the monitor. 5. In addition, in the displayx software we

With the continuous development of smart phones, the functions of mobile phones have become more and more powerful, among which the function of taking long pictures has become one of the important functions used by many users in daily life. Long screenshots can help users save a long web page, conversation record or picture at one time for easy viewing and sharing. Among many mobile phone brands, Huawei mobile phones are also one of the brands highly respected by users, and their function of cropping long pictures is also highly praised. This article will introduce you to the correct method of taking long pictures on Huawei mobile phones, as well as some expert tips to help you make better use of Huawei mobile phones.

The Xiaomi Mi 15 series is expected to be officially released in October, and its full series codenames have been exposed in the foreign media MiCode code base. Among them, the flagship Xiaomi Mi 15 Ultra is codenamed "Xuanyuan" (meaning "Xuanyuan"). This name comes from the Yellow Emperor in Chinese mythology, which symbolizes nobility. Xiaomi 15 is codenamed "Dada", while Xiaomi 15Pro is named "Haotian" (meaning "Haotian"). The internal code name of Xiaomi Mi 15S Pro is "dijun", which alludes to Emperor Jun, the creator god of "The Classic of Mountains and Seas". Xiaomi 15Ultra series covers
