gpt4 book ai didi

scala - 大型数据集 i sqlcontext 上的选择查询失败

转载 作者:行者123 更新时间:2023-12-04 15:44:44 26 4
gpt4 key购买 nike

我的代码正在从 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")

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)

最佳答案

错误NumberFormatException大概是在这个地方抛出的:

p(1).trim.toInt

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

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

  • 您还可以 try catch 错误并提供默认值,以防解析出现任何问题:

    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 中。

关于scala - 大型数据集 i sqlcontext 上的选择查询失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56419334/

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