Neo4jOperator¶
Use the Neo4jOperator
to execute
SQL commands in a Neo4j database.
Using the Operator¶
Use the neo4j_conn_id
argument to connect to your Neo4j instance where
the connection metadata is structured as follows:
Parameter |
Input |
---|---|
Host: string |
Neo4j hostname |
Schema: string |
Database name |
Login: string |
Neo4j user |
Password: string |
Neo4j user password |
Port: int |
Neo4j port |
neo4j_task = Neo4jOperator(
task_id="run_neo4j_query",
neo4j_conn_id="neo4j_conn_id",
sql='MATCH (tom {name: "Tom Hanks", date: "{{ds}}"}) RETURN tom',
dag=dag,
)
Passing parameters into the query¶
Neo4jOperator provides parameters
argument to pass parameters into the
query. This allows you to use placeholders in your parameterized query and
substitute them with actual values at execution time.
When using the parameters
argument, you should prefix placeholders in your
query using the $
syntax. For example, if your query uses a placeholder
like $name
, you would provide the parameters as {"name": "value"}
in
the operator. This allows you to write dynamic queries without having to
concatenate strings. This is particularly useful when you want to execute
the same query with different values, or use values from the Airflow
context, such as task instance parameters or variables.
neo4j_task = Neo4jOperator(
task_id="run_neo4j_query_with_parameters",
neo4j_conn_id="neo4j_conn_id",
parameters={"name": "Tom Hanks"},
sql='MATCH (actor {name: $name, date: "{{ds}}"}) RETURN actor',
dag=dag,
)