Skip to content

Commit

Permalink
Add Neo4j hook and operator (apache#13324)
Browse files Browse the repository at this point in the history
  • Loading branch information
subkanthi authored Jan 14, 2021
1 parent c128aa7 commit 1d2977f
Show file tree
Hide file tree
Showing 24 changed files with 710 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ apache.webhdfs, async, atlas, aws, azure, cassandra, celery, cgroups, cloudant,
crypto, dask, databricks, datadog, devel, devel_all, devel_ci, devel_hadoop, dingding, discord, doc,
docker, druid, elasticsearch, exasol, facebook, ftp, gcp, gcp_api, github_enterprise, google,
google_auth, grpc, hashicorp, hdfs, hive, http, imap, jdbc, jenkins, jira, kerberos, kubernetes,
ldap, microsoft.azure, microsoft.mssql, microsoft.winrm, mongo, mssql, mysql, odbc, openfaas,
ldap, microsoft.azure, microsoft.mssql, microsoft.winrm, mongo, mssql, mysql, neo4j, odbc, openfaas,
opsgenie, oracle, pagerduty, 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, vertica, virtualenv, webhdfs, winrm,
Expand Down
2 changes: 1 addition & 1 deletion INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ apache.webhdfs, async, atlas, aws, azure, cassandra, celery, cgroups, cloudant,
crypto, dask, databricks, datadog, devel, devel_all, devel_ci, devel_hadoop, dingding, discord, doc,
docker, druid, elasticsearch, exasol, facebook, ftp, gcp, gcp_api, github_enterprise, google,
google_auth, grpc, hashicorp, hdfs, hive, http, imap, jdbc, jenkins, jira, kerberos, kubernetes,
ldap, microsoft.azure, microsoft.mssql, microsoft.winrm, mongo, mssql, mysql, odbc, openfaas,
ldap, microsoft.azure, microsoft.mssql, microsoft.winrm, mongo, mssql, mysql, neo4j, odbc, openfaas,
opsgenie, oracle, pagerduty, 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, vertica, virtualenv, webhdfs, winrm,
Expand Down
18 changes: 18 additions & 0 deletions airflow/providers/neo4j/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!--
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/neo4j/__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/neo4j/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.
48 changes: 48 additions & 0 deletions airflow/providers/neo4j/example_dags/example_neo4j.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
# 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.
"""
Example use of Neo4j related operators.
"""

from airflow import DAG
from airflow.providers.neo4j.operators.neo4j import Neo4jOperator
from airflow.utils.dates import days_ago

default_args = {
'owner': 'airflow',
}

dag = DAG(
'example_neo4j',
default_args=default_args,
start_date=days_ago(2),
tags=['example'],
)

# [START run_query_neo4j_operator]

neo4j_task = Neo4jOperator(
task_id='run_neo4j_query',
neo4j_conn_id='neo4j_conn_id',
sql='MATCH (tom {name: "Tom Hanks"}) RETURN tom',
dag=dag,
)

# [END run_query_neo4j_operator]

neo4j_task
17 changes: 17 additions & 0 deletions airflow/providers/neo4j/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.
117 changes: 117 additions & 0 deletions airflow/providers/neo4j/hooks/neo4j.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#
# 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.

"""This module allows to connect to a Neo4j database."""

from neo4j import GraphDatabase, Neo4jDriver, Result

from airflow.hooks.base import BaseHook
from airflow.models import Connection


class Neo4jHook(BaseHook):
"""
Interact with Neo4j.
Performs a connection to Neo4j and runs the query.
"""

conn_name_attr = 'neo4j_conn_id'
default_conn_name = 'neo4j_default'
conn_type = 'neo4j'
hook_name = 'Neo4j'

def __init__(self, conn_id: str = default_conn_name, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.neo4j_conn_id = conn_id
self.connection = kwargs.pop("connection", None)
self.client = None
self.extras = None
self.uri = None

def get_conn(self) -> Neo4jDriver:
"""
Function that initiates a new Neo4j connection
with username, password and database schema.
"""
self.connection = self.get_connection(self.neo4j_conn_id)
self.extras = self.connection.extra_dejson.copy()

self.uri = self.get_uri(self.connection)
self.log.info('URI: %s', self.uri)

if self.client is not None:
return self.client

is_encrypted = self.connection.extra_dejson.get('encrypted', False)

self.client = GraphDatabase.driver(
self.uri, auth=(self.connection.login, self.connection.password), encrypted=is_encrypted
)

return self.client

def get_uri(self, conn: Connection) -> str:
"""
Build the uri based on extras
- Default - uses bolt scheme(bolt://)
- neo4j_scheme - neo4j://
- certs_self_signed - neo4j+ssc://
- certs_trusted_ca - neo4j+s://
:param conn: connection object.
:return: uri
"""
use_neo4j_scheme = conn.extra_dejson.get('neo4j_scheme', False)
scheme = 'neo4j' if use_neo4j_scheme else 'bolt'

# Self signed certificates
ssc = conn.extra_dejson.get('certs_self_signed', False)

# Only certificates signed by CA.
trusted_ca = conn.extra_dejson.get('certs_trusted_ca', False)
encryption_scheme = ''

if ssc:
encryption_scheme = '+ssc'
elif trusted_ca:
encryption_scheme = '+s'

return '{scheme}{encryption_scheme}://{host}:{port}'.format(
scheme=scheme,
encryption_scheme=encryption_scheme,
host=conn.host,
port='7687' if conn.port is None else f'{conn.port}',
)

def run(self, query) -> Result:
"""
Function to create a neo4j session
and execute the query in the session.
:param query: Neo4j query
:return: Result
"""
driver = self.get_conn()
if not self.connection.schema:
with driver.session() as session:
result = session.run(query)
else:
with driver.session(database=self.connection.schema) as session:
result = session.run(query)
return result
17 changes: 17 additions & 0 deletions airflow/providers/neo4j/operators/__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.
62 changes: 62 additions & 0 deletions airflow/providers/neo4j/operators/neo4j.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#
# 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 Dict, Iterable, Mapping, Optional, Union

from airflow.models import BaseOperator
from airflow.providers.neo4j.hooks.neo4j import Neo4jHook
from airflow.utils.decorators import apply_defaults


class Neo4jOperator(BaseOperator):
"""
Executes sql code in a specific Neo4j database
.. seealso::
For more information on how to use this operator, take a look at the guide:
:ref:`howto/operator:Neo4jOperator`
:param sql: the sql code to be executed. Can receive a str representing a
sql statement, a list of str (sql statements)
:type sql: str or list[str]
:param neo4j_conn_id: reference to a specific Neo4j database
:type neo4j_conn_id: str
"""

@apply_defaults
def __init__(
self,
*,
sql: str,
neo4j_conn_id: str = 'neo4j_default',
parameters: Optional[Union[Mapping, Iterable]] = None,
**kwargs,
) -> None:
super().__init__(**kwargs)
self.neo4j_conn_id = neo4j_conn_id
self.sql = sql
self.parameters = parameters
self.hook = None

def get_hook(self):
"""Function to retrieve the Neo4j Hook."""
return Neo4jHook(conn_id=self.neo4j_conn_id)

def execute(self, context: Dict) -> None:
self.log.info('Executing: %s', self.sql)
self.hook = self.get_hook()
self.hook.run(self.sql)
44 changes: 44 additions & 0 deletions airflow/providers/neo4j/provider.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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-neo4j
name: Neo4j
description: |
`Neo4j <https://neo4j.com/>`__
versions:
- 1.0.0
integrations:
- integration-name: Neo4j
external-doc-url: https://neo4j.com/
how-to-guide:
- /docs/apache-airflow-providers-neo4j/operators/neo4j.rst
tags: [software]

operators:
- integration-name: Neo4j
python-modules:
- airflow.providers.neo4j.operators.neo4j

hooks:
- integration-name: Neo4j
python-modules:
- airflow.providers.neo4j.hooks.neo4j

hook-class-names:
- airflow.providers.neo4j.hooks.neo4j.Neo4jHook
Loading

0 comments on commit 1d2977f

Please sign in to comment.