One way to interact with hive is by starting hives thrift server. If
you want to use the raw java API,  I stole some code from the Command
Line Interface and made a simple driver program. (I attached it).

You could also take a look at the source hwi folder. That is how we
have multiple hive clients started inside a web application.

I would suggest the thrift service as that should present yoru client
program with a stable API. If you just make a work-alike program like
TestHive upstream changes may  be an issue down the line.
package test;
import java.util.*;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.ql.exec.Utilities;
import org.apache.hadoop.hive.ql.exec.Utilities.StreamPrinter;
import org.apache.hadoop.hive.ql.session.SessionState;
import org.apache.hadoop.hive.ql.Driver;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hive.ql.session.SessionState.LogHelper;
import org.apache.hadoop.hive.ql.*;
import org.apache.hadoop.hive.cli.*;

import java.io.*;
public class TestHive {

  public static void main(String [] args) throws Exception {

    OptionsProcessor oproc = new OptionsProcessor();
    if(! oproc.process_stage1(args)) {
      System.out.println("Problem processing arfs");
    }

    SessionState.initHiveLog4j();
    CliSessionState ss = new CliSessionState (new HiveConf(SessionState.class));

   // ss.in = System.in;
   // ss.out = new PrintStream(System.out,true, "UTF-8");
   // ss.err = new PrintStream(System.err,true, "UTF-8");

    SessionState.start(ss);

    if(! oproc.process_stage2(ss)) {
      System.out.println("Problem with stage2");
    }

    SetProcessor sp=new SetProcessor();
    Driver qp=new Driver();
    int ret = -1;
    int sret=-1;
    //Log LOG = LogFactory.getLog("CliDriver");
    //LogHelper console = new LogHelper(LOG);
    sret = sp.run("set mapred.map.tasks=1");
    sret = sp.run("set mapred.reduce.tasks=1");
    
    ret = qp.run("SELECT people.* from people");
    Vector <String> res = new Vector<String>();
    while (qp.getResults(res)) {
        System.out.println("ResSize:"+ res.size());
        for (String row:res){
          System.out.print(row+"\n");
        }
    }
    
    //res.clear();
  } // end main
} // end TestHive

Reply via email to