2016-06-16 00:39:12 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2018-04-14 10:13:23 +03:00
|
|
|
# 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.
|
2016-06-16 00:39:12 +03:00
|
|
|
|
2015-03-04 21:24:23 +03:00
|
|
|
import psycopg2
|
2016-03-29 15:03:01 +03:00
|
|
|
import psycopg2.extensions
|
2017-10-19 21:50:33 +03:00
|
|
|
from contextlib import closing
|
2015-06-14 18:02:32 +03:00
|
|
|
|
2015-07-17 23:52:56 +03:00
|
|
|
from airflow.hooks.dbapi_hook import DbApiHook
|
2015-03-04 21:24:23 +03:00
|
|
|
|
|
|
|
|
2015-07-17 23:52:56 +03:00
|
|
|
class PostgresHook(DbApiHook):
|
2017-02-12 23:43:41 +03:00
|
|
|
"""
|
2015-03-04 21:24:23 +03:00
|
|
|
Interact with Postgres.
|
2016-03-18 00:07:37 +03:00
|
|
|
You can specify ssl parameters in the extra field of your connection
|
|
|
|
as ``{"sslmode": "require", "sslcert": "/path/to/cert.pem", etc}``.
|
2017-10-02 18:12:27 +03:00
|
|
|
|
|
|
|
Note: For Redshift, use keepalives_idle in the extra connection parameters
|
|
|
|
and set it to less than 300 seconds.
|
2017-02-12 23:43:41 +03:00
|
|
|
"""
|
2015-07-18 01:15:17 +03:00
|
|
|
conn_name_attr = 'postgres_conn_id'
|
|
|
|
default_conn_name = 'postgres_default'
|
2017-01-24 17:45:39 +03:00
|
|
|
supports_autocommit = True
|
2015-07-18 01:15:17 +03:00
|
|
|
|
2017-02-12 23:43:41 +03:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(PostgresHook, self).__init__(*args, **kwargs)
|
|
|
|
self.schema = kwargs.pop("schema", None)
|
|
|
|
|
2015-03-04 21:24:23 +03:00
|
|
|
def get_conn(self):
|
2015-07-24 08:44:32 +03:00
|
|
|
conn = self.get_connection(self.postgres_conn_id)
|
2016-03-18 00:07:37 +03:00
|
|
|
conn_args = dict(
|
2015-07-17 23:52:56 +03:00
|
|
|
host=conn.host,
|
2017-05-04 22:34:38 +03:00
|
|
|
user=conn.login,
|
|
|
|
password=conn.password,
|
|
|
|
dbname=self.schema or conn.schema,
|
|
|
|
port=conn.port)
|
2016-03-18 00:07:37 +03:00
|
|
|
# check for ssl parameters in conn.extra
|
|
|
|
for arg_name, arg_val in conn.extra_dejson.items():
|
2017-10-02 18:12:27 +03:00
|
|
|
if arg_name in ['sslmode', 'sslcert', 'sslkey',
|
|
|
|
'sslrootcert', 'sslcrl', 'application_name',
|
|
|
|
'keepalives_idle']:
|
2016-03-18 00:07:37 +03:00
|
|
|
conn_args[arg_name] = arg_val
|
2017-10-02 18:12:27 +03:00
|
|
|
|
2018-01-20 12:05:16 +03:00
|
|
|
self.conn = psycopg2.connect(**conn_args)
|
|
|
|
return self.conn
|
2016-03-29 15:03:01 +03:00
|
|
|
|
2017-10-19 21:50:33 +03:00
|
|
|
def copy_expert(self, sql, filename, open=open):
|
|
|
|
'''
|
|
|
|
Executes SQL using psycopg2 copy_expert method
|
|
|
|
Necessary to execute COPY command without access to a superuser
|
|
|
|
'''
|
|
|
|
f = open(filename, 'w')
|
|
|
|
with closing(self.get_conn()) as conn:
|
|
|
|
with closing(conn.cursor()) as cur:
|
|
|
|
cur.copy_expert(sql, f)
|
|
|
|
|
2016-03-29 15:03:01 +03:00
|
|
|
@staticmethod
|
2016-11-04 16:41:44 +03:00
|
|
|
def _serialize_cell(cell, conn):
|
|
|
|
"""
|
2017-05-13 15:49:05 +03:00
|
|
|
Postgresql will adapt all arguments to the execute() method internally,
|
|
|
|
hence we return cell without any conversion.
|
2017-10-02 18:12:27 +03:00
|
|
|
|
|
|
|
See http://initd.org/psycopg/docs/advanced.html#adapting-new-types for
|
2017-05-13 15:49:05 +03:00
|
|
|
more information.
|
2016-11-04 16:41:44 +03:00
|
|
|
|
|
|
|
:param cell: The cell to insert into the table
|
|
|
|
:type cell: object
|
|
|
|
:param conn: The database connection
|
|
|
|
:type conn: connection object
|
2017-05-13 15:49:05 +03:00
|
|
|
:return: The cell
|
|
|
|
:rtype: object
|
2016-11-04 16:41:44 +03:00
|
|
|
"""
|
2017-05-13 15:49:05 +03:00
|
|
|
return cell
|