-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastexport.py
executable file
·77 lines (73 loc) · 2.74 KB
/
fastexport.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#
# 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.
from typing import Iterable, Mapping, Optional, Union
from airflow.models import BaseOperator
from airflow.providers.teradata.hooks.ttu import TtuHook
from airflow.utils.decorators import apply_defaults
class FastExportOperator(BaseOperator):
"""
Export a table from Teradata to csv file
:param sql_select_stmt: Select statament to export
:type sql_select_stmt: str
:param output_file: output file to export
:type output_file: str
"""
template_fields = ('sql','output_file',)
template_ext = ('.sql',)
ui_color = '#a8e4b1'
def __init__(
self,
*,
sql_select_stmt: str,
output_file: str,
delimiter: str = ';',
encoding: str = 'UTF8',
spool_mode: str = 'SPOOL',
xcom_push: bool = True,
ttu_conn_id: str = 'ttu_default',
max_sessions: Optional[int] = 1,
block_size: Optional[int] = 1048472,
**kwargs,
) -> None:
super().__init__(**kwargs)
self.sql = sql_select_stmt
self.output_file = output_file
self.delimiter = delimiter
self.encoding = encoding
self.spool_mode = spool_mode
self.xcom_push = xcom_push
self._hook = None
self.ttu_conn_id = ttu_conn_id
self.max_sessions = max_sessions
self.block_size = block_size
def execute(self, context):
"""
Call the function
"""
self._hook = TtuHook(ttu_conn_id=self.ttu_conn_id)
self._hook.execute_tptexport(sql=self.sql,
output_file=self.output_file,
delimiter=self.delimiter,
encoding=self.encoding,
spool_mode=self.spool_mode,
xcom_push_flag=self.xcom_push,
max_sessions=self.max_sessions,
block_size=self.block_size
)
def on_kill(self):
self._hook.on_kill()