With mapPartion() or foreachPartition(), you can only modify/iterate the partition data. Nodes can't be invoked while executing the code as it will be executed on the executors. This code should be executed only from the driver node. Thus only from the driver code you can access dataframes or spark session.