`
yugouai
  • 浏览: 491776 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

vertica自定义函数

阅读更多

Vertica实现mysql函数substring_index:

package com.yy.vertica;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import com.vertica.sdk.BlockReader;
import com.vertica.sdk.BlockWriter;
import com.vertica.sdk.ColumnTypes;
import com.vertica.sdk.DestroyInvocation;
import com.vertica.sdk.ScalarFunction;
import com.vertica.sdk.ScalarFunctionFactory;
import com.vertica.sdk.ServerInterface;
import com.vertica.sdk.SizedColumnTypes;
import com.vertica.sdk.UdfException;
import com.vertica.sdk.VerticaType;

/**
 * vertica udf : substring_index
 *
 */
public class UDFSubStringIndexFactory extends ScalarFunctionFactory
{

	@Override
	public ScalarFunction createScalarFunction(ServerInterface arg0) {
		return new SubStringIndex();
	}
	

	public class  SubStringIndex extends ScalarFunction{

		@Override
		public void processBlock(ServerInterface serverInterface, BlockReader argReader, BlockWriter argWriter) throws UdfException, DestroyInvocation {
			//see how many arguments were passed in
			int numCols = argReader.getNumCols();
			
			//check argument nums
			if (numCols != 3) {
				throw new UdfException(0, "Must supply 3 arguments:String input, String stripChars, int index");
			}
			
			//make sure input columns : String input, String stripChars, int index
			SizedColumnTypes inTypes = argReader.getTypeMetaData();
			VerticaType firstParamType = inTypes.getColumnType(0);
			VerticaType secondParamType = inTypes.getColumnType(1);
			VerticaType thirdParamType = inTypes.getColumnType(2);
			if (!firstParamType.isStringType() && !secondParamType.isStringType() && !thirdParamType.isInt()) {
				throw new UdfException(0, "make sure input columns is : String input, String stripChars, int index");
			}
			
			String paramString = argReader.getString(0);
			String stripChars =  argReader.getString(1);
			int index = NumberUtils.toInt(Long.toString(argReader.getLong(2)));
			argWriter.setString(evaluate(paramString, stripChars, index));
		}
		
		public String evaluate(String input, String stripChars, int index) {
			String[] al = StringUtils.split(input, stripChars);
			if (al == null || stripChars == null || index == 0) {
				return null;
			}
			int indexAbs = Math.abs(index)>=al.length ? al.length: Math.abs(index);
			String[] result = new String[indexAbs];
			List<String> tmp = Arrays.asList(al);
			if (index > 0) {
				System.arraycopy(tmp.toArray(), 0, result, 0, indexAbs);
				return StringUtils.join(result, stripChars);
			}
			// 反向取值
			Collections.reverse(tmp);
			System.arraycopy(tmp.toArray(), 0, result, 0, indexAbs);
			List<String> res = Arrays.asList(result);
			Collections.reverse(res);
			return StringUtils.join(res, stripChars);
		}
		
	}
	
	@Override
	public void getPrototype(ServerInterface serverInterface, ColumnTypes argTypes, ColumnTypes returnType) {
		// Accepts any number and type or arguments. The ScalarFunction
        // class handles parsing the arguments.
		argTypes.addVarchar();
		argTypes.addVarchar();
		argTypes.addInt();
		returnType.addVarchar();
	}
	
	@Override
	public void getReturnType(ServerInterface srvInterface, final SizedColumnTypes argTypes, SizedColumnTypes returnType){
		VerticaType type = argTypes.getColumnType(0);
		returnType.addVarchar(type.getStringLength());
	}
}

 结果输出

Vmart=> select substring_index('a/b/c','/',2);
-[ RECORD 1 ]---+----
substring_index | a/b

Vmart=> select substring_index('a/b/c','/',3);
-[ RECORD 1 ]---+------
substring_index | a/b/c

Vmart=> select substring_index('a/b/c','/',1);
-[ RECORD 1 ]---+--
substring_index | a

 

注意点:

abstract void com.vertica.sdk.UDXFactory.getReturnType ( ServerInterface srvInterface, SizedColumnTypes argTypes,

SizedColumnTypes returnType ) throws UdfException [pure virtual]

Function to tell Vertica what the return types (and length/precision if necessary) of this UDX are.

For CHAR/VARCHAR types, specify the max length,

For NUMERIC types, specify the precision and scale.

For Time types (with or without time zone), specify the precision, -1 means unspecified/don’t care

For IntervalYM/IntervalDS types, specify the precision and range

For all other types, no length/precision specification needed

 

字符串返回值需要指定返回长度。

 

创建自定义函数分两个步骤:

1、创建lib

2、创建function

Vmart=> SELECT SET_CONFIG_PARAMETER('JavaBinaryForUDx','/usr/bin/java');
Vmart=> create LIBRARY verticaextlib as '/home/dbadmin/verticaext.jar' language 'Java';
CREATE LIBRARY
Vmart=> create function substring_index as language 'Java' name 'com.yy.vertica.UDFSubStringIndexFactory' librARY verticaextlib;
CREATE FUNCTION

 

分享到:
评论

相关推荐

    vertica数据库的连接jar包

    Java连接数据vertica数据库 就可以连接数据库 import java.sql.*; import java.util.Properties; Properties myProp = new Properties(); //用于设置数据库的用户名 myProp.put("user", "dbadmin"); //用于...

    vertica jdbc 驱动driver

    vertica jdbc驱动 不用解释了吧

    vertica-jdbc-9.2.1-0.jar

    You can download the Vertica drivers here: https://my.vertica.com/download/vertica/client-drivers/ Once you have the files you want (i.e. vertica-jdbc-9.2.1-0.jar) you should be able to run a ...

    Vertica 分析型数据库完全参考文档

    Vertica Documentation - Vertica Analytic Database Contents Vertica® 9.1.x Documentation 5 Vertica 9.1.x Supported Platforms 11 Vertica 9.1.x New Features and Changes 39 Vertica Concepts 64 ...

    Vertica 常用操作说明 v1

    Vertica除了用GUI方式创建数据库外,也可以用如下命令创建数据库: v001:/home/dbadmin $ admintools -t create_db -s v001,v002,v003,v005,v006,v007,v008 -d $DBNAME -p $DBPWD 如下修改vsql所在服务器的 ~/.bash...

    Vertica入门文档

    Vertica

    vertica windows客户端

    我用过性能最好的mpp数据库vertica的客户端 vertica 美国hp收购的一个mpp数据库

    vertica文档

    vertica 文档

    Vertica 7 JAVA SDK

    Vertica 7 JAVA SDK文档

    vertica基础培训.pptx

    vertica培训的ppt ,里面内容包含vertica的介绍,vertica的使用,以及vertica的扩展。可以作为培训的模板ppt

    vertica备份恢复方案

    vertica备份恢复方案

    vertica jdbc driver 驱动下载

    vertica jdbc driver包含三个版本的驱动,分别是5.6,7.0,8.0

    HP_Vertica数据库从入门到精通

    惠普的数据库Vertica还是很不错,这是少有的教程,网上Vertica的资料比较少,只能看这个他们的内部文档了

    Introduction Vertica Architecture_Overview

    Introduction vertica Architecture_Overview

    HP Vertica Essentials

    Vertica是HP收购的一个列式数据库,在数据分析、挖掘方面很有潜力,特别是对列式数据分析比传统的关系型数据库更有优势。本书是对Vertica的概念、管理等等有比较详细的讲解。(英文版)

    vertica驱动

    vertica 数据库的驱动,如果连接数据库,需要在连接数据库工具中加入这个驱动,才能正常启动

    Vertica的那些事.pdf

    Vertica的那些事博客整理,对于初学者掌握MPP数据库很有帮助。对于想要学习Vertica是很有用的,笔者曾从Vertica小白一步步走来,现在分享学习经验给大家,想要进一步交流可以在博客联系我。

    vertica-jdbc-9.3.1-0.jar

    vertica数据库的连接jar包下载

    Vertica_9.1.x完整文档

    Vertica_9.1.x_Complete_Documentation Vertica_9.1.x的完整文档,详细介绍Vertica及其功能,包括安装、管理手册、数据分析、使用控制台、SQL参考手册

    vertica安装包RPM第一部分

    vertica安装包RPM第一部分,共计两部分,大小255M,版本9.1.1

Global site tag (gtag.js) - Google Analytics