cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

DatabricksSession and SparkConf

fdeba
New Contributor

Hi,

I want to initialize a Spark session using `DatabricksSession`. However, it seems not possible to call `.config()` and pass it a `SparkConf` instance. The following works:

# Initialize the configuration for the Spark session
confSettings = [
    ("spark.sql.legacy.timeParserPolicy", "CORRECTED"),
    ("spark.sql.mapKeyDedupPolicy", "LAST_WIN"),
    ("spark.sql.legacy.parquet.nanosAsLong", "true"),
]
conf = SparkConf() \
    .setMaster("local") \
    .setAll(confSettings) \
    .setExecutorEnv(confSettings)

# Initialize a Spark session
spark = SparkSession.builder \
    .config(conf=conf) \
    .getOrCreate()
 
But the following throws an error:
 
```
# Initialize the configuration for the Spark session
confSettings = [
    ("spark.sql.legacy.timeParserPolicy", "CORRECTED"),
    ("spark.sql.mapKeyDedupPolicy", "LAST_WIN"),
    ("spark.sql.legacy.parquet.nanosAsLong", "true"),
]
conf = SparkConf() \
    .setMaster("local") \
    .setAll(confSettings) \
    .setExecutorEnv(confSettings)

# Initialize a Spark session
spark = DatabricksSession.builder \
    .profile("<profile-name>") \
    .config(conf=conf) \    
    .getOrCreate()
```

Is there another way to set configuration for a Spark session when using `DatabricksSession`?

Thanks.
2 REPLIES 2

Slash
Contributor

 

Hi @fdeba ,

Maybe try to set them up after you acquire spark session with spark.conf.set , for example using loop:

 

 

 

 

# Initialize the configuration for the Spark session
confSettings = [
    ("spark.sql.legacy.timeParserPolicy", "CORRECTED"),
    ("spark.sql.mapKeyDedupPolicy", "LAST_WIN"),
    ("spark.sql.legacy.parquet.nanosAsLong", "true"),
]


# Initialize a Spark session
spark = DatabricksSession.builder \
    .profile("<profile-name>") \
    .config(conf=conf) \    
    .getOrCreate()

for conf in confSettings:
    spark.conf.set(conf[0], conf[1])

 

 

 

 

 

Witold
Contributor

In almost all cases you don't need to create a new spark session, as Databricks will do it for you automatically.

If it's only about spark configurations, there are multiple ways to set it:

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group