GHK
GHK

Reputation: 251

Executing Hive Query from Java

I tried to execute a small hive query from Java, but it is failing with below error, bur when I copy the same query and run on terminal it is giving me the result.

Can someone help me on this.

Java Code:

Runtime.getRuntime().exec("hive -e 'show databases;'");

Error thrown:

FAILED: ParseException line 1:5 cannot recognize input near '<EOF>' '<EOF>' '<EOF>' in ddl statement

Regards, GHK.

Upvotes: 3

Views: 8253

Answers (1)

Davis Broda
Davis Broda

Reputation: 4135

I have been working with this Java problem for a while, and I believe I have solved this problem. Basically the reason you are failing is because the environment variables are not ser up properly. put the following in your /home/<username>/.bash_profile file and restart your machine to fix this.

HIVE_HOME=/usr/lib/hive

export HIVE_HOME

PATH=$PATH:$HIVE_HOME/bin/hive

export PATH

This will ensure that they get set up properly.

However while this will get rid of the error it still won't show you a list of databases because the process that runs the hive command will run in the background, not on the console the main program is running from. The following code will let you redirect the outputs of the program to the console that the main program is running from.

package testing.console;

import java.io.IOException;
import java.lang.ProcessBuilder;
import java.util.Map;

import testing.console.OutputRedirector;

//This Works

public class ConsoleTester {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        ProcessBuilder hiveProcessBuilder = new ProcessBuilder("hive", "-e",
                "show databases");
        String path = processEnv.get("PATH");
        Process hiveProcess = hiveProcessBuilder.start();

        OutputRedirector outRedirect = new OutputRedirector(
                hiveProcess.getInputStream(), "HIVE_OUTPUT");
        OutputRedirector outToConsole = new OutputRedirector(
                hiveProcess.getErrorStream(), "HIVE_LOG");

        outRedirect.start();
        outToConsole.start();    
    }

}

And the OutputRedirector class used to get the output to console.

package testing.console;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class OutputRedirector extends Thread {

    InputStream is;
    String type;

    public OutputRedirector(InputStream is, String type){
        this.is = is;
        this.type = type;
    }
    @Override
    public void run() {
        try {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(type + "> " + line);
            }
        } catch (IOException ioE) {

        }
    }

}

Upvotes: 3

Related Questions