Amazon EMR群集上的spark-csv出错

时间:2017-01-24 19:41:49

标签: apache-spark amazon-emr spark-csv

我正在尝试使用简单的Spark步骤执行来运行EMR集群,并且遇到了无法解决的错误。当我在Eclipse中本地运行它时,该程序可以正常工作,但是当我在EMR集群上运行它时,它不会运行。该程序只是试图将S3上的CSV文件转换为Parquet格式。

当我在EMR中运行时,出现以下错误:

  

引起:com.univocity.parsers.common.TextParsingException:已解析输入的长度(1000001)超过解析器设置(1000000)中定义的最大字符数。   在已解析的内容中标识了行分隔符。这可能是错误的原因。解析器设置中的行分隔符设置为“\ n”。解析内容:

我没有超过1000000限制的任何字段。我试过从s3,s3n和s3a位置读取。

    import org.apache.spark.SparkSession
    import org.apache.spark.sql.types._

    object TestEMR {
      def main(args: Array[String]) {
        val spark = SparkSession.builder().appName("Spark Convert to Parquet").getOrCreate()
        val schema = StructType(
            Array(
              StructField("field1", StringType ,nullable = true),
              StructField("field2", IntegerType ,nullable = true),
              StructField("field3", IntegerType ,nullable = true),
              StructField("field4", TimestampType ,nullable = true),
              StructField("field5", TimestampType ,nullable = true),
              StructField("field6", StringType ,nullable = true),
              StructField("field7", StringType ,nullable = true),
              StructField("field8", StringType ,nullable = true),
              StructField("field9", StringType ,nullable = true),
              StructField("field10", StringType ,nullable = true),
              StructField("field11", StringType ,nullable = true),
              StructField("field12", StringType ,nullable = true),
              StructField("field13", StringType ,nullable = true),
              StructField("field14", StringType ,nullable = true),
              StructField("field15", StringType ,nullable = true),
              StructField("field16", StringType ,nullable = true),
              StructField("field17", StringType ,nullable = true),
              StructField("field18", StringType ,nullable = true),
              StructField("field19", StringType ,nullable = true),
              StructField("field20", StringType ,nullable = true)
            )
          )

        val df = spark.read
            .format("com.databricks.spark.csv")
            .schema(schema)
            .option("nullValue","")
            .option("treatEmptyValuesAsNulls","true")
            .load("s3://mybucket/input/myfile.csv")
       df.write.mode("append").parquet("s3://mybucket/output/myfile")
       spark.stop
      }
    }

2 个答案:

答案 0 :(得分:0)

听起来没有找到行结尾,所以不断读取,直到它达到单行上10K字符的限制。

正如他们所说:检查该文件的换行符

答案 1 :(得分:0)

此问题仍在spark-csv jira中公开。如果您没有数据问题或读取RDD然后创建数据帧,他们提供了使用open csv解析器的解决方法。

val rdd = sc.textFile("file.csv")
// Here, filtering or transformation
//val filteredRDD = rdd.filter..
//val transformedRDD = rdd.map..

val df = new CsvParser().csvRdd(sqlContext, transformedRDD)