Created
December 6, 2012 03:57
-
-
Save moaikids/4221663 to your computer and use it in GitHub Desktop.
udf_initialize.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
import sys | |
from optparse import OptionParser | |
sys.path.append('/usr/local/hive/lib/py') | |
from hive_service import ThriftHive | |
from hive_service.ttypes import HiveServerException | |
from thrift import Thrift | |
from thrift.transport import TSocket | |
from thrift.transport import TTransport | |
from thrift.protocol import TBinaryProtocol | |
def initialize(host, port, file): | |
#init Hive Thrift Server | |
print "--- execute UDF initialize query ---" | |
print "" | |
transport = TTransport.TBufferedTransport(TSocket.TSocket(host, port)) | |
protocol = TBinaryProtocol.TBinaryProtocol(transport) | |
client = ThriftHive.Client(protocol) | |
try: | |
transport.open() | |
#read hiverc | |
f = open(file) | |
try: | |
line = f.readline() | |
while line: | |
line = line.strip() | |
print line | |
try: | |
client.execute("%s" % line.strip().replace(';', '')) | |
except: | |
print "Unexpected error:", sys.exc_info() | |
line = f.readline() | |
finally: | |
f.close() | |
finally: | |
transport.close() | |
def construct_arg(): | |
parser = OptionParser() | |
parser.add_option("-a", "--host", dest="host", default="localhost") | |
parser.add_option("-p", "--port", dest="port", default=10000) | |
parser.add_option("-d", "--definition", dest="definition", default="/usr/local/hive/bin/.hiverc") | |
return parser.parse_args() | |
if __name__ == '__main__': | |
opts, args = construct_arg() | |
host = opts.host | |
port = int(opts.port) | |
file = opts.definition | |
print "host : %s" % host | |
print "port : %s" % port | |
print "definition file : %s" % file | |
print "" | |
initialize(host, port, file) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment