Skip to content

Commit

Permalink
Add Tabular provider
Browse files Browse the repository at this point in the history
Co-authored-by: Kyle Bendickson <[email protected]>
  • Loading branch information
Fokko and kbendick committed May 17, 2022
1 parent c3cd787 commit 29e6511
Show file tree
Hide file tree
Showing 27 changed files with 660 additions and 146 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
/airflow/providers/google/ @turbaszek
/airflow/providers/snowflake/ @turbaszek @potiuk @mik-laj
/airflow/providers/cncf/kubernetes @jedcunningham
/airflow/providers/tabular/ @Fokko
/docs/apache-airflow-providers-cncf-kubernetes @jedcunningham

# Dev tools
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/airflow_providers_bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ body:
- sqlite
- ssh
- tableau
- tabular
- telegram
- trino
- vertica
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -655,8 +655,8 @@ grpc, hashicorp, hdfs, hive, http, imap, influxdb, jdbc, jenkins, jira, kerberos
leveldb, microsoft.azure, microsoft.mssql, microsoft.psrp, microsoft.winrm, mongo, mssql, mysql,
neo4j, odbc, openfaas, opsgenie, oracle, pagerduty, pandas, papermill, password, pinot, plexus,
postgres, presto, qds, qubole, rabbitmq, redis, s3, salesforce, samba, segment, sendgrid, sentry,
sftp, singularity, slack, snowflake, spark, sqlite, ssh, statsd, tableau, telegram, trino, vertica,
virtualenv, webhdfs, winrm, yandex, zendesk
sftp, singularity, slack, snowflake, spark, sqlite, ssh, statsd, tableau, tabular, telegram, trino,
vertica, virtualenv, webhdfs, winrm, yandex, zendesk
.. END EXTRAS HERE
Provider packages
Expand Down
4 changes: 2 additions & 2 deletions INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ grpc, hashicorp, hdfs, hive, http, imap, influxdb, jdbc, jenkins, jira, kerberos
leveldb, microsoft.azure, microsoft.mssql, microsoft.psrp, microsoft.winrm, mongo, mssql, mysql,
neo4j, odbc, openfaas, opsgenie, oracle, pagerduty, pandas, papermill, password, pinot, plexus,
postgres, presto, qds, qubole, rabbitmq, redis, s3, salesforce, samba, segment, sendgrid, sentry,
sftp, singularity, slack, snowflake, spark, sqlite, ssh, statsd, tableau, telegram, trino, vertica,
virtualenv, webhdfs, winrm, yandex, zendesk
sftp, singularity, slack, snowflake, spark, sqlite, ssh, statsd, tableau, tabular, telegram, trino,
vertica, virtualenv, webhdfs, winrm, yandex, zendesk
# END EXTRAS HERE

# For installing Airflow in development environments - see CONTRIBUTING.rst
Expand Down
24 changes: 24 additions & 0 deletions airflow/providers/tabular/CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.. 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.
Changelog
---------

0.0.1
.....

Initial version of the provider.
17 changes: 17 additions & 0 deletions airflow/providers/tabular/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# 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.
17 changes: 17 additions & 0 deletions airflow/providers/tabular/example_dags/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# 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.
46 changes: 46 additions & 0 deletions airflow/providers/tabular/example_dags/example_tabular.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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 datetime import datetime, timedelta

from airflow import DAG
from airflow.operators.bash import BashOperator
from airflow.providers.tabular.hooks.tabular import TabularHook

default_args = {
"owner": "airflow",
"depends_on_past": False,
"email": ["[email protected]"],
"email_on_failure": False,
"email_on_retry": False,
}

bash_command = f"""
echo "Our token: {TabularHook().get_token_macro()}"
echo "Also as an environment variable:"
env | grep TOKEN
"""

with DAG(
"tabular", default_args=default_args, start_date=datetime(2021, 1, 1), schedule_interval=timedelta(1)
) as dag:
# This also works for the SparkSubmit operator
BashOperator(
task_id="with_tabular_environment_variable",
bash_command=bash_command,
env={"TOKEN": TabularHook().get_token_macro()},
)
17 changes: 17 additions & 0 deletions airflow/providers/tabular/hooks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# 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.
104 changes: 104 additions & 0 deletions airflow/providers/tabular/hooks/tabular.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# 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 Any, Dict, Tuple

import requests
from requests import HTTPError

from airflow.hooks.base import BaseHook

DEFAULT_TABULAR_URL = "https://api.tabulardata.io/ws/v1"

TOKENS_ENDPOINT = "oauth/tokens"


class TabularHook(BaseHook):
"""
This hook acts as a base hook for tabular services. It offers the ability to generate temporary,
short-lived session tokens to use within Airflow submitted jobs.
:param tabular_conn_id: The :ref:`Tabular connection id<howto/connection:tabular>`
which refers to the information to connect to the Tabular OAuth service.
"""

