Kettle の結果の出力行にフィールド (または新しい列) を動的に追加したいと思います。
フロムの投稿を読むのに何時間も費やした後、彼はスクリプティングのドキュメントがあまりうまくできていなかったので、Stackoverflow が役立つかどうか疑問に思いました。
以下の手順を使用して、動的列生成を生成できます。
入力値はどのようにSQLクエリに渡されますか?それらが変数の場合は、テーブル入力ステップを「変数の取得」ステップに渡して、その方法で新しい列を取得します。
または、計算機を使用して列を追加するか、定数を追加することもできます。
または、「システム情報の取得」ステップを使用して、コマンドラインの引数や日付などを取得することもできます。
まず、ユーザー定義の Java クラスのステップで使用したコードの一部を示します。
private int fieldToHashGeoIndex;
private int fieldToHashHeadIndex;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
Object[] r=getRow();
if (r==null)
{
setOutputDone();
return false;
}
if (first) {
fieldToHashGeoIndex = getInputRowMeta().indexOfValue(getParameter("FIELD_TO_HASH_GEO"));
if (fieldToHashGeoIndex<0) {
throw new KettleException("Field to hash not found in the input row, check parameter 'FIELD_TO_HASH_GEO'!");
}
fieldToHashHeadIndex = getInputRowMeta().indexOfValue(getParameter("FIELD_TO_HASH_HEAD"));
if (fieldToHashHeadIndex<0) {
throw new KettleException("Field to hash not found in the input row, check parameter 'FIELD_TO_HASH_HEAD'!");
}
first=false;
}
Object[] outputRowData = RowDataUtil.resizeArray(r, data.outputRowMeta.size());
int outputIndex = getInputRowMeta().size();
String fieldToHashGeo = getInputRowMeta().getString(r, fieldToHashGeoIndex);
String fieldToHashHead = getInputRowMeta().getString(r, fieldToHashHeadIndex);
outputRowData[outputIndex++] = MurmurHash.hash64(fieldToHashGeo);
outputRowData[outputIndex++] = MurmurHash.hash64(fieldToHashHead);
putRow(data.outputRowMeta, outputRowData);
return true;
}
さて、通常outputRowMeta
はステップの構成から構成しますが、コードで変更できる場合があります。これにより、コードで追加のフィールドを指定できるようになります。
別の方法として、「field1」、「field2」などの固定出力フィールドをステップに定義し、他の場所でフィールドの名前を追跡することにより、可変フィールドをラッチすることもできます。おそらく、String 型のすべてのフィールドを作成し、後で独自の型調整を行う必要があります。
ただし、可変出力フィールドは問題を引き起こす可能性があります。タイプの不一致やフィールドの欠落によるエラーを避けるために、後のステップで何をするかについて非常に注意する必要があります。