Source code for airflow.providers.apache.hive.transfers.hive_to_mysql
## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License."""This module contains operator to move data from Hive to MySQL."""fromtempfileimportNamedTemporaryFilefromtypingimportDict,Optionalfromairflow.modelsimportBaseOperatorfromairflow.providers.apache.hive.hooks.hiveimportHiveServer2Hookfromairflow.providers.mysql.hooks.mysqlimportMySqlHookfromairflow.utils.operator_helpersimportcontext_to_airflow_vars
[docs]classHiveToMySqlOperator(BaseOperator):""" Moves data from Hive to MySQL, note that for now the data is loaded into memory before being pushed to MySQL, so this operator should be used for smallish amount of data. :param sql: SQL query to execute against Hive server. (templated) :type sql: str :param mysql_table: target MySQL table, use dot notation to target a specific database. (templated) :type mysql_table: str :param mysql_conn_id: source mysql connection :type mysql_conn_id: str :param metastore_conn_id: Reference to the :ref:`metastore thrift service connection id <howto/connection:hive_metastore>`. :type metastore_conn_id: str :param mysql_preoperator: sql statement to run against mysql prior to import, typically use to truncate of delete in place of the data coming in, allowing the task to be idempotent (running the task twice won't double load data). (templated) :type mysql_preoperator: str :param mysql_postoperator: sql statement to run against mysql after the import, typically used to move data from staging to production and issue cleanup commands. (templated) :type mysql_postoperator: str :param bulk_load: flag to use bulk_load option. This loads mysql directly from a tab-delimited text file using the LOAD DATA LOCAL INFILE command. This option requires an extra connection parameter for the destination MySQL connection: {'local_infile': true}. :type bulk_load: bool :param hive_conf: :type hive_conf: dict """
[docs]defexecute(self,context):hive=HiveServer2Hook(hiveserver2_conn_id=self.hiveserver2_conn_id)self.log.info("Extracting data from Hive: %s",self.sql)hive_conf=context_to_airflow_vars(context)ifself.hive_conf:hive_conf.update(self.hive_conf)ifself.bulk_load:withNamedTemporaryFile()astmp_file:hive.to_csv(self.sql,tmp_file.name,delimiter='\t',lineterminator='\n',output_header=False,hive_conf=hive_conf,)mysql=self._call_preoperator()mysql.bulk_load(table=self.mysql_table,tmp_file=tmp_file.name)else:hive_results=hive.get_records(self.sql,hive_conf=hive_conf)mysql=self._call_preoperator()mysql.insert_rows(table=self.mysql_table,rows=hive_results)ifself.mysql_postoperator:self.log.info("Running MySQL postoperator")mysql.run(self.mysql_postoperator)self.log.info("Done.")
def_call_preoperator(self):mysql=MySqlHook(mysql_conn_id=self.mysql_conn_id)ifself.mysql_preoperator:self.log.info("Running MySQL preoperator")mysql.run(self.mysql_preoperator)self.log.info("Inserting rows into MySQL")returnmysql