Java实现Mysql数据库中 substring_index函数
前言: 由于hive中没有这个 substring_index函数,所以就自定义一个udf函数来调用使用。(不通过hive使用时可以直接使用下方的代码,如果需要被hive使用 记得继承 UDF类)
主要方法: indexOf(str, position); // str 查找的字符串,position 匹配字符串的开始位置
MysqlSubStringIndex.java
package com.mysql.util; /** * <p>Description:实现mysql的 substring_index(str,str,int) 函数功能 </p> * @author duanfeixia * @date 2019年7月3日 */ public class MysqlSubStringIndex { /** * * @param targetStr * 目标字符串 * @param str * 查找字符串 * @param index * 第n次出现 * @param order * 顺序(大于0表示正序,小于0表示反序) * @return */ public static String evaluate(String targetStr, String str, int index, int order) { /** * 当 str 不存在于 targetStr 时,不管是正序还是反序都返回原字符串 * 当index大于 str 在 targetStr 中出现的次数,不管是正序还是反序都返回原字符串 */ String result = targetStr;//默认返回字符串为原字符串 if (targetStr == null || targetStr.trim().length() == 0) { return result; } //当index=0时,返回空 if(index == 0){ return ""; } //判断是正序还是反序(大于等于0表示正序,小于0表示反序) if(order < 0){ targetStr = new StringBuffer(targetStr).reverse().toString(); } int beginIndex = 0;//用于匹配字符串的起始位置 int count = 0; //记录字符串出现的次数 while ((beginIndex = targetStr.indexOf(str, beginIndex)) != -1) { count++; //当index与字符串出现次数相同时,开始返回结果 if (count == index) { if (order < 0) {//反序时 targetStr = new StringBuffer(targetStr).reverse().toString(); result = targetStr.substring(targetStr.length() - beginIndex); }else{//正序时 result = targetStr.substring(0, beginIndex); } return result; } beginIndex = beginIndex + str.length();//更改匹配字符串的起始位置 } return result; } //测试 public static void main(String[] args) { String result11 = evaluate("ehello.wolrd.1ee", "e", 1,1); String result21 = evaluate("ehello.wolrd.1ee", "e", 2,1); String result31 = evaluate("ehello.wolrd.1ee", "e", 3,1); String result41 = evaluate("ehello.wolrd.1ee", "e", 4,1); String result51 = evaluate("ehello.wolrd.1ee", "e", 5,1); String result1 = evaluate("ehello.wolrd.1ee", "e", 1,-1); String result2 = evaluate("ehello.wolrd.1ee", "e", 2,-1); String result3 = evaluate("ehello.wolrd.1ee", "e", 3,-1); String result4 = evaluate("ehello.wolrd.1ee", "e", 4,-1); String result5 = evaluate("ehello.wolrd.1ee", "e", 5,-1); System.out.println("正序-result11= "+result11); System.out.println("正序-result21= "+result21); System.out.println("正序-result31= "+result31); System.out.println("正序-result41= "+result41); System.out.println("正序-result51= "+result51); System.out.println("---------------------------"); System.out.println("反序-result1= "+result1); System.out.println("反序-result2= "+result2); System.out.println("反序-result3= "+result3); System.out.println("反序-result4= "+result4); System.out.println("反序-result5= "+result5); System.out.println("---------------------------"); String result = evaluate("ehello.wolrd.1ee", "e", 0,-1); System.out.println("index=0时-result= "+result); } }
测试结果:
Hive中UDF函数定义,继承了UDF类
package com.mysql.util.hiveudf; import org.apache.hadoop.hive.ql.exec.UDF; /** * <p>Description:实现mysql的 substring_index(str,str,int) 函数功能 </p> * @author duanfeixia * @date 2019年7月3日 */ public class MysqlSubStringIndex extends UDF{ /** * * @param targetStr * 目标字符串 * @param str * 查找字符串 * @param index * 第n次出现 * @param order * 顺序(大于0表示正序,小于0表示反序) * @return */ public static String evaluate(String targetStr, String str, int index, int order) { /** * 当 str 不存在于 targetStr 时,不管是正序还是反序都返回原字符串 * 当index大于 str 在 targetStr 中出现的次数,不管是正序还是反序都返回原字符串 */ String result = targetStr;//默认返回字符串为原字符串 if (targetStr == null || targetStr.trim().length() == 0) { return result; } //当index=0时,返回空 if(index == 0){ return ""; } //判断是正序还是反序(大于等于0表示正序,小于0表示反序) if(order < 0){ targetStr = new StringBuffer(targetStr).reverse().toString(); } int beginIndex = 0;//用于匹配字符串的起始位置 int count = 0; //记录字符串出现的次数 while ((beginIndex = targetStr.indexOf(str, beginIndex)) != -1) { count++; //当index与字符串出现次数相同时,开始返回结果 if (count == index) { if (order < 0) {//反序时 targetStr = new StringBuffer(targetStr).reverse().toString(); result = targetStr.substring(targetStr.length() - beginIndex); }else{//正序时 result = targetStr.substring(0, beginIndex); } return result; } beginIndex = beginIndex + str.length();//更改匹配字符串的起始位置 } return result; } public static void main(String[] args) { String result11 = evaluate("ehello.wolrd.1ee", "e", 1,1); String result21 = evaluate("ehello.wolrd.1ee", "e", 2,1); String result31 = evaluate("ehello.wolrd.1ee", "e", 3,1); String result41 = evaluate("ehello.wolrd.1ee", "e", 4,1); String result51 = evaluate("ehello.wolrd.1ee", "e", 5,1); String result1 = evaluate("ehello.wolrd.1ee", "e", 1,-1); String result2 = evaluate("ehello.wolrd.1ee", "e", 2,-1); String result3 = evaluate("ehello.wolrd.1ee", "e", 3,-1); String result4 = evaluate("ehello.wolrd.1ee", "e", 4,-1); String result5 = evaluate("ehello.wolrd.1ee", "e", 5,-1); System.out.println("正序-result11= "+result11); System.out.println("正序-result21= "+result21); System.out.println("正序-result31= "+result31); System.out.println("正序-result41= "+result41); System.out.println("正序-result51= "+result51); System.out.println("---------------------------"); System.out.println("反序-result1= "+result1); System.out.println("反序-result2= "+result2); System.out.println("反序-result3= "+result3); System.out.println("反序-result4= "+result4); System.out.println("反序-result5= "+result5); System.out.println("---------------------------"); String result = evaluate("ehello.wolrd.1ee", "e", 0,-1); System.out.println("index=0时-result= "+result); } }