I'm running a script using Spark which works fine both on my computer and on a Google VM using all cores available in parallel (~6000 processes). However, when I've tried to run it on Azure Databricks using a cluster of min 2 workers and max 25 with 4 cores each and running DB 9.0 it just goes one by one without parallelizing.
Is there any extra set-up needed to run Spark in Databricks other than the usual in any VM?
Here's a test script I've been using to debug the parallelization issue (the cluster load goes <10%):
import pandas as pd
import os
import numpy as np
import datetime
from pyspark.sql import SparkSession
from pyspark.sql.types import BooleanType, StructType,StructField,IntegerType,FloatType, StringType
def main(df_estructural):
sku = df_estructural.Material.iloc[0]
df = pd.read_csv(os.path.join(DATA, 'Sales', str(sku)+'.csv'), header=0, thousands=',', decimal='.', encoding = 'latin-1')
local_unique = len(df.Local.unique())
return pd.DataFrame(np.array([[str(FIN.value), str(sku), str(local_unique )]]), columns=('Date', 'Material', 'Local'))
if __name__ == '__main__':
FIN = '2021-09-27'
DATA = '/dbfs/mnt/simulaciones/data'
DATA_RESULTADOS = '/dbfs/mnt/simulaciones/Resultados'
df = pd.read_csv(os.path.join(DATA, 'list.csv'), sep=',', header=0, thousands='.', decimal=',', encoding = 'latin-1')
df = df_OTB[['Material', 'Alpha']]
spark = SparkSession \
.builder \
.appName("test") \
.config("spark.sql.execution.arrow.pyspark.enabled", "true") \
.getOrCreate()
FIN = spark.sparkContext.broadcast(FIN)
schema = StructType([StructField('Date', StringType(), True),
StructField('Material', StringType(), True),
StructField('Local', StringType(), True)])
df_spark = spark.createDataFrame(df)
df_estructural = df_spark \
.groupby(['Material']) \
.applyInPandas(main, schema=schema) \
.collect()
spark.createDataFrame(df_estructural).toPandas().to_csv(os.path.join(DATA_RESULTADOS,'test.csv')), index=False)