Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to override the conn type for Druid #42793

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion providers/src/airflow/providers/apache/druid/hooks/druid.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ def get_conn_url(self, ingestion_type: IngestionType = IngestionType.BATCH) -> s
"""Get Druid connection url."""
host = self.conn.host
port = self.conn.port
conn_type = self.conn.conn_type or "http"
if self.conn.schema:
conn_type = self.conn.schema
else:
conn_type = self.conn.conn_type or "http"
if ingestion_type == IngestionType.BATCH:
endpoint = self.conn.extra_dejson.get("endpoint", "")
else:
Expand Down
44 changes: 39 additions & 5 deletions providers/tests/apache/druid/hooks/test_druid.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ class TestDRuidhook(DruidHook):
self.is_sql_based_ingestion = False

def get_conn_url(self, ingestion_type: IngestionType = IngestionType.BATCH):
if self.conn.schema:
conn_type = self.conn.schema
else:
conn_type = "http"

if ingestion_type == IngestionType.MSQ:
return "http://druid-overlord:8081/druid/v2/sql/task"
return "http://druid-overlord:8081/druid/indexer/v1/task"
return f"{conn_type}://druid-overlord:8081/druid/v2/sql/task"
return f"{conn_type}://druid-overlord:8081/druid/indexer/v1/task"

self.db_hook = TestDRuidhook()

Expand Down Expand Up @@ -257,7 +262,8 @@ def get_conn_url(self, ingestion_type: IngestionType = IngestionType.BATCH):
def test_conn_property(self, mock_get_connection):
get_conn_value = MagicMock()
get_conn_value.host = "test_host"
get_conn_value.conn_type = "https"
get_conn_value.conn_type = "http"
get_conn_value.schema = None
get_conn_value.port = "1"
get_conn_value.extra_dejson = {"endpoint": "ingest"}
mock_get_connection.return_value = get_conn_value
Expand All @@ -268,8 +274,22 @@ def test_conn_property(self, mock_get_connection):
def test_get_conn_url(self, mock_get_connection):
get_conn_value = MagicMock()
get_conn_value.host = "test_host"
get_conn_value.conn_type = "https"
get_conn_value.conn_type = "http"
get_conn_value.schema = None
get_conn_value.port = "1"
get_conn_value.extra_dejson = {"endpoint": "ingest"}
mock_get_connection.return_value = get_conn_value
hook = DruidHook(timeout=1, max_ingestion_time=5)
assert hook.get_conn_url() == "http://test_host:1/ingest"

@patch("airflow.providers.apache.druid.hooks.druid.DruidHook.get_connection")
def test_get_conn_url_with_schema(self, mock_get_connection):
get_conn_value = MagicMock()
get_conn_value.host = "test_host"
get_conn_value.conn_type = "http"
get_conn_value.schema = None
get_conn_value.port = "1"
get_conn_value.schema = "https"
get_conn_value.extra_dejson = {"endpoint": "ingest"}
mock_get_connection.return_value = get_conn_value
hook = DruidHook(timeout=1, max_ingestion_time=5)
Expand All @@ -279,8 +299,21 @@ def test_get_conn_url(self, mock_get_connection):
def test_get_conn_url_with_ingestion_type(self, mock_get_connection):
get_conn_value = MagicMock()
get_conn_value.host = "test_host"
get_conn_value.conn_type = "https"
get_conn_value.conn_type = "http"
get_conn_value.schema = None
get_conn_value.port = "1"
get_conn_value.extra_dejson = {"endpoint": "ingest", "msq_endpoint": "sql_ingest"}
mock_get_connection.return_value = get_conn_value
hook = DruidHook(timeout=1, max_ingestion_time=5)
assert hook.get_conn_url(IngestionType.MSQ) == "http://test_host:1/sql_ingest"

@patch("airflow.providers.apache.druid.hooks.druid.DruidHook.get_connection")
def test_get_conn_url_with_ingestion_type_and_schema(self, mock_get_connection):
get_conn_value = MagicMock()
get_conn_value.host = "test_host"
get_conn_value.conn_type = "http"
get_conn_value.port = "1"
get_conn_value.schema = "https"
get_conn_value.extra_dejson = {"endpoint": "ingest", "msq_endpoint": "sql_ingest"}
mock_get_connection.return_value = get_conn_value
hook = DruidHook(timeout=1, max_ingestion_time=5)
Expand Down Expand Up @@ -343,6 +376,7 @@ def setup_method(self):
self.conn = conn = MagicMock()
self.conn.host = "host"
self.conn.port = "1000"
self.conn.schema = None
self.conn.conn_type = "druid"
self.conn.extra_dejson = {"endpoint": "druid/v2/sql"}
self.conn.cursor.return_value = self.cur
Expand Down