hive-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Zizon Qiu <zzd...@gmail.com>
Subject Re: Embedding Hive
Date Wed, 25 Apr 2012 05:52:27 GMT
I had try the followings, and it works..

SessionState session = new SessionState(conf);// the confi should have
enough informations,such as the access to hive meta server database(the
myslq 额头)
session.setIsSilent(true);
session.setIsVerbose(true);
SessionState.start(session);
Driver driver = new Driver();
driver.init();
driver.run(query).getResponseCode()

FileOutputStream file = null;

//ArrayList<String> result = new ArrayList<String>();
//driver.getResults(result);
// driver.getResult had a limit in rows that can fetch from,so, I look into
the implementation,and extract followings from the hive source.

JobConf job = new JobConf(conf, ExecDriver.class);
FetchOperator operator = new
FetchOperator(driver.getPlan().getFetchTask().getWork(), job);
String serdeName =
HiveConf.getVar(conf,HiveConf.ConfVars.HIVEFETCHOUTPUTSERDE);
Class<? extends SerDe> serdeClass = Class.forName(serdeName,
true,JavaUtils.getClassLoader()).asSubclass(SerDe.class);

SerDe serde = ReflectionUtils.newInstance(serdeClass, null);
Properties serdeProp = new Properties();

if (serde instanceof DelimitedJSONSerDe) {
serdeProp.put(Constants.SERIALIZATION_FORMAT,"" + Utilities.tabCode);

serdeProp.put(Constants.SERIALIZATION_NULL_FORMAT,driver.getPlan().getFetchTask().getWork().getSerializationNullFormat());
}
serde.initialize(job, serdeProp);
file = new FileOutputStream(out_file);
InspectableObject io = operator.getNextRow();
while (io != null) {
file.write((((Text) serde.serialize(io.o, io.oi)).toString() +
"\n").getBytes());
 io = operator.getNextRow();
}

On Wed, Apr 25, 2012 at 1:42 PM, Dilip Joseph <dilip.antony.joseph@gmail.com
> wrote:

> You can directly embed the hive client library in your java program, and
> use it without running a hive service.  My blog post at
> http://csgrad.blogspot.com/2010/04/to-use-language-other-than-java-say.htmldescribes
how to run hive queries from Jython.  Something very similar
> should work for Java.
>
> Dilip
>
>
> On Tue, Apr 24, 2012 at 10:33 PM, Vinod Singh <vinod@vinodsingh.com>wrote:
>
>> Hello,
>>
>> I would like to embed Hive (client) in my application to execute a
>> sequence of queries. Right now I do it using CLI (hive -f myScript.sql).
>> Problem with this approach is that I do not get an return / error code to
>> know the status of query programmatically.
>>
>> So my question is what is the best way to embed Hive in a Java
>> application? I know JDBC is one approach but that will need Hive service
>> should be running somewhere and I want to avoid that.
>>
>> Thanks,
>> Vinod
>>
>
>
>
> --
> _________________________________________
> Dilip Antony Joseph
> http://csgrad.blogspot.com
> http://www.marydilip.info
>

Mime
View raw message