0

I am using databricks notebook and neo4j spark connector to run cypher query to create constraints. While executing its given an error.. I tried multiple ways to change the databricks runtime version and spark connector version, but no luck.

Error msg: IllegalArgumentException: Please provide a valid WRITE query.

I tried to test using multiple version of databricks runtime along with spark connector and did not get any luck. Here is the code.

from pyspark.sql import SparkSession

# Initialize Spark session
spark = SparkSession.builder \
 .appName("Neo4j Integration") \
 .getOrCreate()

# Neo4j connection config
neo4j_config = {
 "url": "bolt://localhost:7687",
 "authentication.type": "basic",
 "authentication.basic.username": "<username>",
 "authentication.basic.password": "password"
}

# Sample data
data = [
{"id": "1", "name": "Alice"}
]
 
# Create DataFrame
df = spark.createDataFrame(data)
 
 
Constraints_Query = """
CREATE CONSTRAINT unique_person IF NOT EXISTS FOR (p:Person) REQUIRE p.name IS UNIQUE
"""
# Execute Cypher query to create constraint
df.write.format("org.neo4j.spark.DataSource") \
    .options(**neo4j_config) \
    .option("query", Constraints_Query) \
    .option("database", "neo4j")\
    .mode('Overwrite')\
    .save()

1 Answer 1

1

If you want to initialize constraints with the Spark Connector for Neo4j, you have two choices:

  1. configure the right schema optimization strategy, constraints will be inferred from your dataframe schema

  2. use the script option (where the value would be CREATE CONSTRAINT unique_person IF NOT EXISTS FOR (p:Person) REQUIRE p.name IS UNIQUE)

In any case, your query call must be for data imports, not schema initialization.
The official documentation hopefully helps in that regards.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @fbiville for your quick help. I am able to re-write with schema optimization and it worked.
Glad to hear that! Can you please mark the answer as accepted if it worked for you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.