@@ -88,6 +88,7 @@ def __init__(
8888 self .auth = conn .extra_dejson .get ('auth' , 'noSasl' )
8989 self .conn = conn
9090 self .run_as = run_as
91+ self .sub_process = None
9192
9293 if mapred_queue_priority :
9394 mapred_queue_priority = mapred_queue_priority .upper ()
@@ -241,24 +242,24 @@ def run_cli(self, hql, schema=None, verbose=True, hive_conf=None):
241242
242243 if verbose :
243244 self .log .info ("%s" , " " .join (hive_cmd ))
244- sp = subprocess .Popen (
245+ sub_process = subprocess .Popen (
245246 hive_cmd ,
246247 stdout = subprocess .PIPE ,
247248 stderr = subprocess .STDOUT ,
248249 cwd = tmp_dir ,
249250 close_fds = True )
250- self .sp = sp
251+ self .sub_process = sub_process
251252 stdout = ''
252253 while True :
253- line = sp .stdout .readline ()
254+ line = sub_process .stdout .readline ()
254255 if not line :
255256 break
256257 stdout += line .decode ('UTF-8' )
257258 if verbose :
258259 self .log .info (line .decode ('UTF-8' ).strip ())
259- sp .wait ()
260+ sub_process .wait ()
260261
261- if sp .returncode :
262+ if sub_process .returncode :
262263 raise AirflowException (stdout )
263264
264265 return stdout
@@ -338,7 +339,7 @@ def load_df(
338339 """
339340
340341 def _infer_field_types_from_df (df ):
341- DTYPE_KIND_HIVE_TYPE = {
342+ dtype_kind_hive_type = {
342343 'b' : 'BOOLEAN' , # boolean
343344 'i' : 'BIGINT' , # signed integer
344345 'u' : 'BIGINT' , # unsigned integer
@@ -351,10 +352,10 @@ def _infer_field_types_from_df(df):
351352 'V' : 'STRING' # void
352353 }
353354
354- d = OrderedDict ()
355+ order_type = OrderedDict ()
355356 for col , dtype in df .dtypes .iteritems ():
356- d [col ] = DTYPE_KIND_HIVE_TYPE [dtype .kind ]
357- return d
357+ order_type [col ] = dtype_kind_hive_type [dtype .kind ]
358+ return order_type
358359
359360 if pandas_kwargs is None :
360361 pandas_kwargs = {}
@@ -466,12 +467,15 @@ def load_file(
466467 self .run_cli (hql )
467468
468469 def kill (self ):
470+ """
471+ Kill Hive cli command
472+ """
469473 if hasattr (self , 'sp' ):
470- if self .sp .poll () is None :
474+ if self .sub_process .poll () is None :
471475 print ("Killing the Hive job" )
472- self .sp .terminate ()
476+ self .sub_process .terminate ()
473477 time .sleep (60 )
474- self .sp .kill ()
478+ self .sub_process .kill ()
475479
476480
477481class HiveMetastoreHook (BaseHook ):
@@ -488,9 +492,9 @@ def __init__(self, metastore_conn_id='metastore_default'):
488492 def __getstate__ (self ):
489493 # This is for pickling to work despite the thirft hive client not
490494 # being pickable
491- d = dict (self .__dict__ )
492- del d ['metastore' ]
493- return d
495+ state = dict (self .__dict__ )
496+ del state ['metastore' ]
497+ return state
494498
495499 def __setstate__ (self , d ):
496500 self .__dict__ .update (d )
@@ -504,18 +508,18 @@ def get_metastore_client(self):
504508 from thrift .transport import TSocket , TTransport
505509 from thrift .protocol import TBinaryProtocol
506510
507- ms = self ._find_valid_server ()
511+ conn = self ._find_valid_server ()
508512
509- if ms is None :
513+ if not conn :
510514 raise AirflowException ("Failed to locate the valid server." )
511515
512- auth_mechanism = ms .extra_dejson .get ('authMechanism' , 'NOSASL' )
516+ auth_mechanism = conn .extra_dejson .get ('authMechanism' , 'NOSASL' )
513517
514518 if conf .get ('core' , 'security' ) == 'kerberos' :
515- auth_mechanism = ms .extra_dejson .get ('authMechanism' , 'GSSAPI' )
516- kerberos_service_name = ms .extra_dejson .get ('kerberos_service_name' , 'hive' )
519+ auth_mechanism = conn .extra_dejson .get ('authMechanism' , 'GSSAPI' )
520+ kerberos_service_name = conn .extra_dejson .get ('kerberos_service_name' , 'hive' )
517521
518- conn_socket = TSocket .TSocket (ms .host , ms .port )
522+ conn_socket = TSocket .TSocket (conn .host , conn .port )
519523
520524 if conf .get ('core' , 'security' ) == 'kerberos' \
521525 and auth_mechanism == 'GSSAPI' :
@@ -526,7 +530,7 @@ def get_metastore_client(self):
526530
527531 def sasl_factory ():
528532 sasl_client = sasl .Client ()
529- sasl_client .setAttr ("host" , ms .host )
533+ sasl_client .setAttr ("host" , conn .host )
530534 sasl_client .setAttr ("service" , kerberos_service_name )
531535 sasl_client .init ()
532536 return sasl_client
@@ -551,6 +555,7 @@ def _find_valid_server(self):
551555 return conn
552556 else :
553557 self .log .info ("Could not connect to %s:%s" , conn .host , conn .port )
558+ return None
554559
555560 def get_conn (self ):
556561 return self .metastore
@@ -577,10 +582,7 @@ def check_for_partition(self, schema, table, partition):
577582 partitions = client .get_partitions_by_filter (
578583 schema , table , partition , 1 )
579584
580- if partitions :
581- return True
582- else :
583- return False
585+ return bool (partitions )
584586
585587 def check_for_named_partition (self , schema , table , partition_name ):
586588 """
@@ -634,8 +636,7 @@ def get_databases(self, pattern='*'):
634636 with self .metastore as client :
635637 return client .get_databases (pattern )
636638
637- def get_partitions (
638- self , schema , table_name , filter = None ):
639+ def get_partitions (self , schema , table_name , partition_filter = None ):
639640 """
640641 Returns a list of all partitions in a table. Works only
641642 for tables with less than 32767 (java short max val).
@@ -654,10 +655,10 @@ def get_partitions(
654655 if len (table .partitionKeys ) == 0 :
655656 raise AirflowException ("The table isn't partitioned" )
656657 else :
657- if filter :
658+ if partition_filter :
658659 parts = client .get_partitions_by_filter (
659660 db_name = schema , tbl_name = table_name ,
660- filter = filter , max_parts = HiveMetastoreHook .MAX_PART_COUNT )
661+ filter = partition_filter , max_parts = HiveMetastoreHook .MAX_PART_COUNT )
661662 else :
662663 parts = client .get_partitions (
663664 db_name = schema , tbl_name = table_name ,
@@ -770,7 +771,7 @@ def table_exists(self, table_name, db='default'):
770771 try :
771772 self .get_table (table_name , db )
772773 return True
773- except Exception :
774+ except Exception : # pylint: disable=broad-except
774775 return False
775776
776777
@@ -849,7 +850,7 @@ def _get_results(self, hql, schema='default', fetch_size=None, hive_conf=None):
849850 lowered_statement .startswith ('show' ) or
850851 (lowered_statement .startswith ('set' ) and
851852 '=' not in lowered_statement )):
852- description = [ c for c in cur .description ]
853+ description = cur .description
853854 if previous_description and previous_description != description :
854855 message = '''The statements are producing different descriptions:
855856 Current: {}
0 commit comments