pyspark:如何通过多处理并行处理Dataframe的多列?

ss2ws0br  于 2021-05-27  发布在  Spark
关注(0)|答案(0)|浏览(588)

我想 QuantileDiscretizer pyspark中的dataframe列。但是有大约 4,000 要转换的列。所以我想用 multiprocessing 方法如下。

import multiprocessing as mp
import multiprocessing.pool
from pyspark.ml.feature import QuantileDiscretizer

def transform_col(train,col,numBuckets=5):
    '''
    return: df
    '''
    discretizer = QuantileDiscretizer(numBuckets=numBuckets, inputCol=col, outputCol=col + "_bin")
    discretizer = discretizer.fit(train)
    train = discretizer.transform(train)
    return train

# just an example.

train_df = spark.createDataFrame([[1,2],[3,4],[3,5],[8,8],[3,9],[8,1],[7,1]],["a","b"])
pool = mp.Pool(processes=mp.cpu_count() - 1)

# arguments

process_col = ["a","b"]
args = zip([train_df.select(col) for col in process_col],
           [col for col in process_col]
           )
res = dict(zip([col for col in process_col], pool.starmap(transform_col, args)))
for col in res.keys():
    train_df = train_df.withColumn("process_{}".format(col), res[col].select(col)).drop(col)
pool.close()
pool.join()

但我遇到了以下错误。错误:

Py4JError: An error occurred while calling o385.__getstate__. Trace:
py4j.Py4JException: Method __getstate__([]) does not exist
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318)
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:326)
    at py4j.Gateway.invoke(Gateway.java:274)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)

那么,有没有办法解决这个问题呢?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题