Skip to content
Advertisement

Using pyspark.sql.functions without sparkContext import problem

I have situation which can be trivialized to example with two files.

filters.py

JavaScript

main.py

JavaScript

It appears, that F.col object cannot be created without active sparkSession/sparkContext object, so import fails.

Is there any way to keep filters separated from other files and how i can import them?

My situation is a little bit more complicated, this filters is used in many different functions across project, so i cant import it inside every function. I need a way to import it safely to global namespace.

Advertisement

Answer

You could create conditions as strings:

filters.py

JavaScript

And then use eval to run is as code:

main.py

JavaScript

The result in this example is, as expected:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement