Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

from builtins import str 

__author__ = 'janomar' 

 

import logging 

import jaydebeapi 

 

from airflow.hooks.dbapi_hook import DbApiHook 

 

class JdbcHook(DbApiHook): 

    """ 

    General hook for jdbc db access. 

 

    If a connection id is specified, host, port, schema, username and password will be taken from the predefined connection. 

    Raises an airflow error if the given connection id doesn't exist. 

    Otherwise host, port, schema, username and password can be specified on the fly. 

 

 

 

    :param jdbc_url: jdbc connection url 

    :type jdbc_url: string 

    :param jdbc_driver_name: jdbc driver name 

    :type jdbc_driver_name: string 

    :param jdbc_driver_loc: path to jdbc driver 

    :type jdbc_driver_loc: string 

    :param conn_id: reference to a predefined database 

    :type conn_id: string 

    :param sql: the sql code to be executed 

    :type sql: string or string pointing to a template file. File must have 

        a '.sql' extensions. 

    """ 

 

 

    conn_name_attr = 'jdbc_conn_id' 

    default_conn_name = 'jdbc_default' 

    supports_autocommit = True 

 

    def get_conn(self): 

        conn = self.get_connection(getattr(self, self.conn_name_attr)) 

        host = conn.host 

        login = conn.login 

        psw = conn.password 

        jdbc_driver_loc = conn.extra_dejson.get('extra__jdbc__drv_path') 

        jdbc_driver_name = conn.extra_dejson.get('extra__jdbc__drv_clsname') 

 

        conn = jaydebeapi.connect(jdbc_driver_name, 

                           [str(host), str(login), str(psw)], 

                                  jdbc_driver_loc,) 

        return conn 

 

    def set_autocommit(self, conn, autocommit): 

        """ 

        Enable or disable autocommit for the given connection 

        :param conn: The connection 

        :return: 

        """ 

        conn.jconn.autocommit = autocommit