我应该在代码中添加些什么,以避免使用pyspark出现“超出最大允许字节数”错误?

时间:2019-02-20 19:20:48

标签: python apache-spark pyspark

我有一个具有400万行和10列的数据框。我正在尝试使用pyspark将它写到Cloudera Data Science Workbench的hdfs表中。尝试执行此操作时遇到错误:

[Stage 0:>                                                          (0 + 1) / 
2]19/02/20 12:31:04 ERROR datasources.FileFormatWriter: Aborting job null.
org.apache.spark.SparkException: Job aborted due to stage failure: Serialized task 0:0 was 318690577 bytes, which exceeds max allowed: spark.rpc.message.maxSize (134217728 bytes). Consider increasing spark.rpc.message.maxSize or using broadcast variables for large values.

我可以将数据帧分解为3个数据帧,并分别执行3次Spark写入操作,但是如果可能的话,我想只执行一次,只需在Spark代码中添加诸如coalesce之类的内容即可。

import pandas as pd
df=pd.read_csv('BulkWhois/2019-02-20_Arin_Bulk/Networks_arin_db_2-20-2019_parsed.csv')

'''PYSPARK'''
from pyspark.sql import SQLContext
from pyspark.sql import *
from pyspark.sql.types import *
from pyspark import SparkContext
spark = SparkSession.builder.appName('Arin_Network').getOrCreate()

schema = StructType([StructField('NetHandle', StringType(), False),
                     StructField('OrgID', StringType(), True),
                     StructField('Parent', StringType(), True),
                     StructField('NetName', StringType(), True),
                     StructField('NetRange', StringType(), True),
                     StructField('NetType', StringType(), True),
                     StructField('Comment', StringType(), True),
                     StructField('RegDate', StringType(), True),
                     StructField('Updated', StringType(), True),
                     StructField('Source', StringType(), True)])

dataframe = spark.createDataFrame(df, schema)
dataframe.write. \
  mode("append"). \
  option("path", "/user/hive/warehouse/bulkwhois_analytics.db/arin_network"). \
  saveAsTable("bulkwhois_analytics.arin_network")

1 个答案:

答案 0 :(得分:0)

User10465355提到我应该直接使用Spark。这样做比较简单,并且是实现此目的的正确方法。

from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('Networks').getOrCreate()

dataset = spark.read.csv('Networks_arin_db_2-20-2019_parsed.csv', header=True, inferSchema=True)
dataset.show(5)

dataset.write \
  .mode("append") \
  .option("path", "/user/hive/warehouse/analytics.db/arin_network") \
  .saveAsTable("analytics.arin_network")