Skip to main content

creating UDFs

  • April 16, 2014
  • 14 replies
  • 27 views

Roman_R
Forum|alt.badge.img
Hello,

May be anyone can help me.

I am trying to create Java Scalar Function and use it in Vertica. For he first time I am trying to create function that described in the documentation. I did all steps exact like it described there. But when I invoke "create function" command in vsql I receive next error:

ROLLBACK 3399:  Failure in UDx RPC call InvokeSetExecContext(): Error in User Defined Object [add2ints], error code: 0

com.vertica.sdk.UdfException: Couldn't instantiate class home/dbadmin/workspace/tasks/src/com/mycompany/example/Add2intsFactory

Possibly I miss some settings in Vertica?

Thank you for any help.

14 replies

PRana
Forum|alt.badge.img
  • Participating Frequently
  • April 16, 2014
Hi Roman,

This error pops up when vertica is unable to find the class specified in the create function statement in the library jar. Can you please check if the name of the class in the create function statement is the same as that in the jar. Please note that the case must match as well. If the problem still persists, please post a reproducer.

Thanks
Pratibha

Roman_R
Forum|alt.badge.img
  • Author
  • Participating Frequently
  • April 16, 2014
Failure in UDx RPC call InvokeSetExecContext(): Error in User Defined Object [tokenize], error code: 0
com.vertica.sdk.UdfException: Couldn't instantiate class com.mycompany.example.TokenFactory
        at com.vertica.udxfence.UDxExecContext.findUDXFactoryInLibrary(UDxExecContext.java:356)
        at com.vertica.udxfence.UDxExecContext.setupLanguageContext(UDxExecContext.java:391)
        at com.vertica.udxfence.UDxExecContext.setupContext(UDxExecContext.java:437)
        at com.vertica.udxfence.UDxExecContext.run(UDxExecContext.java:197)
        at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.ClassNotFoundException: com.mycompany.example.TokenFactory
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:270)
        at com.vertica.udxfence.UDxExecContext.findUDXFactoryInLibrary(UDxExecContext.java:341)
        ... 4 more

PRana
Forum|alt.badge.img
  • Participating Frequently
  • April 16, 2014
Hi Roman,

Can you please provide the statements that you are using to create the library and the function. can you also attach you java code.

Thanks
Pratibha

Roman_R
Forum|alt.badge.img
  • Author
  • Participating Frequently
  • April 16, 2014
java code:

package com.mycompany.example;
import com.vertica.sdk.*;

public class TokenFactory extends TransformFunctionFactory{
    @Override
    public void getPrototype(ServerInterface srvInterface, ColumnTypes argTypes,ColumnTypes returnType){
        argTypes.addVarchar();
        returnType.addVarchar();
    }
    @Override
    public void getReturnType(ServerInterface srvInterface, SizedColumnTypes inputTypes,SizedColumnTypes outputTypes){
        outputTypes.addVarchar(inputTypes.getColumnType(0).getStringLength(),"Tokens");
        }
    public class TokenizeString extends TransformFunction {
        @Override
        public void processPartition(ServerInterface srvInterface,PartitionReader inputReader,PartitionWriter outputWriter) throws UdfException,DestroyInvocation {
            do {
                if(inputReader.isStringNull(0)) {
                    outputWriter.setStringNull(0);
                }
                else {
                    String[] tokens = inputReader.getString(0).split("\\s+");
                    for(int i=0;i<tokens.length;i++) {
                        outputWriter.getStringWriter(0).copy(tokens);
                        outputWriter.next();                    }
                }
            }
            while(inputReader.next());
        }
    }
    @Override
    public TransformFunction createTransformFunction(ServerInterface srvInterface) {
        return new TokenizeString();
    }
}

create library sts:
dbadmin=>create library tokenizelib as'/home/dbadmin/TokinizeStringLib.jar'
dbadmin-> language 'Java';

create transform function sts:
dbadmin=> create transform function tokenize as language 'Java' name
dbadmin-> 'com.mycompany.example.TokenFactory' library tokenizelib;

Thanks

PRana
Forum|alt.badge.img
  • Participating Frequently
  • April 17, 2014
Hi Roman,

This works for me.
prana=> create library tokeniz1elib as '/home/prana/Downloads/build/tokenize.jar' language 'java';
CREATE LIBRARY
prana=> create transform function tokenize1 as language 'Java' name 'com.mycompany.example.TokenFactory' library tokenizelib;
CREATE TRANSFORM FUNCTION





Roman_R
Forum|alt.badge.img
  • Author
  • Participating Frequently
  • April 17, 2014
Could you please write the commands that you use to compile Java classes and create jar files?

PRana
Forum|alt.badge.img
  • Participating Frequently
  • April 17, 2014
Sure ! Here they are.

localhost:tmp $ javac-cp /opt/vertica/bin/VerticaSDK.jar /opt/vertica/sdk/com/vertica/sdk/BuildInfo.java -d build/javaLibs

localhost:tmp $ javac -cp /opt/vertica/bin/VerticaSDK.jar TokenFactory.java -d build/javaLibs

localhost:tmp $ cd build/javaLibs

localhost:tmp $ jar cf tokenLib.jar .

prana=> create library tokenlib as '/home/prana/tmp/build/javaLibs/tokenLib.jar' language 'java';

prana=> create transform function tokenize as language 'Java' name 'com.mycompany.example.TokenFactory' library tokenlib;












Roman_R
Forum|alt.badge.img
  • Author
  • Participating Frequently
  • April 17, 2014
Thank you. We will try this but I believe that we have done the same steps and it not work.

Roman_R
Forum|alt.badge.img
  • Author
  • Participating Frequently
  • April 17, 2014
Its not working. I receive the same error. Possibly some settings should changed?

PRana
Forum|alt.badge.img
  • Participating Frequently
  • April 17, 2014
Hi Roman,

Can you please open a support case.

Thanks
pratibha



Roman_R
Forum|alt.badge.img
  • Author
  • Participating Frequently
  • April 17, 2014
Hi Pratibha,

We now use eclipse to create jar file and select one more option:
Export generated class files and resources

Without this option it not work.

I don't know what similar option in jar command, but this is the only way when function is normally created.

Abhishek_Chaudh
Hi Roman,

I have created a Java UDF. I have created Jar file for entire project using Eclipse. Jar file already has BuildInfo.class file, but still vertica isn't able to find it in JAR. Any clue?

2015-02-12 12:01:58.958 Init Session:0x7ff7f8013570-a0000000001fa9 <ROLLBACK> @v_vertica_icici_node0001: VP001/3399: Failure in UDx RPC call InvokeSetExecContext(): Error in User Defined Object [], error code: 0Error happened in loading jar file, Vertica build info class not found

Below is the snapshot of my jar file contents. 

image

Abhishek_Chaudh
This is now solved :) 
I was keeping BuildInfo.class in incorrect package. It has to be in com.vertica.sdk.


Forum|alt.badge.img+2
  • Participating Frequently
  • March 23, 2015

Make sure you copy your jar file to /opt/vertica/bin.