kettle转换中使用javascript例子整理(1)

这些例子在(\data-integration\samples\transformations目录下)

1 JavaScript - Access database connection metadata.ktr(访问数据库连接)







var db = _step_.getTransMeta().findDatabase("db_mysql_etltest_repository_4.2");//db_mysql_etltest_repository_4.2,是自己起的名字,是在数据库连接的时候连接名

var url = db.getURL();

var hostname = db.getHostname();

文本文件中的输出:

url:jdbc:mysql://localhost:3306/etltest_repository4_2?defaultFetchSize=500&useCursorFetch=true 

hostname:localhost 


2 JavaScript - Access environment variables.ktr(访问环境变量,参数与变量相同)



var v1 = getVariable("VAR1", "啊啊啊啊");//第二个参数表示为空时默认取值

var v2 = getVariable("java.io.tmpdir", "");

 

文本文件输出

v1@v2

呵呵@C:\Users\ADMINI~1\AppData\Local\Temp\

3 JavaScript - Add 2 and a half seconds to a Date.ktr(时间相加)





var thenInt = now.getInteger()+2500;//ms

var then = newjava.util.Date( thenInt );

文本文件输出:

now@thenInt@then

2014/09/23 14:09:40.653@ 1411452583153@2014/09/23 14:09:43.153


4 JavaScript - Base64 Decoder and Encoder.ktr(64位解码、编码)





var bytes = Packages.org.apache.commons.codec.binary.Base64.decodeBase64( F1.getString().getBytes() );

var decString =new Packages.java.lang.String( bytes );//64位解码器

//编码器

var encString =new Packages.java.lang.String( Packages.org.apache.commons.codec.binary.Base64.encodeBase64( decString.getBytes() ) );

文本文件输出:

F1@decString@bytes@encString

VGV4dCBmaWxlIGlucHV0  @Text file input  @"[B@43e76d"@VGV4dCBmaWxlIGlucHV0 

5 (重)JavaScript -create new rows.ktr(创建新的行)






if (groupsField!=null)

{

  var groups = groupsField.split(",");

  for (i=0;i<groups.length;i++)

  {

    newRow = createRowCopy(getOutputRowMeta().size());//新的一行,输出的列数

    var rowIndex = getInputRowMeta().size();//输入的列数

    newRow[rowIndex++] = trim( groups[i] );//对应列的值

    newRow[rowIndex++] = "N";

    putRow(newRow);//输出该行

  }

}

var subgroup = "";//对应上面的列名

var ignore = "Y";//对应上面的列名


文本文件输出:

member@groupsField@subgroup@ignore

Member 2 @Group1, Group2                @Group1@N

Member 2 @Group1, Group2                @Group2@N

Member 1 @Group1, Group2, Group3, Group4  @Group1@N

Member 1 @Group1, Group2, Group3, Group4  @Group2@N

Member 1 @Group1, Group2, Group3, Group4  @Group3@N

Member 1 @Group1, Group2, Group3, Group4  @Group4@N

 

6 JavaScript - date to string conversion.ktr(日期->字符串)





var datString =date2str(datOriginal, "yyyy**MM**dd hh&&mm&&ss#SSS");

// other date formats can be found in the manual searching for "date formats"

// remeber to change to length below of datString if you change the format

文本文件输出:

datOriginal@datString

2007/01/27 00:00:00.000@2007**01**27 12&&00&&00#000 




阅读更多
个人分类: kettle
下一篇kettle创建数据库资源库,执行SQL语句有两条执行失败
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