How Do I Use Java In Greenplum?

This is the sixth in a series of “How do I do X in Greenplum database” and today’s topic is How do I use Java in Greenplum.

Greenplum supports many languages and one of these is Java. This is defined as a language called “pljava”. Because it is Java, it still requires you to compile the code and put it in a jar that is your classpath. There are a couple of additional steps to configure this in Greenplum so I will go through these steps.

Prerequisites:
1. JDK installed either on the master or a desktop. If on a desktop, you’ll need to copy the jar file to the master so you can then copy it to the segments.
2. Logged into the master (mdw) as user gpadmin.
3. Methods must be public and static.

First step, write some Java and here is an example of a file named Example.java:

public class Example
{
        public static String substring(String text, int beginIndex, int endIndex)
        {
                return text.substring(beginIndex, endIndex);
        }
}

Create a manifest file named manifest.txt:

Manifest-Version: 1.0
Main-Class: Example
Specification-Title: "Example"
Specification-Version: "1.0"
Created-By: 1.6.0_35-b10-428-11M3811
Build-Date: 09/28/2012 10:09 AM

Compile the Java:

javac *.java

Jar the file:

jar cfm analytics.jar manifest.txt *.class

Copy the analytics.jar to all Greenplum servers. The gphosts_file contains a list of all of your severs. An example of that is:

mdw
smdw
sdw1
sdw2
sdw3
sdw4

And the command to copy the Jar file:

gpscp -f gphosts_file analytics.jar =:/usr/local/greenplum-db/lib/postgresql/java/

Set your classpath variable inside Greenplum:

gpconfig -c pljava_classpath -v \'analytics.jar\'

Apply the configuration change:

gpstop -u

Install the pljava language and in this example, I’m putting it into the gpdb database. Note: this is a one time activity per database.

psql gpdb -f $GPHOME/share/postgresql/pljava/install.sql

Make sure the classpath is set correctly:

show pljava_classpath

It should say ‘analytics.jar’.

And finally, a working example:

create table temp (a varchar) distributed randomly;
insert into temp values ('a string');

--Example function
create or replace function java_substring(varchar, int, int)
returns varchar as 'Example.substring' language java;

--Example execution
select java_substring(a, 1, 3) from temp;

This was a pretty simple example of using PL/Java. Enjoy!

4 thoughts on “How Do I Use Java In Greenplum?

  1. Fabiano Fernandes

    Hi, I tried your example, but I’m getting a error. Any ideia I can solve this?

    ERROR: java.lang.ClassNotFoundException: substring(JNICalls.c:70)

    ********** Error **********

    ERROR: java.lang.ClassNotFoundException: substring(JNICalls.c:70)
    SQL state: XX000

    Reply
    1. Jon Post author

      It looks like the class file is not found. You could have a type-o in the name or the configuration with gpconfig. You might also have forgotten a step like gpstop -u.

      Good luck!

      Reply
  2. Marshall Presser

    For completeness sake, did you want to show the contents of the install.sql file,
    Is it more than a create language statement?

    Reply
    1. Jon Post author

      Here is the install.sql file:
      CREATE FUNCTION pljava_call_handler() RETURNS language_handler AS ‘pljava’ LANGUAGE C;
      CREATE FUNCTION pljavau_call_handler() RETURNS language_handler AS ‘pljava’ LANGUAGE C;
      CREATE TRUSTED LANGUAGE java HANDLER pljava_call_handler;
      CREATE LANGUAGE javaU HANDLER pljavau_call_handler;

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.