kettle實現用戶名動態脫敏和時間模糊

  • 用戶名脫敏
    在給用戶名進行脫敏時,通常情況下我們使用正則表達式或者其他方法,但是正則表達式在kettle中不容易實現動態脫敏(即將一個用戶名除去首字符和尾字符都替換爲***)
    舉個例子:
CSDN 脫敏後爲 C**N   //4-2 = 2 個 *
abcdefg 脫敏後爲 a*****g    //7-2 =5 個 *
即在中間輸出:(字符串的長度-2)個   *
  • 時間模糊
    將時間字段模糊至年月日,如將2013-9-30 07:41:00模糊爲2013-9-30

步驟:
1. 數據庫中各個字段以及內容
在這裏我們可以看到爬取時間後面有時分秒,用戶名也是完整顯示
在這裏插入圖片描述
2. 添加JAVA代碼進行用戶名動態脫敏

代碼位於Processor部分,紅框中的代碼是動態處理的部分,在這裏插入圖片描述

import java.util.*;
import java.util.Collections;

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
	if (first){
	  first = false;

	}

	Object[] r = getRow();

	if (r == null) {
	  setOutputDone();
	  return false;
	}

	// It is always safest to call createOutputRow() to ensure that your output row's Object[] is large
    // enough to handle any new fields you are creating in this step.
    r = createOutputRow(r, data.outputRowMeta.size());
	
    /* TODO: Your code here. (See Sample)
    
    // Get the value from an input field
    String foobar = get(Fields.In, "a_fieldname").getString(r);

    foobar += "bar";
    
    // Set a value in a new output field
    get(Fields.Out, "output_fieldname").setValue(r, foobar);
	
	*/
	String id = get(Fields.In, "評論者ID").getString(r);//獲取輸入
	int len = id.length();
	
	if (len == 1) {
		id = "*";
		}
		else if (len == 2) {
			id = id.charAt(0)+"*";
		}
		else {
			String replace = String.join("", Collections.nCopies(len-2, "*"));
		    id = id.charAt(0)+replace+id.charAt(len-1);
		}
	
	get(Fields.Out, "評論者ID_mask").setValue(r, id);
	// Send the row on to the next step.
    putRow(data.outputRowMeta, r);//輸出

	return true;
}

3. 使用字段選擇實現時間模糊
在這裏插入圖片描述
4.添加excel輸出,輸出到xls文件
在這裏插入圖片描述
5.連接控件
在這裏插入圖片描述
6.啓動並輸出結果
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章