zhongjiajie commented on a change in pull request #4405: [AIRFLOW-3598] Add 
tests for MsSqlToHiveTransfer
URL: https://github.com/apache/airflow/pull/4405#discussion_r256213166
 
 

 ##########
 File path: airflow/operators/mssql_to_hive.py
 ##########
 @@ -98,38 +99,34 @@ def __init__(
 
     @classmethod
     def type_map(cls, mssql_type):
-        t = pymssql
-        d = {
-            t.BINARY.value: 'INT',
-            t.DECIMAL.value: 'FLOAT',
-            t.NUMBER.value: 'INT',
+        map_dict = {
+            pymssql.BINARY.value: 'INT',
+            pymssql.DECIMAL.value: 'FLOAT',
+            pymssql.NUMBER.value: 'INT',
         }
-        return d[mssql_type] if mssql_type in d else 'STRING'
+        return map_dict[mssql_type] if mssql_type in map_dict else 'STRING'
 
     def execute(self, context):
-        hive = HiveCliHook(hive_cli_conn_id=self.hive_cli_conn_id)
         mssql = MsSqlHook(mssql_conn_id=self.mssql_conn_id)
-
         self.log.info("Dumping Microsoft SQL Server query results to local 
file")
-        conn = mssql.get_conn()
-        cursor = conn.cursor()
-        cursor.execute(self.sql)
-        with NamedTemporaryFile("w") as f:
-            csv_writer = csv.writer(f, delimiter=self.delimiter, 
encoding='utf-8')
-            field_dict = OrderedDict()
-            col_count = 0
-            for field in cursor.description:
-                col_count += 1
-                col_position = "Column{position}".format(position=col_count)
-                field_dict[col_position if field[0] == '' else field[0]] \
-                    = self.type_map(field[1])
-            csv_writer.writerows(cursor)
-            f.flush()
-            cursor.close()
-            conn.close()
+        with closing(mssql.get_conn()) as conn:
+            with closing(conn.cursor()) as cursor:
+                cursor.execute(self.sql)
+                with NamedTemporaryFile("w") as tmp_file:
+                    csv_writer = csv.writer(tmp_file, 
delimiter=self.delimiter, encoding='utf-8')
+                    field_dict = OrderedDict()
+                    col_count = 0
+                    for field in cursor.description:
+                        col_count += 1
+                        col_position = 
"Column{position}".format(position=col_count)
+                        field_dict[col_position if field[0] == '' else 
field[0]] = self.type_map(field[1])
+                    csv_writer.writerows(cursor)
+                    tmp_file.flush()
+
+            hive = HiveCliHook(hive_cli_conn_id=self.hive_cli_conn_id)
 
 Review comment:
   I wonder if we should change code style.
   * 1st we just want to add unit test to it.
   * 2nd many of transfer oprator are same code format below
   
   excepting this change @feng-tao
   ```python
   src_hook = XXXHook(conn_id=self.conn_id)
   dest_hook = YYYHook(conn_id=self.conn_id)
   
   ...<get data from src_hook>
   with NamedTemporaryFile("w") as f:
       ...
       ...<write data to temp file>...
       ...
       dest_hook.transfer(param)
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to