选择“查询在大型数据集 I SQL 上下文上失败”



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

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 中任务 0.0 中的异常 (TID 604( java.lang.ArrayIndexOutOfBounds异常: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]

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

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

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

相关内容

最新更新