【问题标题】:select query fails on large dataset i sqlcontext在大型数据集 i sqlcontext 上选择查询失败
【发布时间】:2019-06-02 22:17:10
【问题描述】:

我的代码正在从 sqlcontext 读取数据。该表中有 2000 万条记录。我想在表中计算 totalCount。

val finalresult = sqlContext.sql(“SELECT movieid,
tagname, occurrence AS eachTagCount, count AS
totalCount FROM result ORDER BY movieid”) 

我想在不使用 groupby 的情况下计算一列的总数并将其保存在文本文件中。 .我更改了我的保存文件而无需额外]

 >val sqlContext = new org.apache.spark.sql.SQLContext(sc)
    import sqlContext.implicits._
import sqlContext._
case class DataClass(UserId: Int, MovieId:Int, Tag: String)
// Create an RDD of DataClass objects and register it as a table.
val Data = sc.textFile("file:///usr/local/spark/dataset/tagupdate").map(_.split(",")).map(p => DataClass(p(0).trim.toInt, p(1).trim.toInt, p(2).trim)).toDF()
Data.registerTempTable("tag")

val orderedId = sqlContext.sql("SELECT MovieId AS Id,Tag FROM tag ORDER BY MovieId")
orderedId.rdd
  .map(_.toSeq.map(_+"").reduce(_+";"+_))
  .saveAsTextFile("/usr/local/spark/dataset/algorithm3/output")
  // orderedId.write.parquet("ordered.parquet")
val eachTagCount =orderedId.groupBy("Tag").count()
//eachTagCount.show()
eachTagCount.rdd
 .map(_.toSeq.map(_+"").reduce(_+";"+_))
 .saveAsTextFile("/usr/local/spark/dataset/algorithm3/output2")

错误执行程序:阶段 7.0 (TID 604) 中任务 0.0 中的异常 java.lang.ArrayIndexOutOfBoundsException: 1 at tags$$anonfun$6.apply(tags.scala:46) at tags$$anonfun$6.apply(tags. scala:46) 在 scala.collection.Iterator$$anon$11.next(Iterator.scala:410)

【问题讨论】:

  • 您的问题到底是什么?答案很可能在您的帖子中,上面写着NumberFormatException: For input string: "10]"
  • 出现此异常的原因及解决方法
  • 那么答案肯定在你原来的帖子里。
  • 我想在不使用 groupby 的情况下计算一列的总数并将其保存在文本文件中
  • 然后,一旦您解决了这个问题,请尝试这样做,如果您遇到困难,请提出另一个问题,显示您编写的代码以及您的特定问题。请参考How to ask a good question

标签: scala apache-spark


【解决方案1】:

错误NumberFormatException可能是在这个地方抛出的:

p(1).trim.toInt

它被抛出是因为你试图解析 10] 这显然不是一个有效的数字。

  • 您可以尝试在文件中找到有问题的地方,然后删除额外的]

  • 您也可以尝试捕获错误并提供默认值以防解析出现任何问题:

    import scala.util.Try
    
    Try(p(1).trim.toInt).getOrElse(0) //return 0 in case there is problem with parsing.
    
  • 您可以做的另一件事是从您尝试解析的字符串中删除不是数字的字符:

    //filter out everything which is not a digit
    p(1).filter(_.isDigit).toInt)
    

如果所有内容都被过滤掉并留下一个空字符串,它也可能会失败,因此最好将其包装在 Try 中。

【讨论】:

  • thanks@Krzysztof Atłasik 。我使用 eachTagCount.rdd .map(.toSeq.map(_+"").reduce(_+";"+ )) .saveAsTextFile("/usr/local/spark/dataset/algorithm3/output2") 但出现另一个异常 ERROR Executor: Exception in task 0.0 in stage 7.0 (TID 604) java.lang.ArrayIndexOutOfBoundsException: 1 at tags$$anonfun$6.apply(tags.scala:46) at tags$$anonfun$6.apply(tags.scala:46) at scala.collection.Iterator$$anon$11.next(Iterator.scala:410) 任何帮助
  • 你能编辑你原来的问题并添加这个错误吗?
猜你喜欢
  • 2018-04-19
  • 1970-01-01
  • 1970-01-01
  • 2018-11-19
  • 2020-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-22
相关资源
最近更新 更多