• kettle通过java步骤获取汉字首拼


    kettle通过java步骤获取汉字首拼

    用途描述

    一组数据,需要获取汉字首拼后,输出;

    实现效果

    在这里插入图片描述

    添加jar包

    pinyin4j-2.5.0.jar

    自定义常量数据

    在这里插入图片描述
    在这里插入图片描述

    Java代码

    在这里插入图片描述

    完整代码:

    import net.sourceforge.pinyin4j.PinyinHelper;
    import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
    import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
    import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
    import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
    
    String nameField;
    String pyField;
    
    public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
    {
    	
    	// Let's look up parameters only once for performance reason.	
    	// 	
    	if (first) {	
    		nameField = "name";//getParameter("name");	
    		pyField = "py" ; // getParameter("py");	
    		first=false;	
    	}
    	
    	// First, get a row from the default input hop
    	//
    	Object[] r = getRow();
    	
    	// If the row object is null, we are done processing.
    	//
    	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.
    	//
    	Object[] outputRow = createOutputRow(r, data.outputRowMeta.size());
    	String name = get(Fields.In, nameField).getString(r);	
    	
    	// Set the value in the output field	
    	//
    	String py = toFirstChar(name);	
    	get(Fields.Out, pyField).setValue(outputRow, py);	
    	
    	// putRow will send the row on to the default output hop.	
    	//
    	putRow(data.outputRowMeta, outputRow);
    	
    	return true;
    }
    
    
    /**
     * 获取字符串拼音的第一个字母
     */
    public static String toFirstChar(String chinese){
        // 特殊字符处理
        if (null == chinese || chinese.isEmpty()) return "";
        chinese=chinese.replace(" ","");
    
        StringBuilder pinyinStr = new StringBuilder();
        char[] newChar = chinese.toCharArray();  //转为单个字符
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    String [] arr = PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat);
                    if(null == arr || arr.length<1) continue;
                    pinyinStr.append(arr[0].charAt(0));
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr.append(newChar[i]);
            }
        }
        return pinyinStr.toString();
    }
    /**
     * 汉字转为拼音
     */
    public static String toPinyin(String chinese){
    	String pinyinStr = "";
    	char[] newChar = chinese.toCharArray();
    	HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
    	defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    	defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    	for (int i = 0; i < newChar.length; i++) {
    		if (newChar[i] > 128) {
    			try {
    				pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
    			} catch (BadHanyuPinyinOutputFormatCombination e) {
    				e.printStackTrace();
    			}
    		}else{
    			pinyinStr += newChar[i];
    		}
    	}
    	return pinyinStr;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100

    –END–

  • 相关阅读:
    还在直接用localStorage么?全网最细:本地存储二次封装(含加密、解密、过期处理)
    半导体二极管
    Vue开发的请求
    腾讯mini项目-【指标监控服务重构】2023-08-11
    【Electron】开发实战
    re --- 正则表达式操作
    Linux之租云服务器及配docker环境
    面渣逆袭:HashMap追魂二十三问
    用Java语言简单做几个数组相关的练习题吧
    FallO’ween 活动:元宇宙中秋天与万圣节的邂逅
  • 原文地址:https://blog.csdn.net/huryer/article/details/132725315