Kettle and kettle

Source: Internet
Author: User

Kettle and kettle
Zookeeper

Access the previous row of data in kettle

In kettle, many users use the features of javascript steps when they need to access the first row of data in the data stream. In fact, it is not necessary to say that the javascript steps are not good, mainly because the performance is low, it also increases complexity because writing code makes javascript steps hard to understand. It is best to use kettle standard steps for implementation. Our principle is that the Code is minimized and the process is easy.

 

(The Analytic Query Step) analysis Query steps

The TheAnalytic Query step can be used to find the front or back rows in the data stream and set the value to the current row. In many cases, it is very useful, but I feel that it is usually ignored by developers. This step is included in the "statistics" classification. The following sections describe how to use it.

 

Scenario: Calculate the sum of inventory

Assume that you need to calculate the sum of a field in the data stream, such as inventory and. Raw data is the transaction information for each cargo, resulting in inventory increase or decrease, transactions are sorted by date. Calculate the current inventory value after each transaction and save it as a new field. The result is shown in:

Columns A, B, and C are input, and column D is calculated through the conversion step.

 

One solution is based on the steps of "Analytic Query" and "JavaScript modified Value"

The idea is to use the short javasript code to record the inventory of the current product, increase the transaction value, and save the results in the new field. If the data stream is sorted by product and date, you need to add an inventory counter for the current product. If the current product is a new product (relative to the previous line), the counter is set to 1; otherwise, it is set to 0. In general, many people access the previous line through javascript code. This method is not desirable. In addition to the javascript step, there is also the "Analytic Query" step. In this example, the javascript step is only based on the new field value to determine whether it is a new product. Try to keep the javascript step concise and reduce the error generation.

Here isSolution, Click to download:

First, read the data and sort the data by product and date. Then, add the product value in the previous line to the "prev_product" field. The next step is to compare the data with the current product, and save the flag value in the "is_new_product" field. It is very easy to use the previous information in the subsequent javascript steps.

The javascript code is as follows:

Varstock;

If (is_new_product = 1 ){

Stock = 0;

}

Stock + = delta;

To make the results more intuitive, the last two steps are to remove the auxiliary fields and sort the installation date and product. You can right-click each step and view the output step to confirm the output fields of each step.

 

Simpler Method: No-step solution

Using the "group by" step is more advantageous than using the "AnalyticQuery" step and javascript step in this scenario.

No-step solutionCode download.

 

 


In the kettle graphic interface, data extraction and conversion can be directly implemented. Why do we need to save it as a ktr file first and then call it using a java program?

Can be run, non-graphical operation can reduce the resources consumed by the graphic interface, and there are many running methods, such as sample interface running, command line calling running, Program Calling running, remote machine running (cluster running).

How to import multiple sheet data of an excel worksheet into a table in kettle?

When adding a file, you can select sheet. Pay attention to the settings in the excel input file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.