【问题标题】:Spark Scala - rdd distinct nullpointerexceptionSpark Scala - rdd distinct nullpointerexception
【发布时间】:2021-03-14 08:51:18
【问题描述】:

我正在使用 spark 进行小步骤,我的练习将 JSON 文件加载到 RDD 中,选择一列,然后使用 distinct 获取唯一值。 我正在过滤的列包含多个值(CSV 行)并且必须拆分。

val sqlContext = spark.sqlContext
import org.apache.spark.sql.hive.HiveContext
val hiveCtx = new HiveContext(sc)
import hiveCtx.implicits._
val bizDF = hiveCtx.jsonFile("/home/xpto/Documents/PersonalProjects/Yelp_P1/yelp_academic_dataset_business.json")
val catRdd = bizDF.select("categories").rdd.flatMap(row => (row.getString(0).split(",").map(_.trim))).distinct

当我运行语句“catRdd.take (10).foreach (println)”时返回异常:

org.apache.spark.SparkException:作业因阶段故障而中止:阶段 234.0 中的任务 0 失败 1 次,最近一次失败:阶段 234.0 中丢失任务 0.0(TID 682、192.168.0.122、执行程序驱动程序):

java.lang.NullPointerException
    at $anonfun$catRdd$1(<console>:39)
    at $anonfun$catRdd$1$adapted(<console>:39)
    at scala.collection.Iterator$$anon$11.nextCur(Iterator.scala:484)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:490)
    at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:458)
    at org.apache.spark.util.collection.ExternalSorter.insertAll(ExternalSorter.scala:192)
    at org.apache.spark.shuffle.sort.SortShuffleWriter.write(SortShuffleWriter.scala:62)
    at org.apache.spark.shuffle.ShuffleWriteProcessor.write(ShuffleWriteProcessor.scala:59)
    at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:99)
    at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:52)
    at org.apache.spark.scheduler.Task.run(Task.scala:127)
    at org.apache.spark.executor.Executor$TaskRunner.$anonfun$run$3(Executor.scala:446)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1377)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:449)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

Driver stacktrace:
  at org.apache.spark.scheduler.DAGScheduler.failJobAndIndependentStages(DAGScheduler.scala:2059)
  at org.apache.spark.scheduler.DAGScheduler.$anonfun$abortStage$2(DAGScheduler.scala:2008)
  at org.apache.spark.scheduler.DAGScheduler.$anonfun$abortStage$2$adapted(DAGScheduler.scala:2007)
  at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)
  at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)
  at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)
  at org.apache.spark.scheduler.DAGScheduler.abortStage(DAGScheduler.scala:2007)
  at org.apache.spark.scheduler.DAGScheduler.$anonfun$handleTaskSetFailed$1(DAGScheduler.scala:973)
  at org.apache.spark.scheduler.DAGScheduler.$anonfun$handleTaskSetFailed$1$adapted(DAGScheduler.scala:973)
  at scala.Option.foreach(Option.scala:407)
  at org.apache.spark.scheduler.DAGScheduler.handleTaskSetFailed(DAGScheduler.scala:973)
  at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.doOnReceive(DAGScheduler.scala:2239)
  at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:2188)
  at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:2177)
  at org.apache.spark.util.EventLoop$$anon$1.run(EventLoop.scala:49)
  at org.apache.spark.scheduler.DAGScheduler.runJob(DAGScheduler.scala:775)
  at org.apache.spark.SparkContext.runJob(SparkContext.scala:2099)
  at org.apache.spark.SparkContext.runJob(SparkContext.scala:2120)
  at org.apache.spark.SparkContext.runJob(SparkContext.scala:2139)
  at org.apache.spark.rdd.RDD.$anonfun$take$1(RDD.scala:1423)
  at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
  at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
  at org.apache.spark.rdd.RDD.withScope(RDD.scala:388)
  at org.apache.spark.rdd.RDD.take(RDD.scala:1396)
  ... 48 elided
Caused by: java.lang.NullPointerException
  at $anonfun$catRdd$1(<console>:39)
  at $anonfun$catRdd$1$adapted(<console>:39)
  at scala.collection.Iterator$$anon$11.nextCur(Iterator.scala:484)
  at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:490)
  at scala.collection.Iterator$$anon$10.hasNext(Iterator.scala:458)
  at org.apache.spark.util.collection.ExternalSorter.insertAll(ExternalSorter.scala:192)
  at org.apache.spark.shuffle.sort.SortShuffleWriter.write(SortShuffleWriter.scala:62)
  at org.apache.spark.shuffle.ShuffleWriteProcessor.write(ShuffleWriteProcessor.scala:59)
  at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:99)
  at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:52)
  at org.apache.spark.scheduler.Task.run(Task.scala:127)
  at org.apache.spark.executor.Executor$TaskRunner.$anonfun$run$3(Executor.scala:446)
  at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1377)
  at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:449)
  ... 3 more

我运行的spark版本是2.12-3.0.1

【问题讨论】:

  • 请展示您的输入文件的样子
  • 我尝试使用我自己的 json 文件,它对我有用,没有错误
  • 您好,非常感谢您的快速回复。文件布局你可以在这里找到业务模式:yelp.com/dataset/documentation/main我在本地使用 zeppelin 运行这个,我不知道是不是问题。

标签: scala apache-spark


【解决方案1】:

我找到了适合我要求的解决方案:

import org.apache.spark.sql.functions._
import org.apache.spark.sql.expressions.Window
val v1 = bizDF.withColumn("categories", split(col("categories"), ","))
  .select(col("categories")(0).as("description"))
  .distinct
  .coalesce(1)
  .orderBy(asc("description"))

val windowSpec = Window.orderBy("description")
val v2 = v1.withColumn("id",row_number.over(windowSpec))
val v3 = v2.select("id","description")

【讨论】:

    【解决方案2】:

    您的 json 文件有多行,HiveCtx 不支持。改用 spark session 试试这个:

    val bizDF = spark.read.format("json").option("multiline", "true").load("/home/xpto/Documents/PersonalProjects/Yelp_P1/yelp_academic_dataset_business.json")
    val catRdd = bizDF.select("categories").rdd.flatMap(row => (row.getString(0).split(",").map(_.trim))).distinct
    catRdd.take(10).foreach(println)
    

    【讨论】:

    • 这可行,但只需要 json 的第一项。我加载的文件有多个记录。我将架构发送给您,但整个数据集都在这里:yelp.com/dataset
    • @xavy 它对我有用,而且不只是拿第一项。您能否发布一个较小的 json 示例、预期输出和我的代码输出?
    • 前两行有这个:Active Life, Gun\/Rifle Ranges, Guns & Ammo, Shopping Hardware Stores, Home Services, Building Supplies, Home & Garden, Shopping after the distinct, the second "购物”应该被淘汰。但在我的情况下返回一个错误。
    猜你喜欢
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 2018-09-29
    • 2016-12-16
    相关资源
    最近更新 更多