Source

hiver / hiver / hive_service / ThriftHive-remote

#!/usr/bin/env python
#
# Autogenerated by Thrift Compiler (0.7.0)
#
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
#

import sys
import pprint
from urlparse import urlparse
from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.transport import THttpClient
from thrift.protocol import TBinaryProtocol

import ThriftHive
from ttypes import *

if len(sys.argv) <= 1 or sys.argv[1] == '--help':
  print ''
  print 'Usage: ' + sys.argv[0] + ' [-h host:port] [-u url] [-f[ramed]] function [arg1 [arg2...]]'
  print ''
  print 'Functions:'
  print '  void execute(string query)'
  print '  string fetchOne()'
  print '   fetchN(i32 numRows)'
  print '   fetchAll()'
  print '  Schema getSchema()'
  print '  Schema getThriftSchema()'
  print '  HiveClusterStatus getClusterStatus()'
  print '  QueryPlan getQueryPlan()'
  print '  void clean()'
  print ''
  sys.exit(0)

pp = pprint.PrettyPrinter(indent = 2)
host = 'localhost'
port = 9090
uri = ''
framed = False
http = False
argi = 1

if sys.argv[argi] == '-h':
  parts = sys.argv[argi+1].split(':')
  host = parts[0]
  port = int(parts[1])
  argi += 2

if sys.argv[argi] == '-u':
  url = urlparse(sys.argv[argi+1])
  parts = url[1].split(':')
  host = parts[0]
  if len(parts) > 1:
    port = int(parts[1])
  else:
    port = 80
  uri = url[2]
  if url[4]:
    uri += '?%s' % url[4]
  http = True
  argi += 2

if sys.argv[argi] == '-f' or sys.argv[argi] == '-framed':
  framed = True
  argi += 1

cmd = sys.argv[argi]
args = sys.argv[argi+1:]

if http:
  transport = THttpClient.THttpClient(host, port, uri)
else:
  socket = TSocket.TSocket(host, port)
  if framed:
    transport = TTransport.TFramedTransport(socket)
  else:
    transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = ThriftHive.Client(protocol)
transport.open()

if cmd == 'execute':
  if len(args) != 1:
    print 'execute requires 1 args'
    sys.exit(1)
  pp.pprint(client.execute(args[0],))

elif cmd == 'fetchOne':
  if len(args) != 0:
    print 'fetchOne requires 0 args'
    sys.exit(1)
  pp.pprint(client.fetchOne())

elif cmd == 'fetchN':
  if len(args) != 1:
    print 'fetchN requires 1 args'
    sys.exit(1)
  pp.pprint(client.fetchN(eval(args[0]),))

elif cmd == 'fetchAll':
  if len(args) != 0:
    print 'fetchAll requires 0 args'
    sys.exit(1)
  pp.pprint(client.fetchAll())

elif cmd == 'getSchema':
  if len(args) != 0:
    print 'getSchema requires 0 args'
    sys.exit(1)
  pp.pprint(client.getSchema())

elif cmd == 'getThriftSchema':
  if len(args) != 0:
    print 'getThriftSchema requires 0 args'
    sys.exit(1)
  pp.pprint(client.getThriftSchema())

elif cmd == 'getClusterStatus':
  if len(args) != 0:
    print 'getClusterStatus requires 0 args'
    sys.exit(1)
  pp.pprint(client.getClusterStatus())

elif cmd == 'getQueryPlan':
  if len(args) != 0:
    print 'getQueryPlan requires 0 args'
    sys.exit(1)
  pp.pprint(client.getQueryPlan())

elif cmd == 'clean':
  if len(args) != 0:
    print 'clean requires 0 args'
    sys.exit(1)
  pp.pprint(client.clean())

else:
  print 'Unrecognized method %s' % cmd
  sys.exit(1)

transport.close()