Use javascript steps and FIRETODB functions in kettle to define your own database queries

Source: Internet
Author: User
Tags error handling mysql in

Use javascript steps and FIRETODB functions in kettle to define your own database queries

If you need to implement non-traditional database query operations. To discuss this scenario, if you need to read the regular table in the database, then check the number of field match expressions for each row entered.

Run a database query in a JavaScript step

When the JavaScript step is initialized, query the database. Gets the regular table-set of records. The input data loop of each row is then checked for matching expressions, assuming matching. Record variable plus one, and finally write the result into the new field on the move.

The idea is very clear, but how to implement database query in JavaScript steps?

Here is a very useful function firedb (), he needs to define the good one database connection in the conversion in advance as a parameter. and a SQL query statement, where SQL can only be a SELECT query statement, because the Firedb () function has no transaction or error handling. The return value is an array of record rows, each of which is an array with multiple fields.

Here is the code snippet. This code runs only once, preferably in initialization tab, which improves the efficiency of the JavaScript steps.

Read all rules from Scrapbook DB

var Xarr =firetodb ("Scrapbook", "select Rule from rules;");

var regex = [];

For each row, save the returned rulefor later use

for (var i in Xarr) {

Regex.push (Xarr[i][0]);

}

The following code is for each line to run;

var rules_matched = 0;

var jstr = newjava.lang.String (string_to_test);

Count matching rules

for (var i in regex) {

Varrule = Regex[i];

if (jstr.matches (rule)) {

rules_matched++;

}

}

To download the Demo sample Here, try the use of the Firedb () function, and demonstrate the SQL statement that demonstrates the sample data for MySQL in the sample example. In the code, "Scrapbook" is the name of the connection to the MySQL database as defined in the conversion.

Use javascript steps and FIRETODB functions in kettle to define your own database queries

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.