conn_name_attr = 'tabular_conn_id'
default_conn_name = "tabular_default"
conn_type = "tabular"
hook_name = "Tabular"

@staticmethod
def get_connection_form_widgets() -> Dict[str, Any]:
"""Returns connection widgets to add to connection form"""
from flask_appbuilder.fieldwidgets import BS3TextFieldWidget
from flask_babel import lazy_gettext
from wtforms import StringField

return {
"extra__tabular__baseUrl": StringField(
lazy_gettext("Tabular Base URL"), widget=BS3TextFieldWidget()
),
}

@staticmethod
def get_ui_field_behaviour() -> Dict[str, Any]:
"""Returns custom field behaviour"""
return {
"hidden_fields": ["schema", "port", "host"],
"relabeling": {
"login": "Tabular Client ID",
"password": "Tabular Client Secret",
},
"placeholders": {
"login": "client_id (token credentials auth)",
"password": "secret (token credentials auth)",
"extra__tabular__baseUrl": DEFAULT_TABULAR_URL,
},
}

def __init__(self, tabular_conn_id: str = default_conn_name) -> None:
super().__init__()
self.conn_id = tabular_conn_id

def test_connection(self) -> Tuple[bool, str]:
"""Test the Tabular connection."""
try:
self.get_conn()
return True, "Successfully fetched token from Tabular"
except HTTPError as e:
return False, f"HTTP Error: {e}"
except Exception as e:
return False, str(e)

def get_conn(self) -> str:
"""Obtain a short-lived access token via a client_id and client_secret."""
conn = self.get_connection(self.conn_id)
extras = conn.extra_dejson
client_id = conn.login
client_secret = conn.password

base_url = extras.get("extra__tabular__baseUrl", DEFAULT_TABULAR_URL)
headers = {"Content-Type": "application/x-www-form-urlencoded"}
data = {"client_id": client_id, "client_secret": client_secret}

response = requests.post(f"{base_url}/{TOKENS_ENDPOINT}", data=data, headers=headers)
response.raise_for_status()

return response.json()["access_token"]

def get_token_macro(self):
return f'{{{{ conn.{self.conn_id}.get_hook().get_conn() }}}}'
43 changes: 43 additions & 0 deletions airflow/providers/tabular/provider.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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.

---
package-name: apache-airflow-providers-tabular
name: Tabular
description: |
`Tabular <https://tabular.io/>`__
versions:
- 0.0.1

additional-dependencies:
- apache-airflow>=2.2.0

integrations:
- integration-name: Tabular
external-doc-url: https://tabular.io/docs/
logo: /integration-logos/tabular/tabular.jpeg
tags: [software]

hooks:
- integration-name: Tabular
python-modules:
- airflow.providers.tabular.hooks.tabular

connection-types:
- hook-class-name: airflow.providers.tabular.hooks.tabular.TabularHook
connection-type: tabular
8 changes: 8 additions & 0 deletions airflow/utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,14 @@ def create_default_connections(session: Session = NEW_SESSION):
),
session,
)
merge_conn(
Connection(
conn_id="tabular_default",
conn_type="tabular",
extra='{"extra__tabular__baseUrl": "https://api.tabulardata.io/ws/v1"}',
),
session,
)
merge_conn(
Connection(
conn_id="trino_default",
Expand Down
27 changes: 27 additions & 0 deletions docs/apache-airflow-providers-tabular/commits.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

.. 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.
Package apache-airflow-providers-tabular
------------------------------------------------------

`Tabular <https://tabular.io/>`__


This is detailed commit list of changes for versions provider package: ``tabular``.
For high-level changelog, see :doc:`package information including changelog <index>`.
41 changes: 41 additions & 0 deletions docs/apache-airflow-providers-tabular/connections.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
.. 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.
.. _howto/connection:tabular:

Tabular Connection
==================

The Tabular connection type enables connection to Tabular to request a short lived token to access the Apache Iceberg tables. This token can be injected as an environment variable, to be used with Trino, Spark, Flink or your favorite query engine that supports Apache Iceberg.

Default Connection IDs
----------------------

Tabular Hook uses the parameter ``tabular_conn_id`` for Connection IDs and the value of the parameter as ``tabular_default`` by default. You can create multiple connections in case you want to switch between environments.

Configuring the Connection
--------------------------
Client ID
The Client ID from Tabular

Client Secret
The Client Secret from Tabular

Extra (optional, connection parameters)
Specify the extra parameters (as json dictionary) that can be used the Tabular connection. The following parameters out of the standard python parameters are supported:

* ``base_url`` - Optional, sets the URL to the Tabular environment. By default `https://api.tabulardata.io/ws/v1`
Loading

0 comments on commit 29e6511

Please sign in to comment.