gpt4 book ai didi

scala - 如何避免 Spark NumberFormatException : null

转载 作者:行者123 更新时间:2023-12-01 15:15:00 30 4
gpt4 key购买 nike

我有一个从我遇到的特定异常派生出的一般性问题。

我正在使用 spark 1.6 通过 dataproc 查询数据。我需要从 2 个日志中获取 1 天的数据(~10000 个文件),然后进行一些转换。

但是,我的数据可能(或可能没有)有一些坏数据在一整天的查询都没有成功之后,我尝试了 00-09 小时,没有得到任何错误。尝试了 10-19 小时,但出现异常。逐小时尝试,发现坏数据在小时:10。第 11 和 12 小时还好

基本上我的代码是:

val imps = sqlContext.read.format("com.databricks.spark.csv").option("header", "false").option("inferSchema", "true").load("gs://logs.xxxx.com/2016/03/14/xxxxx/imps/2016-03-14-10*").select("C0","C18","C7","C9","C33","C29","C63").registerTempTable("imps")

val conv = sqlContext.read.format("com.databricks.spark.csv").option("header", "false").option("inferSchema", "true").load("gs://logs.xxxx.com/2016/03/14/xxxxx/conv/2016-03-14-10*").select("C0","C18","C7","C9","C33","C29","C65").registerTempTable("conversions")

val ff = sqlContext.sql("select * from (select * from imps) A inner join (select * from conversions) B on A.C0=B.C0 and A.C7=B.C7 and A.C18=B.C18 ").coalesce(16).write.format("com.databricks.spark.csv").save("gs://xxxx-spark-results/newSparkResults/Plara2.6Mar14_10_1/")

{over - 简化}

我得到的错误是:

org.apache.spark.SparkException: Job aborted due to stage failure: Task 38 in stage 130.0 failed 4 times, most recent failure: Lost task 38.3 in stage 130.0 (TID 88495, plara26-0317-0001-sw-v8oc.c.xxxxx-analytics.internal): java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:542)
at java.lang.Integer.parseInt(Integer.java:615)
at scala.collection.immutable.StringLike$class.toInt(StringLike.scala:229)
at scala.collection.immutable.StringOps.toInt(StringOps.scala:31)
at com.databricks.spark.csv.util.TypeCast$.castTo(TypeCast.scala:53)
at com.databricks.spark.csv.CsvRelation$$anonfun$buildScan$6.apply(CsvRelation.scala:181)
at com.databricks.spark.csv.CsvRelation$$anonfun$buildScan$6.apply(CsvRelation.scala:162)
at scala.collection.Iterator$$anon$13.hasNext(Iterator.scala:371)
at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:327)
at scala.collection.Iterator$$anon$14.hasNext(Iterator.scala:388)
at org.apache.spark.sql.execution.aggregate.TungstenAggregationIterator.processInputs(TungstenAggregationIterator.scala:511)
at org.apache.spark.sql.execution.aggregate.TungstenAggregationIterator.<init>(TungstenAggregationIterator.scala:686)
at org.apache.spark.sql.execution.aggregate.TungstenAggregate$$anonfun$doExecute$1$$anonfun$2.apply(TungstenAggregate.scala:95)
at org.apache.spark.sql.execution.aggregate.TungstenAggregate$$anonfun$doExecute$1$$anonfun$2.apply(TungstenAggregate.scala:86)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitions$1$$anonfun$apply$20.apply(RDD.scala:710)
at org.apache.spark.rdd.RDD$$anonfun$mapPartitions$1$$anonfun$apply$20.apply(RDD.scala:710)
at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:306)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:270)
at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:306)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:270)
at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:73)
at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:41)
at org.apache.spark.scheduler.Task.run(Task.scala:89)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:213)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

所以我的问题是 - 如何使用 spark-csv 实现异常处理?我可以将数据帧转换为 RDD 并在那里进行处理,但似乎必须有更好的方法......

有人解决过类似的问题吗?

最佳答案

这是因为自动推断架构对于输入文件中的无效数据并不安全。

当使用不同的输入文件时,这可能会导致数据帧架构不同。

假设我们有一个带有 float 的 csv 文件,其中包含一个字符串:

0.018
0.095
0.000
'hoi'
0.000
0.093
0.012

当我们使用 inferSchema 将其读入数据帧时,如下所示:

>>> df = spark.read.format('csv').option('inferSchema', True).load('./test_csv.dat')
>>> df.show()
+-----+
| _c0|
+-----+
|0.018|
|0.095|
|0.000|
|'hoi'|
|0.000|
|0.093|
|0.012|
+-----+

那么类型没有正确推断:

>>> df.schema
StructType(List(StructField(_c0,StringType,true)))

您可以通过手动转换列来解决这个问题,如下所示:

>>> df = df.withColumn('val_float', df_tmp._c0.cast(FloatType())).select('val_float')
>>> df.show()
+---------+
|val_float|
+---------+
| 0.018|
| 0.095|
| 0.0|
| null|
| 0.0|
| 0.093|
| 0.012|
+---------+

>>> df.schema
StructType(List(StructField(val_float,FloatType,true)))

关于scala - 如何避免 Spark NumberFormatException : null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36057276/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com