eightban's memo

残しておきたい記事をまとめてみました。このブログに書いてあるドキュメントやブログで配布しているファイルの使用によって発生するいかなる損害に対してもこのブログの管理者は責任を負いません。使用する場合は自己責任のもとに使用してください。

Talend

Talend Open Studioで自作関数を作成する ~ Talend

投稿日:

Talend Open Studioで使用したい文字列関数が無かったので自作関数を作成してみました。

自作関数の作成

リポジトリ->コード->ルーチンに「usercomponent」フォルダを作成して、その中にルーチンを作成します。

 

今回は一つのルーチンに関数を複数作るので「UserRoutines」の名前のルーチンを作成します。

 

サンプルとして「helloExample」の自作関数が作成されます。コメント行には使用方法を書かれています。

D:\oss\talend\TOS_DI-V6.4.1\workspace\LOCAL_PROJECT\code\routines\usercode\UserRoutines_0.1.item
に実体があります。

 

ルーチンライブラリの編集を選びます。

 

文字列操作の外部ライブラリを使うので「外部ライブラリインポート」を選びます。

 

アイテムのロックが表示されていたので、編集していたルーチンを閉じます。

 

Newを選びます。

 

 

文字列操作のライブラリ

com.ibm.icu_52.1.0.v201404241930.jar を選びます。

 

OK

 

Finish

 

終わったら「ルーチンの編集」を選びます。

 

import com.ibm.icu.text.Transliteratorを挿入します。

標準関数のノーマライズの関数(英数字は半角、カナは全角など)を使いたいので、

import java.text.Normalizerとimport java.text.Normalizer.Formも入れておきます。

 

下記の関数を作ります。

  1. Normalize:英数字記号を半角、ひらがな、カナを全角に変換
  2. Han2ZenICU:半角を全角に変換 .
  3. MID:文字列切り取り

ソースを参考にしてください。

package routines;
import java.text.Normalizer;
import java.text.Normalizer.Form;
import com.ibm.icu.text.Transliterator;
/*
 * user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
 * it must be before the "{talendTypes}" key.
 * 
 * 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
 * long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
 * Short
 * 
 * 3. {Category} define a category for the Function. it is required. its value is user-defined .
 * 
 * 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
 * 
 * <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
 * Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
 * added. you can have many parameters for the Function.
 * 
 * 5. {example} gives a example for the Function. it is optional.
 */
public class UserRoutines {

    /**
     * helloExample: not return value, only print "hello" + message.
     * 
     * 
     * {talendTypes} String
     * 
     * {Category} User Defined
     * 
     * {param} string("world") input: The string need to be printed.
     * 
     * {example} helloExemple("world") # hello world !.
     */
    public static void helloExample(String message) {
        if (message == null) {
            message = "World"; //$NON-NLS-1$
        }
        System.out.println("Hello " + message + " !"); //$NON-NLS-1$ //$NON-NLS-2$
    }

    /**
     * Normalize:英数字記号を半角、ひらがな、カナを全角に変換 .
     *
     *
     * {talendTypes} String
     *
     * {Category} User Defined
     *
     * {param} string("world") input: The string need to be printed.
     *
     * {example} Normalize("world") # world.
     */

      public static String Normalize(String string) {
            return string == null ? null : Normalizer.normalize(string, Form.NFKC);
      }
        /**
         * Han2ZenICU:半角を全角に変換 .
         *
         *
         * {talendTypes} String
         *
         * {Category} User Defined
         *
         * {param} string("world") input: The string need to be printed.
         *
         * {example} Han2ZenICU("world") # world
         */
      public static String Han2ZenICU(String string) {
              Transliterator trH2F = Transliterator.getInstance("Halfwidth-Fullwidth");
              return string == null ? null : trH2F.transliterate(string);
      }
       /**
         *Returns the string starting at index position start.
         *
         * {talendTypes} String
         *
         * {Category} User Defined
         *
         * {param} string("hello world!") string: String.
         *
         * {param} int(0) index : index
         *
         * {param} int(5) index2 : index2
         *
         * {example} MID("hello world!",0,5) # hello
         */
        public static String MID(String string, int index, int index2) {
            if (index > string.length()) {
                return null;
            }
            return string == null ? null : string.substring(index, Math.min(string.length(), index+index2));
        }
    
}

使用法ですが

UserRoutines.Normalize("AabB12345アイウエオガあいうえお")
UserRoutines.Han2ZenICU("AabB12345アイウエオガあいうえお")
UserRoutines.MID(row1.NAME,3,2)

LEFT,RIGHT関数はStringHandlingルーチンにあります。

StringHandling.LEFT("helloWorld!",5)
StringHandling.RIGHT("helloWorld!",5)

javaではsubstring(開始位置、終了位置)
str ="STRING";
str.string(1,3); -> TR

 

使用例

関数を使わない場合

CSVファイルを入力にしtmapを介して変換をかけtlogrowに出力する例です。

ジョブの作成

 

新規JOBを作ります。

 

 

CSVのメタデータがあったので、それを使います。tfileinputdelimitedを選びます。

ない場合は用意してください。

 

先頭の文字をいれてtmapを出します。

 

 

tfileinputdelimitedで右クリックでメニューを出し、ロウ->メインで何もないところに落とします。

 

 

 

tlogと入れて、tLogRowを選びます。

 

出力名を聞いてくるのでoutputなどといれておきます。

 

処理がつながりました。

自動マップを押すと入力と同じ出力となります。

 

 

 

 

実行するとLogに出力されます。

関数を使う場合

tMapで関数を使います。式の右横の四角を押すと画面が開きます。

 

ユーザ定義->自作関数を選びダブルクリックすると関数が左上に入りますが編集して正しいものにします。

 

 

コードが全角、名前が切り取られました。

足りないものを自分で追加できるのも便利なところです。

-Talend

Copyright© eightban's memo , 2024 All Rights Reserved Powered by STINGER.